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 / C++ / MFC
  4. Send data bits through USB

Send data bits through USB

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++hardware
8 Posts 4 Posters 2 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.
  • M Offline
    M Offline
    mythlace
    wrote on last edited by
    #1

    How can I send data bits through USB port? I am familiar with the parallel and serial port and have done some hardware controlling project through parallel port. But how do I send data through USB port using Visual C++?

    C B 2 Replies Last reply
    0
    • M mythlace

      How can I send data bits through USB port? I am familiar with the parallel and serial port and have done some hardware controlling project through parallel port. But how do I send data through USB port using Visual C++?

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      Accessing a USB port is not the same as accessing the serial port: you can't simply "open" it and send data on it. This is because multiple devices could be connected to the same port (see for instance an USB hub). So, what you have to do instead is retrieve the handle to the device driver associated with the device you want to communicate with (of course, the device should be installed on your computer before being able to do so). To retrieve the handle to the driver, it is similar as opening the serial port: you call CreateFile with the driver symbolic name for the file name argument. Once this is done, you can start sending and receiving data using the WriteFile and ReadFile functions (like for a serial port). Not that the driver will implement a specific behavior for these functions, so it really depends on your driver. Finally, you can also send driver specific control code by using the DeviceIOControl function. All these things are very specific to the device you want to access (so, to the driver you are accessing). Which kind of USB device are you trying to access ? There should be some documentation with the driver that explains you how to retrieve the handle to the driver and list the device control codes. If you are trying to access a USB-to-serial converter, things are different because in that case, the driver will emulate a virtual COM port and you can simply use this virtual COM port as a standard COM port.

      Cédric Moonen Software developer
      Charting control [v2.0] OpenGL game tutorial in C++

      _ M 2 Replies Last reply
      0
      • C Cedric Moonen

        Accessing a USB port is not the same as accessing the serial port: you can't simply "open" it and send data on it. This is because multiple devices could be connected to the same port (see for instance an USB hub). So, what you have to do instead is retrieve the handle to the device driver associated with the device you want to communicate with (of course, the device should be installed on your computer before being able to do so). To retrieve the handle to the driver, it is similar as opening the serial port: you call CreateFile with the driver symbolic name for the file name argument. Once this is done, you can start sending and receiving data using the WriteFile and ReadFile functions (like for a serial port). Not that the driver will implement a specific behavior for these functions, so it really depends on your driver. Finally, you can also send driver specific control code by using the DeviceIOControl function. All these things are very specific to the device you want to access (so, to the driver you are accessing). Which kind of USB device are you trying to access ? There should be some documentation with the driver that explains you how to retrieve the handle to the driver and list the device control codes. If you are trying to access a USB-to-serial converter, things are different because in that case, the driver will emulate a virtual COM port and you can simply use this virtual COM port as a standard COM port.

        Cédric Moonen Software developer
        Charting control [v2.0] OpenGL game tutorial in C++

        _ Offline
        _ Offline
        _Superman_
        wrote on last edited by
        #3

        Adding to the reply... The communication finally boils down to the type of endpoint. A USB device has the following hierarchy. Device -> Configuration -> Interface -> Endpoint. Endpoints can be of type Interrupt, Bulk or Isochronous. If you want to use a Virtual COM port, it will be different for different endpoints. Interrupt is the easiest to communicate with as there are drivers built-in. By the way to identify the USB device there is something called a Vendor Id (VID), Product Id (PID) and a serial number.

        «_Superman_» I love work. It gives me something to do between weekends.

        1 Reply Last reply
        0
        • C Cedric Moonen

          Accessing a USB port is not the same as accessing the serial port: you can't simply "open" it and send data on it. This is because multiple devices could be connected to the same port (see for instance an USB hub). So, what you have to do instead is retrieve the handle to the device driver associated with the device you want to communicate with (of course, the device should be installed on your computer before being able to do so). To retrieve the handle to the driver, it is similar as opening the serial port: you call CreateFile with the driver symbolic name for the file name argument. Once this is done, you can start sending and receiving data using the WriteFile and ReadFile functions (like for a serial port). Not that the driver will implement a specific behavior for these functions, so it really depends on your driver. Finally, you can also send driver specific control code by using the DeviceIOControl function. All these things are very specific to the device you want to access (so, to the driver you are accessing). Which kind of USB device are you trying to access ? There should be some documentation with the driver that explains you how to retrieve the handle to the driver and list the device control codes. If you are trying to access a USB-to-serial converter, things are different because in that case, the driver will emulate a virtual COM port and you can simply use this virtual COM port as a standard COM port.

          Cédric Moonen Software developer
          Charting control [v2.0] OpenGL game tutorial in C++

          M Offline
          M Offline
          mythlace
          wrote on last edited by
          #4

          Thanks for you reply.. Can u please send me a sample program for this... I am trying to send bits through USB and control the circuit directly... Actually I am doing it for Robotics, I have used Micro Controller for this but now i want to use computers to access them directly. Thanks

          C 1 Reply Last reply
          0
          • M mythlace

            Thanks for you reply.. Can u please send me a sample program for this... I am trying to send bits through USB and control the circuit directly... Actually I am doing it for Robotics, I have used Micro Controller for this but now i want to use computers to access them directly. Thanks

            C Offline
            C Offline
            Cedric Moonen
            wrote on last edited by
            #5

            dev_programer wrote:

            Can u please send me a sample program for this...

            Humm... Did you read my reply ? This is simply impossible because I don't even which kind of device you are trying to access. Didn't you receive any documentation with the USB device you are trying to achieve ? As I said in my previous reply, the driver symbolic name depends on the driver, the control codes depend on the driver also,... So, you should have receive some kind of documentation with the driver (specially if it is some kind of USB development kit).

            Cédric Moonen Software developer
            Charting control [v2.0] OpenGL game tutorial in C++

            M 1 Reply Last reply
            0
            • C Cedric Moonen

              dev_programer wrote:

              Can u please send me a sample program for this...

              Humm... Did you read my reply ? This is simply impossible because I don't even which kind of device you are trying to access. Didn't you receive any documentation with the USB device you are trying to achieve ? As I said in my previous reply, the driver symbolic name depends on the driver, the control codes depend on the driver also,... So, you should have receive some kind of documentation with the driver (specially if it is some kind of USB development kit).

              Cédric Moonen Software developer
              Charting control [v2.0] OpenGL game tutorial in C++

              M Offline
              M Offline
              mythlace
              wrote on last edited by
              #6

              Actually its not any specific device that i am trying to access. I develop my own robots and circuits and control them using a series of 0's and 1's. Lets say to active a motor first i need to send 1 through parallel port to the circuit which opens the gate and the motor driver circuit is turned on and thus motor begins to operate. How do i do this using USB?

              C 1 Reply Last reply
              0
              • M mythlace

                Actually its not any specific device that i am trying to access. I develop my own robots and circuits and control them using a series of 0's and 1's. Lets say to active a motor first i need to send 1 through parallel port to the circuit which opens the gate and the motor driver circuit is turned on and thus motor begins to operate. How do i do this using USB?

                C Offline
                C Offline
                Cedric Moonen
                wrote on last edited by
                #7

                You need to have at least something on your robot that has a USB port (some kind of micro-controller with a USB port). You need to also to have a driver installed on the computer that is able to communicate with this device. So, I really don't understand what you are trying to achieve: do oyu have some kind of micro-controller that has a USB port on your robot ? If no, I don't see how you are going to communicate with your robot over USB if your robot doesn't even have a USB port :~ So, once you have your USB device (a micro-controller with a USB port for instance), this device should have been supplied with a driver (and documentation). The driver has to be installed on your computer and you have to access the driver if you want to send information to your robot. That's how it works, you can't open the USB port directly, you need to have a driver installed on your computer which takes care of the communication with your USB device.

                Cédric Moonen Software developer
                Charting control [v2.0] OpenGL game tutorial in C++

                1 Reply Last reply
                0
                • M mythlace

                  How can I send data bits through USB port? I am familiar with the parallel and serial port and have done some hardware controlling project through parallel port. But how do I send data through USB port using Visual C++?

                  B Offline
                  B Offline
                  bolivar123
                  wrote on last edited by
                  #8

                  They make USB to parallel adapters. I suggest this as you say you have previously done your robotic control via a parallel port. There's a chance that your existing code that works through a real parallel port might work with one of these.

                  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