• Not Answered

C# lmusbdll.dll

Hello,

is there any  lmusbdll.dll which I can use in Microsoft Visual C# to get a USB-connection?

Thanks

5 Replies

  • Hello Jens,

    The DLLs conform to the Microsoft coding standards.  We don't have anything specifically targeting C#, however, you should be able to import the dll using the standard C# methodology.  Here is an example where someone used a different DLL, but using C#.

    http://e2e.ti.com/support/microcontrollers/stellaris_arm_cortex-m3_microcontroller/f/473/t/103202.aspx

    More information about using dlls written using C/C++ and importing to C# can be found within the Microsoft forums.

    Regards,

    Craig

  • In reply to Craig Giglio94760:

    Please help:

    I am using the Stellaris LaunchPad (LM4F120) to prototype an embedded USB device.

    It would be SO great if someone who knows what they are doing could make a C# (CSHARP) version of the usb_bulk_example for the PC side of the code.

    C# is pretty much the language of choice for PC applications these days.

     

    I am using Visual Studio Express 2012 and C# for the PC side.

    There are bits and pieces of solutions on this forum:

    "Csharp Interfacing to LMDFU.DLL" - http://e2e.ti.com/support/microcontrollers/stellaris_arm_cortex-m3_microcontroller/f/473/t/103202.aspx

    "C#  lmusbdll.dll" http://e2e.ti.com/support/microcontrollers/stellaris_arm_cortex-m3_microcontroller/f/473/t/180076.aspx

     but nothing really seems to work.

     THANKS !

     -------------  MORE DETAIL ------------------

    I tried the code (listed below) from the second link above (using char, then byte buffers).

    I cant figure out how to resolve the following run-time debugger error

    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."

     A similar error is generated for call to all four lmusbdll functions:  InitializeDevice, ReadUSBPacket, WriteUSBPacket, and TerminateDevice.

    ---------------  C# CODE -----------------

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    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] byte[] 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] byte[] 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;

                byte[] szBuffer = new byte[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("{6E45736A-2B1B-4078-B772-B3AF2B6FDE1C}");           

               

                // 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] = 3; // Convert.ToByte("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);

            }

        }

    }

     

     

  • In reply to Craig Giglio94760:

    Hello Craig,

    I´m trying make an software to comunicate with my own board (Stellaris LM3S5632). The firmware looks ok, because I can see the device in Windows (Stellaris Bulk Devices -> Generic Bulk Device). I´m using Embarcadero Delphi XE3 to make the software. To use the dll in Delphi, I changed the function variables. The original function from lmusbdll.h is:

    LMUSB_HANDLE __stdcall InitializeDevice(unsigned short usVID,
                                            unsigned short usPID,
                                            LPGUID lpGUID,
                                            BOOL *pbDriverInstalled);

    I changed to this (without making any modification on .dll file):

    function InitializeDevice(usVID:DWORD; usPID:DWORD; lpGUID:TGUID; pbDriverInstalled:Boolean):THANDLE; stdcall; external  'lmusbdll.dll' index 1;

    The variables are:
      LMUSB_HANDLE: THANDLE;
      MyGuid: TGUID = '{6E45736A-2B1B-4078-B772-B3AF2B6FDE1C}';
      BULK_VID:DWORD= $1cbe;
      BULK_PID:DWORD= $0003

    To test, I put in a button the code:
    LMUSB_HANDLE:=InitializeDevice(BULK_VID,BULK_PID,MyGuid,true);

    When I click in the button, using the lmusbdll.dll, I get the error "Access violation at address 1001EAD5 in module 'lmusbdll.dll'. Write of address 40782B1B."

    When I use the lmusbdll64.dll, the application didn´t start.

    Could help me with this? Is a lmusbdll.dll variable convertion or value error?

    My System:
    WIndows 7 Professional SP1 64bits
    lmusbdll.dll version: 1.0.0.9107
    lmusbdll64.dll version: 1.0.0.9107

    Best Regards,

    Anderson

  • In reply to Anderson Castellar:

    Solved

    https://forums.embarcadero.com/thread.jspa?messageID=527005#527005

  • In reply to Anderson Castellar:

    I realize this is now an old post, but I was wondering if anyone has a ready solution.


    The above posted link is dead and I was unable to find this topic on the Embarcadero web site. I would think that a C# interface to the lmusbdll would be quite useful.


    I'd rather not reinvent the wheel if someone has already solved this.


    Thank you,

    Kevin