usb Bulk Transfer using lmusbdll.dll

Other Parts Discussed in Thread: TMS320F28379D, C2000WARE

I am working on USB Bulk Transfer "lmusbdll.dll" with C# Language in Visual Studio.

I import DLL as below:

[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);

I called Initialize function as below:

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("{6E45736A-2B1B-4078-B772-B3AF2B6FDE1C}");

// Init the USB device

hUSB = InitializeDevice(BULK_VID, BULK_PID,ref GUID_DEVINTERFACE_LUMINARY_BULK, ref bDriverInstalled);

But I am getting error as below:

Managed Debugging Assistant 'PInvokeStackImbalance'
Message=Managed Debugging Assistant 'PInvokeStackImbalance' : 'A call to PInvoke function 'WindowsFormsApp1!WindowsFormsApp1.Form1::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.'

Please help me or provide if any working code available.

Thanks in Advance!

  • Hello,

    what processor are you using?

    ~Leonard

  • Hi Sunil, 

    Looks like there is a mismatch between the way PInovke  expects and the way the function is declared in the C/C++ code and exported from the DLL.

    The default calling convention for P/Invoke is __stdcall, not __cdecl.  Can you try using  __stdcall  as the calling convention while importing the DLL? 

    If this does not work, please try using importing the "tiusbdll.dll" DLL located in C:\ti\c2000\C2000Ware_3_04_00_00\utilities\tools\f2837xd\tiusbdll\Release folder

    Best Regards

    Siddharth

  • Hi Siddharth....Thanks for response.....I also used CallingConvention.StdCall,,,but got same result as below...

    I write code for USB Bulk Transfer using "tiusbdll.dll" in C#. I take reference with USB_Bulk Transfer with TI. Some time code work well but some time Write & Read code not work....Application hang when i call write/read function...

    Once i plug out & again plug in USB. its application work again...this happen randomly.

    DLL Import 

    --------------------------------------------------------------------------------------------

    [DllImport("tiusbdll.dll", EntryPoint = "InitializeDevice", ExactSpelling = true, CallingConvention = CallingConvention.StdCall), SuppressUnmanagedCodeSecurity]
    public static extern IntPtr InitializeDevice(ushort usVID, ushort usPID, ref System.Guid lpGUID, ref bool pbDriverInstalled);

    //[DllImport("tiusbdll.dll", EntryPoint = "ReadUSBPacket", ExactSpelling = true, CallingConvention = CallingConvention.StdCall), SuppressUnmanagedCodeSecurity]
    [DllImport("tiusbdll.dll")]
    public static extern UInt32 ReadUSBPacket(IntPtr hHandle, [In] byte[] pcBuffer, UInt32 ulSize, ref UInt32 pulRead, UInt32 ulTimeoutMs, IntPtr hBreak);

    //[DllImport("tiusbdll.dll", EntryPoint = "WriteUSBPacket", ExactSpelling = true, CallingConvention = CallingConvention.StdCall), SuppressUnmanagedCodeSecurity]
    [DllImport("tiusbdll.dll")]

    public static extern bool WriteUSBPacket(IntPtr hHandle, [Out] byte[] pcBuffer, UInt32 ulSize, ref UInt32 pulWritten);

    --------------------------------------------------------------------------------------------

    Code of USB Intilization

    -------------------------------------------------------------------------------------------

    private void btnInitializeUSB_Click(object sender, EventArgs e)
    {
    ushort BULK_VID = 0x1cbe;
    ushort BULK_PID = 0x0003;
    bool bDriverInstalled = false;
    // Create a GUID, match to what is in INF file
    System.Guid GUID_DEVINTERFACE_LUMINARY_BULK;
    GUID_DEVINTERFACE_LUMINARY_BULK = new System.Guid("{6E45736A-2B1B-4078-B772-B3AF2B6FDE1C}");
    // Init the USB device
    hUSB = InitializeDevice(BULK_VID, BULK_PID, ref GUID_DEVINTERFACE_LUMINARY_BULK, ref bDriverInstalled);
    }

    -------------------------------------------------------------------------------------------

    Code for Read/Write

    private void btnSendData_Click(object sender, EventArgs e)
    {

    UInt32 dwError;
    byte[] szBuffer = new byte[256];
    byte[] szBufferRead = new byte[256];

    const UInt32 INFINITE = 0xFFFFFFFF;
    UInt32 ulRead = 1;
    UInt32 pulWritten = 0;
    IntPtr hBreak = IntPtr.Zero;

    // Load up array with useless "t"

    int x;

    for (x = 0; x < 3; x++)
    {
    szBuffer[x] = 3; // Convert.ToByte("t");
    }

    string strSendMessage = txtSendMessage.Text;

    szBuffer = Encoding.ASCII.GetBytes(txtSendMessage.Text);
    // do this transaction 10k times for the fun of it...

    int y;

    //for (y = 0; y < 10000; y++)

    //{

    bool ret = WriteUSBPacket(hUSB, szBuffer, Convert.ToUInt16(strSendMessage.Length), ref pulWritten);
    dwError = ReadUSBPacket(hUSB, szBufferRead, 256, ref ulRead, INFINITE, hBreak);
    string strRec = ASCIIEncoding.ASCII.GetString(szBufferRead);
    string strSend = ASCIIEncoding.ASCII.GetString(szBuffer);

    }

  • I also checked with USB_Bulk_Transfer with TI tool sample....this also have same case....Can u suggest

  • Sunil,

    Looks like you have created duplicate thread for the same issue. 

    https://e2e.ti.com/support/microcontrollers/c2000-microcontrollers-group/c2000/f/c2000-microcontrollers-forum/1020628/app-hang-with-usb-bulk-transfer-using-tiusbdll-dll

    Can you check if the InitializeDevice API returned successfully and you have a valid handle to the device? 

    Looks like it may returned with an error while connecting to the device.

    Best Regards

    Siddharth