Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. How to operate USB port

How to operate USB port

Scheduled Pinned Locked Moved C#
csharptutorial
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wk_vigorous
    wrote on last edited by
    #1

    HI, guys I am interested in operating USB port in C# . But I don't konw how to do that. Anybody have some idea about it. Thank you vigorous

    L D E 3 Replies Last reply
    0
    • W wk_vigorous

      HI, guys I am interested in operating USB port in C# . But I don't konw how to do that. Anybody have some idea about it. Thank you vigorous

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      The .Net framework doesn't allow you to manipulate hardware. You would have to use C++ to accomplish what you want. Aaron Eldreth TheCollective4.com My Articles While much is too strange to be believed, Nothing is too strange to have happened. - T. Hardy

      E 1 Reply Last reply
      0
      • W wk_vigorous

        HI, guys I am interested in operating USB port in C# . But I don't konw how to do that. Anybody have some idea about it. Thank you vigorous

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        You'd have to be ALOT more specific about what you want to do. On top of that, how to do it will be vendor and device dependent because you'll be interacting with the driver for that device. Also, since all the NT kernel versions of Windows (NT, 2000, XP, 2003) abstract the hardware, you can't get to the hardware directly. You have to go through either the device driver for the device you want, or write your own device driver that exposes an interface to your managed code (C#). RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

        1 Reply Last reply
        0
        • L Lost User

          The .Net framework doesn't allow you to manipulate hardware. You would have to use C++ to accomplish what you want. Aaron Eldreth TheCollective4.com My Articles While much is too strange to be believed, Nothing is too strange to have happened. - T. Hardy

          E Offline
          E Offline
          eggie5
          wrote on last edited by
          #4

          You can do it with C#, I have working code right here... This is maddness, I can't believe you said that. /\ |_ E X E GG

          1 Reply Last reply
          0
          • W wk_vigorous

            HI, guys I am interested in operating USB port in C# . But I don't konw how to do that. Anybody have some idea about it. Thank you vigorous

            E Offline
            E Offline
            eggie5
            wrote on last edited by
            #5

            Elaborate please. /\ |_ E X E GG

            W 1 Reply Last reply
            0
            • E eggie5

              Elaborate please. /\ |_ E X E GG

              W Offline
              W Offline
              wk_vigorous
              wrote on last edited by
              #6

              In the future, I will need get data from a usb port connected with a bar scanner. The bar scanner will scan a bar graph and send them to the PC by usb. At the moment, I still not know more details about the scanner. So I just want to know some idea about operating usb port. For example, I know we can use MSCom control or API to control serial port. But how can I use usb port? If you can provide some code, I will feel very very grateful for your help. Thank you very much. vigorous

              E 1 Reply Last reply
              0
              • W wk_vigorous

                In the future, I will need get data from a usb port connected with a bar scanner. The bar scanner will scan a bar graph and send them to the PC by usb. At the moment, I still not know more details about the scanner. So I just want to know some idea about operating usb port. For example, I know we can use MSCom control or API to control serial port. But how can I use usb port? If you can provide some code, I will feel very very grateful for your help. Thank you very much. vigorous

                E Offline
                E Offline
                eggie5
                wrote on last edited by
                #7

                Alright, I think I have the idea... I know microsft already provides generic drivers for scanners, so you can use that instead of the one that comes with the scanner (which is probably proprietry and not documented). SO, you wont' have to write your own driver, thats good. Um, I suggest you maybe get a development kit, EZ-USB[^]. This will give you some visual results. although it's not necessary. These kits though, come with external memory, a UART and serial connector, a set of push buttons, and an LED readout to facilitate development and debugging of your software. SO, that will help alot in your learning process. Next you need to learn the fundamentals of USB, like writing to a bus, etc. It's much different then the serial port. So just wrap the usb functions in c++ and then you can simply call the methods in your c# app/gui. It's a little more complicated then that though.... I have some code if you want to see it. Actually, everything is in that kit. It's too hard to type all this with one hand. (my wrist is broken.) /\ |_ E X E GG

                E W 2 Replies Last reply
                0
                • E eggie5

                  Alright, I think I have the idea... I know microsft already provides generic drivers for scanners, so you can use that instead of the one that comes with the scanner (which is probably proprietry and not documented). SO, you wont' have to write your own driver, thats good. Um, I suggest you maybe get a development kit, EZ-USB[^]. This will give you some visual results. although it's not necessary. These kits though, come with external memory, a UART and serial connector, a set of push buttons, and an LED readout to facilitate development and debugging of your software. SO, that will help alot in your learning process. Next you need to learn the fundamentals of USB, like writing to a bus, etc. It's much different then the serial port. So just wrap the usb functions in c++ and then you can simply call the methods in your c# app/gui. It's a little more complicated then that though.... I have some code if you want to see it. Actually, everything is in that kit. It's too hard to type all this with one hand. (my wrist is broken.) /\ |_ E X E GG

                  E Offline
                  E Offline
                  eggie5
                  wrote on last edited by
                  #8

                  This complies into a nice .net library with 2 main methods (bulk read, write) that I can seamsly use in my C# app. Actually with no pinvoking.

                  // This is the main DLL file.

                  #include "stdafx.h"

                  #include "csusb.dll" //simple error handler
                  #include "ezusbsys.h" //comes with the cypress board
                  #include "usb100.h" //found in windows DDK; contains USB definitions for constants & structures.

                  using namespace System;
                  using namespace System::Text;
                  using namespace System::Runtime::InteropServices;

                  namespace EzUsb
                  {
                  [StructLayout(LayoutKind::Explicit, Size=18, CharSet=CharSet::Auto)]
                  public __gc class UsbDeviceDescriptor
                  {
                  public:
                  [FieldOffset(0)] System::Byte bLength;
                  [FieldOffset(1)] System::Byte bDescriptorType;
                  [FieldOffset(2)] System::UInt16 bcdUSB;
                  [FieldOffset(4)] System::Byte bDeviceClass;
                  [FieldOffset(5)] System::Byte bDeviceSubClass;
                  [FieldOffset(6)] System::Byte bDeviceProtocol;
                  [FieldOffset(7)] System::Byte bMaxPacketSize0;
                  [FieldOffset(8)] System::UInt16 idVendor;
                  [FieldOffset(10)] System::UInt16 idProduct;
                  [FieldOffset(12)] System::UInt16 bcdDevice;
                  [FieldOffset(14)] System::Byte iManufacturer;
                  [FieldOffset(15)] System::Byte iProduct;
                  [FieldOffset(16)] System::Byte iSerialNumber;
                  [FieldOffset(17)] System::Byte bNumConfigurations;
                  };

                  public \_\_gc class Usb
                  {
                  private:
                  	HANDLE \_hEzUsb;
                  
                  	void ReportError( String\* msg )
                  	{
                  		if( OnErrorMessage )
                  		{
                  			OnErrorMessage( this, new ErrorEventArgs( msg ) );
                  		}
                  	}
                  
                  	void CheckWin32Error()
                  	{
                  		int errCode = GetLastError(); 
                  		if( errCode != 0 )
                  		{
                  			char msg\[256\];
                  			FormatMessage( FORMAT\_MESSAGE\_FROM\_SYSTEM, NULL, errCode, 0, 
                  				msg, 256, NULL );
                  			ReportError( msg );
                  		}
                  	}
                  
                  public:
                  	static const int MaxBlkSize = 64;
                  
                  	\_\_delegate void ErrorMessage( Object\* sender, ErrorEventArgs\* e );
                  	\_\_event ErrorMessage\* OnErrorMessage;
                  
                  	Usb()
                  	{
                  		\_hEzUsb = INVALID\_HANDLE\_VALUE;
                  	}
                  
                  	~Usb()
                  	{
                  		if( \_hEzUsb != INVALID\_HANDLE\_VALUE )
                  		{
                  			CloseHandle( \_hEzUsb );
                  		}
                  	}
                  
                  	bool Open( String\* driverName )
                  	{
                  		char\* lpFileName = new char\[driverName->Length + 10\];
                  		sprintf( lpFileName, "\\\\\\\\.\\\\%s", driverName );
                  		\_hEzUsb = CreateFile( lpFileName,
                  								GENERIC\_WRITE,
                  								FILE\_SHARE\_WRITE,
                  								NULL,
                  								OPEN\_EXISTING,
                  								0,
                  								NULL);
                  		if( \_hEzUsb == INVALID\_HANDLE\_VALUE )
                  		{
                  			CheckWin32Error();
                  		}
                  		delete lpFileName;
                  
                  		return( \_hEzUsb != INVALID\_HANDLE\_VAL
                  
                  1 Reply Last reply
                  0
                  • E eggie5

                    Alright, I think I have the idea... I know microsft already provides generic drivers for scanners, so you can use that instead of the one that comes with the scanner (which is probably proprietry and not documented). SO, you wont' have to write your own driver, thats good. Um, I suggest you maybe get a development kit, EZ-USB[^]. This will give you some visual results. although it's not necessary. These kits though, come with external memory, a UART and serial connector, a set of push buttons, and an LED readout to facilitate development and debugging of your software. SO, that will help alot in your learning process. Next you need to learn the fundamentals of USB, like writing to a bus, etc. It's much different then the serial port. So just wrap the usb functions in c++ and then you can simply call the methods in your c# app/gui. It's a little more complicated then that though.... I have some code if you want to see it. Actually, everything is in that kit. It's too hard to type all this with one hand. (my wrist is broken.) /\ |_ E X E GG

                    W Offline
                    W Offline
                    wk_vigorous
                    wrote on last edited by
                    #9

                    Thank you very much. I am sure your words are very useful and helpful. And I am so sorry to hear about your wrist and hope you will be well quickly. Best regards vigorous

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups