Read data from USB using C++ on Windows 7

The Big Cheese picture The Big Cheese · Jul 11, 2013 · Viewed 7.2k times · Source

I'm trying to read the output from a Trimble SPS-361 GPS receiver, which is connected to a Windows 7 machine via USB, using C++ (preferably) or Java. I know people have asked similar questions, but I can't seem to find what I'm looking for. It sounds like libusb is my best bet, is that true? If so, what is a good tutorial/guide/example code?

It sounds like I need to install/write some sort of device-specific driver and then I can read data off the device with C++ through libusb. Is that the general process for reading from a USB or am I confused?

To summarize what I think need (I know I'm all over the place):

  1. An explanation/tutorial of how to communicate with USB's in general
  2. A more specific tutorial/guide/example code of how to read data off a USB device on Windows 7
  3. What exactly is libusb and is it what I am looking for?

Answer

mark picture mark · Jul 11, 2013

Well, if you want the raw data from the device, you can find it in the USB device list and literally read from it like a file.

Start with getting all the devices:

HDEVINFO hDevInfo = SetupDiGetClassDevs( &HIDWatch::GUID_DEVINTERFACE_HID, 0, 0, DIGCF_DEVICEINTERFACE );

Enumerate each one until you find what you're looking for:

SetupDiEnumInterfaceDevice( hDevInfo, NULL, &HIDWatch::GUID_DEVINTERFACE_HID, index++, &InterfaceInfoData )

Get the device detail via SetupDiGetInterfaceDeviceDetail... then open a file handle to the device path via CreateFile with GENERIC_READ access... run an DeviceIoControl to get the product string using IOCTL_HID_GET_PRODUCT_STRING. If it's not your device, close up the file CloseHandle. If it is yours, sit in a loop (preferably on a thread) doing a ReadFile and you'll get raw records from the device.

That should get you going...