Comment lire et envoyer des données avec libusb pour C/C++ ?

Comment lire et envoyer des données avec libusb pour C/C++ ?

J’ai travaillé avec libusb et jusqu’à présent, je n’ai réussi qu’à lire le vendorID et le productID. Vous ne pouvez pas non plus savoir quel est le nom du périphérique USB, vous devez donc vérifier ici quel est le nom du périphérique USB : http://www.linux-usb.org/usb.ids

C’est ainsi que j’ai résolu le problème de connexion et de lecture du nom du périphérique USB.

#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 deviceID; std::vector deviceName; }USB_devices; std::vector 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 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& portNames, std::vector& vendorIDs, std::vector& 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 vendorID != vendorID) continue; // Overwrite vendor name vendorName = devices.at(i)->vendorName; // Print the name std::vector deviceID = devices.at(i)->deviceID; std::vector 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; }

Facebook
Twitter
LinkedIn
Pinterest

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.