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
  1. Home
  2. General Programming
  3. C#
  4. Communicating with an USB Device

Communicating with an USB Device

Scheduled Pinned Locked Moved C#
tutorialquestion
21 Posts 3 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.
  • H Offline
    H Offline
    haolan
    wrote on last edited by
    #1

    Hi there. I have an USB Device whick have to small functions. It should be possible if I write 'GET' to it, it should then return a integer value. And if I write 'RES' it should reset the Integer value. The Device is connected to my Computer with a USB-Serial cable. I have installed a driver i got with the cable. I have tried using LibUsbDotNet, but when i tries to open a connection to the device the program just crashes and i cant do anything. Anyone who can tell how to communicate with my device?

    D Mircea PuiuM 2 Replies Last reply
    0
    • H haolan

      Hi there. I have an USB Device whick have to small functions. It should be possible if I write 'GET' to it, it should then return a integer value. And if I write 'RES' it should reset the Integer value. The Device is connected to my Computer with a USB-Serial cable. I have installed a driver i got with the cable. I have tried using LibUsbDotNet, but when i tries to open a connection to the device the program just crashes and i cant do anything. Anyone who can tell how to communicate with my device?

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

      You said it was connected using a USB Serial cable?? Is the device exposed on a COM port?? Like COM1, COM2, Com3, ... Try talking to it over the serial port instead of going through the headache of using the devices' drivers.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007

      H 1 Reply Last reply
      0
      • H haolan

        Hi there. I have an USB Device whick have to small functions. It should be possible if I write 'GET' to it, it should then return a integer value. And if I write 'RES' it should reset the Integer value. The Device is connected to my Computer with a USB-Serial cable. I have installed a driver i got with the cable. I have tried using LibUsbDotNet, but when i tries to open a connection to the device the program just crashes and i cant do anything. Anyone who can tell how to communicate with my device?

        Mircea PuiuM Offline
        Mircea PuiuM Offline
        Mircea Puiu
        wrote on last edited by
        #3

        Would a C# version of some DDK's USBView utility be of any help? Get the code and some interesting ideas[^].

        SkyWalker

        1 Reply Last reply
        0
        • D Dave Kreskowiak

          You said it was connected using a USB Serial cable?? Is the device exposed on a COM port?? Like COM1, COM2, Com3, ... Try talking to it over the serial port instead of going through the headache of using the devices' drivers.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007

          H Offline
          H Offline
          haolan
          wrote on last edited by
          #4

          The device has a serial port, so i have connected it to an USB port using a USB-Serial cable.

          D 1 Reply Last reply
          0
          • H haolan

            The device has a serial port, so i have connected it to an USB port using a USB-Serial cable.

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

            Then the device is more-than-likely exposed on a normal COM port. You can probably get to it using any terminal emulator software that uses serial communication (like HyperTerminal). If so, then you can use the SerialPort class in the .NET Framework 2.0 (System.Io.Ports namespace) to talk to it from your code.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007

            H 2 Replies Last reply
            0
            • D Dave Kreskowiak

              Then the device is more-than-likely exposed on a normal COM port. You can probably get to it using any terminal emulator software that uses serial communication (like HyperTerminal). If so, then you can use the SerialPort class in the .NET Framework 2.0 (System.Io.Ports namespace) to talk to it from your code.

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007

              H Offline
              H Offline
              haolan
              wrote on last edited by
              #6

              Ok. Sounds great.. thanks :) I will buy a serial cable tomorrow and try it..

              H D 2 Replies Last reply
              0
              • H haolan

                Ok. Sounds great.. thanks :) I will buy a serial cable tomorrow and try it..

                H Offline
                H Offline
                haolan
                wrote on last edited by
                #7

                Could this be a possible way to call the GET function on the device? SerialPort sp = new SerialPort("COM1"); sp.Write("GET"); char[] buffer = new char[64]; int ans = sp.Read(buffer, 0, 64);

                D 1 Reply Last reply
                0
                • H haolan

                  Ok. Sounds great.. thanks :) I will buy a serial cable tomorrow and try it..

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

                  You just told me you HAD a serial cable and had the device connected to it...??

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                       2006, 2007

                  H 1 Reply Last reply
                  0
                  • H haolan

                    Could this be a possible way to call the GET function on the device? SerialPort sp = new SerialPort("COM1"); sp.Write("GET"); char[] buffer = new char[64]; int ans = sp.Read(buffer, 0, 64);

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

                    In a very, very "quick'n'dirty" implementation, no. You still have to give the SerialPort class constructor the parameters the device is expecting for serial communication, like which COM port (you already have it), baud rate, parity, data bits, and stop bits. Then you need to Open the port before you try to read/write from it. Then you need to Close the port when you're done. Read this...[^]

                    A guide to posting questions on CodeProject[^]
                    Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                         2006, 2007

                    1 Reply Last reply
                    0
                    • D Dave Kreskowiak

                      Then the device is more-than-likely exposed on a normal COM port. You can probably get to it using any terminal emulator software that uses serial communication (like HyperTerminal). If so, then you can use the SerialPort class in the .NET Framework 2.0 (System.Io.Ports namespace) to talk to it from your code.

                      A guide to posting questions on CodeProject[^]
                      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                           2006, 2007

                      H Offline
                      H Offline
                      haolan
                      wrote on last edited by
                      #10

                      Hi again.. I found out that the Computer the device has to be connected to, does not contain any serial port.. So I have to use USB to this. Is it really that hard to communicate with an USB port?

                      D 1 Reply Last reply
                      0
                      • D Dave Kreskowiak

                        You just told me you HAD a serial cable and had the device connected to it...??

                        A guide to posting questions on CodeProject[^]
                        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                             2006, 2007

                        H Offline
                        H Offline
                        haolan
                        wrote on last edited by
                        #11

                        I told you I had a USB-Serial cable connected to it.. It is an adapter from Serial to USB.. The port on the device is Serial and on the computer it has to be USB.

                        1 Reply Last reply
                        0
                        • H haolan

                          Hi again.. I found out that the Computer the device has to be connected to, does not contain any serial port.. So I have to use USB to this. Is it really that hard to communicate with an USB port?

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

                          haolan wrote:

                          Is it really that hard to communicate with an USB port?

                          You don't talk to a USB port. USB is not really a port. What does USB stand for?? "Universial Serial BUS". It works more like the slots inside the computer, not like the ports on the back of it. Yes, it's THAT difficult. You don't take to the bus itself. You talk to each device using whatever methods that device exposes. That could be through drivers, Device I/O calls, normal file system functions, or, in your case, through a COM port.

                          A guide to posting questions on CodeProject[^]
                          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                               2006, 2007

                          H 1 Reply Last reply
                          0
                          • D Dave Kreskowiak

                            haolan wrote:

                            Is it really that hard to communicate with an USB port?

                            You don't talk to a USB port. USB is not really a port. What does USB stand for?? "Universial Serial BUS". It works more like the slots inside the computer, not like the ports on the back of it. Yes, it's THAT difficult. You don't take to the bus itself. You talk to each device using whatever methods that device exposes. That could be through drivers, Device I/O calls, normal file system functions, or, in your case, through a COM port.

                            A guide to posting questions on CodeProject[^]
                            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                 2006, 2007

                            H Offline
                            H Offline
                            haolan
                            wrote on last edited by
                            #13

                            Ahh ok.. I connected the device again, and found out that if I run: string[] ports = SerialPort.GetPortNames(); foreach (string p in ports) { Console.WriteLine(p); } It has created a new COM port named COM6. So i guess i should be able to communicate with my device through that port? I have created the following code for communication: SerialPort sp = new SerialPort("COM6", 1200, Parity.None, 8, StopBits.One); try { sp.Open(); sp.Write("GET"); sp.ReadTimeout = 10000; string ans = sp.ReadLine(); } catch (Exception e) { Console.WriteLine(e.Message); } finally { sp.Close(); } I get timeout when I am waiting for answer, but I guess it could be a mistake of how to communicate with the device.

                            D 1 Reply Last reply
                            0
                            • H haolan

                              Ahh ok.. I connected the device again, and found out that if I run: string[] ports = SerialPort.GetPortNames(); foreach (string p in ports) { Console.WriteLine(p); } It has created a new COM port named COM6. So i guess i should be able to communicate with my device through that port? I have created the following code for communication: SerialPort sp = new SerialPort("COM6", 1200, Parity.None, 8, StopBits.One); try { sp.Open(); sp.Write("GET"); sp.ReadTimeout = 10000; string ans = sp.ReadLine(); } catch (Exception e) { Console.WriteLine(e.Message); } finally { sp.Close(); } I get timeout when I am waiting for answer, but I guess it could be a mistake of how to communicate with the device.

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

                              haolan wrote:

                              It has created a new COM port named COM6. So i guess i should be able to communicate with my device through that port?

                              Probably. Noone except you can relly tell that. Open up HyperTerminal, set the port to COM6, set the parameters to what the device expects, connect, type in your commands and see what happens.

                              haolan wrote:

                              SerialPort sp = new SerialPort("COM6", 1200, Parity.None, 8, StopBits.One);

                              If you don't get a response, or you get a response containing garbage, then you've most likely either got the wrong COM port, or got the communication parameters wrong.

                              A guide to posting questions on CodeProject[^]
                              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                   2006, 2007

                              H 1 Reply Last reply
                              0
                              • D Dave Kreskowiak

                                haolan wrote:

                                It has created a new COM port named COM6. So i guess i should be able to communicate with my device through that port?

                                Probably. Noone except you can relly tell that. Open up HyperTerminal, set the port to COM6, set the parameters to what the device expects, connect, type in your commands and see what happens.

                                haolan wrote:

                                SerialPort sp = new SerialPort("COM6", 1200, Parity.None, 8, StopBits.One);

                                If you don't get a response, or you get a response containing garbage, then you've most likely either got the wrong COM port, or got the communication parameters wrong.

                                A guide to posting questions on CodeProject[^]
                                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                     2006, 2007

                                H Offline
                                H Offline
                                haolan
                                wrote on last edited by
                                #15

                                The communication parameters should be ok. My laptop is a bit weird though.. It has a lot of COM ports (from 3 to 16).. When i connect my device it has two COM3 ports, I hope that it wont make any trouple with my communication.. But how do I use HyperTerminal for such a thing? When I open HyperTermial it seems that it only can communicate with Modems??

                                D 1 Reply Last reply
                                0
                                • H haolan

                                  The communication parameters should be ok. My laptop is a bit weird though.. It has a lot of COM ports (from 3 to 16).. When i connect my device it has two COM3 ports, I hope that it wont make any trouple with my communication.. But how do I use HyperTerminal for such a thing? When I open HyperTermial it seems that it only can communicate with Modems??

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

                                  haolan wrote:

                                  When i connect my device it has two COM3 ports,

                                  Yeah, that's a problem. You can't have multiple ports having the same COM number.

                                  haolan wrote:

                                  It has a lot of COM ports (from 3 to 16)..

                                  Why so many?? I hope this isn't normal for your machine...

                                  haolan wrote:

                                  When I open HyperTermial it seems that it only can communicate with Modems??

                                  HyperTerminal talks to any serial device, not just Modems. BTW, a modem is just another serial device. All you do is launch HyperTerminal, give the session a name, pick the COM port you want to use, give it the communication parameters, and go...

                                  A guide to posting questions on CodeProject[^]
                                  Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                       2006, 2007

                                  H 1 Reply Last reply
                                  0
                                  • D Dave Kreskowiak

                                    haolan wrote:

                                    When i connect my device it has two COM3 ports,

                                    Yeah, that's a problem. You can't have multiple ports having the same COM number.

                                    haolan wrote:

                                    It has a lot of COM ports (from 3 to 16)..

                                    Why so many?? I hope this isn't normal for your machine...

                                    haolan wrote:

                                    When I open HyperTermial it seems that it only can communicate with Modems??

                                    HyperTerminal talks to any serial device, not just Modems. BTW, a modem is just another serial device. All you do is launch HyperTerminal, give the session a name, pick the COM port you want to use, give it the communication parameters, and go...

                                    A guide to posting questions on CodeProject[^]
                                    Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                         2006, 2007

                                    H Offline
                                    H Offline
                                    haolan
                                    wrote on last edited by
                                    #17

                                    I do not know why there are so many ports.. It doesn't have any physical comports. But nevermind.. I got my program to work.. The only problem was that the baudrate was only 1200, so I had to wait a little time after I have wrote the GET method to the device before trying to read from it. I have tried in a windows application to add a timer, but that doesn't seem to work, but if I place a for (int i; i < 100000; i++) between the read and write, it probably works fine. I really cant figure out what the difference is. I gotta find out, because the program can be run at different CPU's. Maybe you have a solution? Another thing that I am worried about is the COM Port name. It is different for each computer I connect it to.. Is there any way to find out which COM port my device is connected to?

                                    D 1 Reply Last reply
                                    0
                                    • H haolan

                                      I do not know why there are so many ports.. It doesn't have any physical comports. But nevermind.. I got my program to work.. The only problem was that the baudrate was only 1200, so I had to wait a little time after I have wrote the GET method to the device before trying to read from it. I have tried in a windows application to add a timer, but that doesn't seem to work, but if I place a for (int i; i < 100000; i++) between the read and write, it probably works fine. I really cant figure out what the difference is. I gotta find out, because the program can be run at different CPU's. Maybe you have a solution? Another thing that I am worried about is the COM Port name. It is different for each computer I connect it to.. Is there any way to find out which COM port my device is connected to?

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

                                      You can't simply put a Write, then a Read, then a Write for the next command, then a Read, ..., in order an expect it to work. Your design is going to have to be much more flexible, like writing a method to send a command, another method to setup an asynchronous read of the port, another method to handle timeouts. A timer is completely useless to you. <blockquote class="FQ"><div class="FQA">haolan wrote:</div>Another thing that I am worried about is the COM Port name. It is different for each computer I connect it to.. Is there any way to find out which COM port my device is connected to? </blockquote> The only way to know is to go through each COM port on the machine and issue some command to it that the device understands and responds to. If the response in what you expect, you found the device. There is no other way to do it.

                                      A guide to posting questions on CodeProject[^]
                                      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                           2006, 2007

                                      H 1 Reply Last reply
                                      0
                                      • D Dave Kreskowiak

                                        You can't simply put a Write, then a Read, then a Write for the next command, then a Read, ..., in order an expect it to work. Your design is going to have to be much more flexible, like writing a method to send a command, another method to setup an asynchronous read of the port, another method to handle timeouts. A timer is completely useless to you. <blockquote class="FQ"><div class="FQA">haolan wrote:</div>Another thing that I am worried about is the COM Port name. It is different for each computer I connect it to.. Is there any way to find out which COM port my device is connected to? </blockquote> The only way to know is to go through each COM port on the machine and issue some command to it that the device understands and responds to. If the response in what you expect, you found the device. There is no other way to do it.

                                        A guide to posting questions on CodeProject[^]
                                        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                             2006, 2007

                                        H Offline
                                        H Offline
                                        haolan
                                        wrote on last edited by
                                        #19

                                        Ok.. So translated into code you are saying that I should do something like this when i read: while(true) { DoRead(); if(TimeOutException) ReadAgain; else { if(bytesToRead > 10) ReadAgain; else break; } } I know this isn't a real code, just a little theory.. The 10 bytes to read is because I know that the valid data always will be 10 characters long. Is this correct understood? By the way.. I really appreciate your patience in this thread ;) Sorry if my english seems a little bad sometimes, I am not originally english and it has been a while since I last got lections in it. :)

                                        D 1 Reply Last reply
                                        0
                                        • H haolan

                                          Ok.. So translated into code you are saying that I should do something like this when i read: while(true) { DoRead(); if(TimeOutException) ReadAgain; else { if(bytesToRead > 10) ReadAgain; else break; } } I know this isn't a real code, just a little theory.. The 10 bytes to read is because I know that the valid data always will be 10 characters long. Is this correct understood? By the way.. I really appreciate your patience in this thread ;) Sorry if my english seems a little bad sometimes, I am not originally english and it has been a while since I last got lections in it. :)

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

                                          haolan wrote:

                                          So translated into code you are saying that I should do something like this when i read:

                                          Kind of, though there are async read methods that do all of this work for you and call you when there is some data to read.

                                          haolan wrote:

                                          The 10 bytes to read is because I know that the valid data always will be 10 characters long.

                                          That's not how you do it. Even though your return data may be 10 bytes, you can still get it in several small chunks in multiple reads. Your code has to be able to reassemble the data as it comes in so it can provide a completed message back to the code that's expecting a 10 byte message.

                                          A guide to posting questions on CodeProject[^]
                                          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                               2006, 2007

                                          H 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