DealMakerz

Complete British News World

How can I read and send data using libusb for C/C++?

How can I read and send data using libusb for C/C++?

I’ve been working with libusb and so far I’ve only been able to read the Vendor ID and Product ID. You also can’t know the name of the USB device, so here you have to check the name of the USB device: http://www.linux-usb.org/usb.ids

This is how I solved the problem of connecting and reading the USB device name.

#define USB_IDS "Hardware/USB/usb.ids" libusb_context* ctx = nullptr; libusb_device_handle* device_handle = nullptr; uint16_t deviceVendorID; uint16_t deviceProductID; bool connectedToUSB = false; typedef struct { // Vendor uint16_t vendorID; std::string vendorName; // Product std::vector<uint16_t> deviceID; std::vector<std::string> deviceName; }USB_devices; std::vector<USB_devices*> devices; int initUSB() { // Init the USB int status = libusb_init(&ctx); // Load the descriptors std::string line; std::ifstream usbIds; usbIds.open(USB_IDS); if (!usbIds.is_open()) return -1; // Clear for (uint16_t i = 0; i < devices.size(); i++) { devices.at(i)->deviceName.clear(); devices.at(i)->deviceID.clear(); delete devices.at(i); } devices.clear(); // Create a struct USB_devices* usb = nullptr; uint16_t ID; std::string name; // Loop the whole list usb.ids while (std::getline(usbIds, line)) { // Check if the first number begins at '#' = 35 = 0x23 or just empty if (line.length() == 0) continue; if (line.at(0) == 0x23) continue; // Check if we reading a tab or just a number int shift = line.at(0) == '\t' ? 1 : 0; // Split, take first part and turn it to hex and string ID = std::stoi(line.substr(shift, line.find(" ")), 0, 16); name = line.substr(line.find(" ") + 1, line.length()); // +1 for reducing double space if (shift == 0) { // Create new usb = new USB_devices(); usb->vendorID = ID; usb->vendorName = name; devices.push_back(usb); } else { // If we have not generate an object first if (usb != nullptr) { usb->deviceID.push_back(ID); usb->deviceName.push_back(name); } } } usbIds.close(); } bool openUSBConnection(uint16_t vendorID, uint16_t productID) { // Connect device_handle = libusb_open_device_with_vid_pid(ctx, vendorID, productID); connectedToUSB = device_handle != nullptr ? true : false; // Check if (connectedToUSB) { // Auto detatch kernel driver if it's supported libusb_set_auto_detach_kernel_driver(device_handle, true); // Claim interface if (libusb_claim_interface(device_handle, 0) >= 0) { deviceVendorID = vendorID; deviceProductID = productID; }else { closeUSBConnection(); } } return isConnectedToUSB(); } bool closeUSBConnection() { if (isConnectedToUSB()) { libusb_release_interface(device_handle, 0); libusb_close(device_handle); } connectedToUSB = false; return !isConnectedToUSB(); } bool isConnectedToUSB() { return connectedToUSB; } void getUSBPortNames(std::vector<std::string>& portNames, std::vector<uint16_t>& vendorIDs, std::vector<uint16_t>& productIDs) { // Get the names libusb_device** devs; int listOfDevices = libusb_get_device_list(ctx, &devs); for (int i = 0; i < listOfDevices; i++) { libusb_device* dev = devs[i]; struct libusb_device_descriptor desc; libusb_get_device_descriptor(dev, &desc); // Vendor and product ID uint16_t vendorID = desc.idVendor; uint16_t productID = desc.idProduct; // Save vendorIDs.push_back(vendorID); productIDs.push_back(productID); // Default names std::string vendorName = "Vendor:" + std::to_string(vendorID); std::string productName = " Product:" + std::to_string(productID); // With space, it looks better // Search for the device name for (uint16_t i = 0; i < devices.size(); i++) { // Find vendor ID if (devices.at(i)->vendorID != vendorID) continue; // Overwrite vendor name vendorName = devices.at(i)->vendorName; // Print the name std::vector<uint16_t> deviceID = devices.at(i)->deviceID; std::vector<std::string> deviceName = devices.at(i)->deviceName; for (uint16_t j = 0; j < deviceID.size(); j++) { if (deviceID.at(j) != productID) continue; // Overwrite product name productName = deviceName.at(j); break; } } // Combine product name and vendor name std::string portName = vendorName + productName; portNames.push_back(portName); } libusb_free_device_list(devs, 1); } int sendDataViaUSB(uint8_t data[], int lengthOfData) { int transmittedBytes = 0; if (isConnectedToUSB()) { libusb_interrupt_transfer(device_handle, LIBUSB_ENDPOINT_OUT, data, lengthOfData, &transmittedBytes, 1000); } return transmittedBytes; } int readDataViaUSB(uint8_t data[], int lengthOfData) { int transmittedBytes = 0; if (isConnectedToUSB()) { libusb_interrupt_transfer(device_handle, LIBUSB_ENDPOINT_IN, data, lengthOfData, &transmittedBytes, 1000); } return transmittedBytes; }