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 / C++ / MFC
  4. Reading from and Writing to a USB device using ReadFile() and WriteFile()

Reading from and Writing to a USB device using ReadFile() and WriteFile()

Scheduled Pinned Locked Moved C / C++ / MFC
hardwarehelp
7 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.
  • K Offline
    K Offline
    koumodaki
    wrote on last edited by
    #1

    Hi, I am working on a hardware controller device which has a USB port I need to use to write commands to the controller. I have been able to ennumerate the device and obtain the handle for that device using CreateFile(). But when I try to execute ReadFile() or WriteFile(), the function fails and GetLastError() returns ERROR_INVALID_FUNCTION. My code looks like this: HANDLE hUsbDevice = CreateFile(path.c_str(),GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if (hUsbDevice == INVALID_HANDLE_VALUE) { val = GetLastError(); cout<<"failed in CreateFile"<<" error: "<<val<<"\n" ; } else { char _send_buff[] = {'I','n','i','t','1','=','0','\0'}; int _send_size = sizeof(_send_buff); int _wrstatus = 0; DWORD _bytes_written = 0; _wrstatus = WriteFile(hUsbDevice,_buff,_size,&_bytes_written,NULL); if (!_wrstatus) cout<<"\nError while writing port: "<<GetLastError()<<endl; } Where am I going wrong here :confused:

    C I H 3 Replies Last reply
    0
    • K koumodaki

      Hi, I am working on a hardware controller device which has a USB port I need to use to write commands to the controller. I have been able to ennumerate the device and obtain the handle for that device using CreateFile(). But when I try to execute ReadFile() or WriteFile(), the function fails and GetLastError() returns ERROR_INVALID_FUNCTION. My code looks like this: HANDLE hUsbDevice = CreateFile(path.c_str(),GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if (hUsbDevice == INVALID_HANDLE_VALUE) { val = GetLastError(); cout<<"failed in CreateFile"<<" error: "<<val<<"\n" ; } else { char _send_buff[] = {'I','n','i','t','1','=','0','\0'}; int _send_size = sizeof(_send_buff); int _wrstatus = 0; DWORD _bytes_written = 0; _wrstatus = WriteFile(hUsbDevice,_buff,_size,&_bytes_written,NULL); if (!_wrstatus) cout<<"\nError while writing port: "<<GetLastError()<<endl; } Where am I going wrong here :confused:

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

      You know that USB is totally different than a serial port on this level ? You can't just open the 'port' and send data on it. Instead you get a handle from a device driver and you communicate with this driver. The driver is then in charge of the communication with the device. Did you receive any kind of information with the USB device ?

      Cédric Moonen Software developer
      Charting control [v1.4]

      K 1 Reply Last reply
      0
      • K koumodaki

        Hi, I am working on a hardware controller device which has a USB port I need to use to write commands to the controller. I have been able to ennumerate the device and obtain the handle for that device using CreateFile(). But when I try to execute ReadFile() or WriteFile(), the function fails and GetLastError() returns ERROR_INVALID_FUNCTION. My code looks like this: HANDLE hUsbDevice = CreateFile(path.c_str(),GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if (hUsbDevice == INVALID_HANDLE_VALUE) { val = GetLastError(); cout<<"failed in CreateFile"<<" error: "<<val<<"\n" ; } else { char _send_buff[] = {'I','n','i','t','1','=','0','\0'}; int _send_size = sizeof(_send_buff); int _wrstatus = 0; DWORD _bytes_written = 0; _wrstatus = WriteFile(hUsbDevice,_buff,_size,&_bytes_written,NULL); if (!_wrstatus) cout<<"\nError while writing port: "<<GetLastError()<<endl; } Where am I going wrong here :confused:

        I Offline
        I Offline
        Iain Clarke Warrior Programmer
        wrote on last edited by
        #3

        Is it me, or are there a lot of "How do I talk to this USB device I have incomplete documentation for" questions lately? A device driver does not actually have to provide an implentation of read or write. If it does not, then the user mode equivalents will fail too. eg:

        // Create dispatch points for create/open, close, unload. 
        DriverObject->MajorFunction\[IRP\_MJ\_CREATE\] = ImcNullDispatch; 
        DriverObject->MajorFunction\[IRP\_MJ\_CLOSE\] = ImcNullDispatch;
        DriverObject->MajorFunction\[IRP\_MJ\_DEVICE\_CONTROL\] = ImcIoCtl;
        DriverObject->DriverUnload = ImcUnload;
        

        That code was taken from a driver I wrote ages ago (function names change to protect the guilty). Not there is no

        DriverObject->MajorFunction\[IRP\_MJ\_READ\] = ... 
        

        In which case, I bet it has a huge DeviceIOControl function. So you'll have to go digging up information from the website of the controller device. Iain.

        Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.

        1 Reply Last reply
        0
        • C Cedric Moonen

          You know that USB is totally different than a serial port on this level ? You can't just open the 'port' and send data on it. Instead you get a handle from a device driver and you communicate with this driver. The driver is then in charge of the communication with the device. Did you receive any kind of information with the USB device ?

          Cédric Moonen Software developer
          Charting control [v1.4]

          K Offline
          K Offline
          koumodaki
          wrote on last edited by
          #4

          I do have some manuals but they do not say much. This USB port is an auxillary port of the controller. Normally it is not used. But I do know the USB drivers. Could you please tell me the procedure to get the driver handle? Is there any article I can refer to?

          C 1 Reply Last reply
          0
          • K koumodaki

            I do have some manuals but they do not say much. This USB port is an auxillary port of the controller. Normally it is not used. But I do know the USB drivers. Could you please tell me the procedure to get the driver handle? Is there any article I can refer to?

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

            koumodaki wrote:

            Could you please tell me the procedure to get the driver handle?

            You have to use CreateFile but you need to supply the driver symbolic name. The only way to know it is to look through the documentation or ask the guys. Is there anything related to DeviceIOControl in your doc ? There's no way we can help you here if you need to use DeviceIOControl because the codes you need to use are specific to the supplier.

            Cédric Moonen Software developer
            Charting control [v1.4]

            K 1 Reply Last reply
            0
            • C Cedric Moonen

              koumodaki wrote:

              Could you please tell me the procedure to get the driver handle?

              You have to use CreateFile but you need to supply the driver symbolic name. The only way to know it is to look through the documentation or ask the guys. Is there anything related to DeviceIOControl in your doc ? There's no way we can help you here if you need to use DeviceIOControl because the codes you need to use are specific to the supplier.

              Cédric Moonen Software developer
              Charting control [v1.4]

              K Offline
              K Offline
              koumodaki
              wrote on last edited by
              #6

              I will have to look into the manual carefull now. Anyway thanks for the help. :)

              1 Reply Last reply
              0
              • K koumodaki

                Hi, I am working on a hardware controller device which has a USB port I need to use to write commands to the controller. I have been able to ennumerate the device and obtain the handle for that device using CreateFile(). But when I try to execute ReadFile() or WriteFile(), the function fails and GetLastError() returns ERROR_INVALID_FUNCTION. My code looks like this: HANDLE hUsbDevice = CreateFile(path.c_str(),GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if (hUsbDevice == INVALID_HANDLE_VALUE) { val = GetLastError(); cout<<"failed in CreateFile"<<" error: "<<val<<"\n" ; } else { char _send_buff[] = {'I','n','i','t','1','=','0','\0'}; int _send_size = sizeof(_send_buff); int _wrstatus = 0; DWORD _bytes_written = 0; _wrstatus = WriteFile(hUsbDevice,_buff,_size,&_bytes_written,NULL); if (!_wrstatus) cout<<"\nError while writing port: "<<GetLastError()<<endl; } Where am I going wrong here :confused:

                H Offline
                H Offline
                Hamid Taebi
                wrote on last edited by
                #7

                See here[^] if helpfuls.

                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