using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runtime.InteropServices;using System.Globalization;using System.Security;namespace USBBulkExample{ class Program { [DllImport("lmusbdll.dll", EntryPoint = "InitializeDevice", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] public static extern IntPtr InitializeDevice(ushort usVID, ushort usPID, ref System.Guid lpGUID, ref bool pbDriverInstalled); [DllImport("lmusbdll.dll", EntryPoint = "ReadUSBPacket", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] public static extern UInt32 ReadUSBPacket(IntPtr hHandle, [In] char[] pcBuffer, UInt32 ulSize, ref UInt32 pulRead, UInt32 ulTimeoutMs, IntPtr hBreak); [DllImport("lmusbdll.dll", EntryPoint = "WriteUSBPacket", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] public static extern bool WriteUSBPacket(IntPtr hHandle, [Out] char[] pcBuffer, UInt32 ulSize, ref UInt32 pulWritten); [DllImport("lmusbdll.dll", EntryPoint = "TerminateDevice", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] public static extern bool TerminateDevice(IntPtr hHandle); static void Main(string[] args) { UInt32 dwError; char[] szBuffer = new char[256]; const UInt32 INFINITE = 0xFFFFFFFF; ushort BULK_VID = 0x1cbe; ushort BULK_PID = 0x0003; bool bDriverInstalled = false; UInt32 ulRead = 0; UInt32 pulWritten = 0; IntPtr hBreak = IntPtr.Zero; IntPtr hUSB = IntPtr.Zero; // Create a GUID, match to what is in INF file System.Guid GUID_DEVINTERFACE_LUMINARY_BULK; GUID_DEVINTERFACE_LUMINARY_BULK = new System.Guid("{273FD48E-C23F-4d4b-B48B-5E5CAA3B3D99}"); // Init the USB device hUSB = InitializeDevice(BULK_VID, BULK_PID, ref GUID_DEVINTERFACE_LUMINARY_BULK, ref bDriverInstalled); // Load up array with useless "t" int x; for (x = 0; x <>256; x++) { szBuffer[x] = Convert.ToChar("t"); } // do this transaction 10k times for the fun of it... int y; for (y = 0; y <>10000; y++) { bool ret = WriteUSBPacket(hUSB, szBuffer, 255, ref pulWritten); dwError = ReadUSBPacket(hUSB, szBuffer, 255, ref ulRead, INFINITE, hBreak); } // Close the connection bool ret1 = TerminateDevice(hUSB); } }}
Thanks, this was helpful.
I did have to change the char buffers to byte buffers to get it to work for me.
Maybe something has changed in the lmusbdll, but the version I'm using defines the buffers as 8-bit UN-signed.
This is really great stuff! Do you have any examples interfacing C# with the LMDFU library? I'm running into problems interfacing to them with the pointer structures.
No sorry I don't.
Does anyone have an idea what baud rate can be reached with this Read/Write function calls?
Grtz 3s
Hi, I keep getting the following error:
A call to PInvoke function 'USBBulkExample!USBBulkExample.Program::InitializeDevice' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
when i run the project. Does that have something to do with the windows version? (Win7, 64-bit) or am I doing something wrong?
//Hans Peter
Hans Peter,
This does appear like the wrong driver was loaded. Have you made sure the 64-bit driver/dll have been loaded onto your system?
Denis
I have installed 64-bit drivers, but the program is running as a 32-bit program. Could that cause the problem? Will try on a 32-bit machine...
Yes, that could definitely be the problem since call stacks would be different. GL
Hi there!
I currently develop some USB software on a LM3S9B92 board and tried to use your project for the communication between PC and Device. All times I debug the software, hUsb returns "0" and at the second round I send something, dwError changed to "87". When I start the compiled USB_dev_Example.exe it runs great. Are there any ideas, what problem i have?
Hi again,
after some testing I got a return value for hUsb and bool ret is now true after sending the first block. My new problem is, that szBuffer is always filled with zero after sending it first. After running the recieve function it is left zero. I got the feeling that no data has been send. An interruot occurs on the uC but i cant read the buffer there, to see if something has been recieved!
Are there any ideas?