• Join
  • Sign In with my.TI Login
Texas Instruments
  • Products
  • Applications
  • Tools & Software
  • Support & Community
  • Sample & Buy
  • About TI
Sample & Purchase Cart Sample & Purchase Cart
  • Search
  • Advanced
TI E2E™ Community
  • Support Forums
  • Blogs
  • Groups
  • Videos
  • 简体中文
  • More ...
TI Home » TI E2E Community » Support Forums » Microcontrollers » Stellaris® ARM® Microcontrollers » Stellaris® ARM® LM3S Microcontrollers Forum » csharp usb_bulk_example
Share
Stellaris® ARM® Microcontrollers
  • Forum
Options
  • Subscribe via RSS
Helpful Stellaris® LM4F Series Links
  • LM4F Series
  • Stellaris PinMux Utility
  • Stellaris® LM4F120 LaunchPad
  • LM4F MCU Applications
  • LM4F MCU Video
  • ARM Cortex-M4F Whitepaper
  • Stellaris MCU Brochure
  • LM4F232 Eval Kit
  • Forums

    csharp usb_bulk_example

    • kestephenson
      Posted by kestephenson
      on Apr 02 2010 07:59 AM
      Prodigy20 points
      Put together a simple program in C# to utilize the usb_bulk_example. Thought it might be useful for people trying to convert the C++ over. Just create a console application and dump this into it.

      Code:


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




      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • jayteemo
      Posted by jayteemo
      on Oct 22 2010 15:23 PM
      Intellectual490 points

      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.

       

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Denis
      Posted by Denis
      on Apr 03 2011 16:23 PM
      Intellectual465 points

      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.

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • kestephenson
      Posted by kestephenson
      on Apr 04 2011 06:38 AM
      Prodigy20 points

      No sorry I don't.

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • 3sRol
      Posted by 3sRol
      on Apr 08 2011 09:18 AM
      Intellectual430 points

      Does anyone have an idea what baud rate can be reached with this Read/Write function calls? 

      Grtz 3s

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Hans Peter Haastrup-Nielsen
      Posted by Hans Peter Haastrup-Nielsen
      on Jun 08 2011 07:50 AM
      Prodigy20 points

      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

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Denis
      Posted by Denis
      on Jun 08 2011 09:02 AM
      Intellectual465 points

      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

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Hans Peter Haastrup-Nielsen
      Posted by Hans Peter Haastrup-Nielsen
      on Jun 10 2011 05:03 AM
      Prodigy20 points

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

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Denis
      Posted by Denis
      on Jun 10 2011 09:30 AM
      Intellectual465 points

      Yes, that could definitely be the problem since call stacks would be different. GL

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Andreas M��ller
      Posted by Andreas M��ller
      on Apr 02 2012 04:09 AM
      Prodigy50 points

      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?

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Andreas M��ller
      Posted by Andreas M��ller
      on Apr 04 2012 00:49 AM
      Prodigy50 points

      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?

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    TI E2E™ Community
    • Support Forums
    • Blogs
    • Videos
    • Groups
    • Site Support & Feedback
    • Settings
    TI E2E™ Community Groups
    • TI University Program
    • Make the Switch
    • Microcontroller Projects
    • Motor Drive & Control
    Other Communities
    • Deyisupport
    • Designsomething.org
    • beagleboard.org
    • TI on Element 14
    • TI on TechXchangeSM
    Other Technical & Support Resources
    • WEBENCH® Design Center
    • Product Information Centers
    • Technical Documents
    • TI Design Network
    • TI Technical Articles
    • TI Training

    All content and materials on this site are provided "as is". TI and its respective suppliers and providers of content make no representations about the suitability of these materials for any purpose and disclaim all warranties and conditions with regard to these materials, including but not limited to all implied warranties and conditions of merchantability, fitness for a particular purpose, title and non-infringement of any third party intellectual property right. TI and its respective suppliers and providers of content make no representations about the suitability of these materials for any purpose and disclaim all warranties and conditions with respect to these materials. No license, either express or implied, by estoppel or otherwise, is granted by TI. Use of the information on this site may require a license from a third party, or a license from TI.

    Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms of Use of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms of Use of this site. TI, its suppliers and providers of content reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.

    Follow Us Texas Instruments on Facebook Texas Instruments on Twitter Texas Instruments on LinkedIn Texas Instruments on Google+
    TI Worldwide | Contact Us | my.TI Login | Site Map | Corporate Citizenship | mobile m.ti.com (Mobile Version)

    TI is a global semiconductor design and manufacturing company. Innovate with 100,000+ analog ICs and
    embedded processors, along with software, tools and the industry’s largest sales/support staff.

    © Copyright 1995-2013 Texas Instruments Incorporated. All rights reserved.
    Trademarks | Privacy Policy | Terms of Use