C++ dll for USB download in C# application

csharp , cpp dll , usb download India
  • 12 years ago
    Hi all, I am using a C++ dll (with functions to initialize, deinitialize, read and write data to USB port) in my C# application(with UI which allows user to select files to write to USB port). The objective of the application is to allow user select a file to download from PC to the target device and receive acknowledgements for every 256 bytes of data received. Once the entire file is downloaded a final acknowledgement is received. For USB communication, C++ API's are used. The problem that I am facing is that the application behaves unpredictably and fails to receive final acknowledgement sometimes. A single test application with USB API's (not converted to dll) in C++ (without UI) works fine, which means the target code and the API's are working fine. Code snippets here... public class UsbDownload { #region Imported DLL functions from usb.dll [DllImport("usb.dll")] private static extern HANDLE usbInit(); [DllImport("usb.dll")] private static extern BOOL usbDeinit(HANDLE hObject); [DllImport("usb.dll")] unsafe private static extern BOOL usbWrite(HANDLE hObject, void* buffer,DWORD nNumberOfBytesToWrite, ref System.UInt32 nNumberOfBytesWritten); [DllImport("usb.dll")] unsafe private static extern BOOL usbRead(HANDLE hObject, void* buffer,DWORD nNumberOfBytesToRead, ref System.UInt32 nNumberOfBytesRead); #endregion private bool writeFile(long nFileSize, byte[] filebuffer) { int nSize = 1; byte readData = 0; while (nSize <= nFileSize) { writeByte(filebuffer[nSize - 1]); if (nSize % TARGET_BUFFER_SIZE == 0) { #if (NO_ACK) Thread.Sleep(3); #else readByte(ref readData); if(readData != ACK_BYTE) { return false; } #endif } nSize++; } readData = 0; readByte(ref readData); if (readData != ACK_BYTE) { return false; } return true; } unsafe private bool writeByte(byte wrData) { byte[] data = new byte[1]; UInt32 noOfBytesWritten = 0; bool result = false; data[0] = wrData; fixed (byte* writePtr = data) { result = usbWrite(m_handle, writePtr, 1, ref noOfBytesWritten); } return result; } unsafe private bool readByte(ref byte rdData) { byte[] data = new byte[1]; UInt32 noOfBytesRead = 0; bool result = false; fixed (byte* readPtr = data) { result = usbRead(m_handle, readPtr, 1, ref noOfBytesRead); rdData = data[0]; } return result; } } } Another C# class creates the GUI, which I think, is unnecessary to put in here. Any suggestions as to what I might be doing wrong in the C# application, would be greatly appreciated. Thanks, Suma H S

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Owning a computer without programming is like having a kitchen and using only the microwave oven” - Charles Petzold