我正在运行下面的程序,它可以与我的一些设备一起使用,但是在init,open,claim和bulk(write)之后的三个程序中工作正常,我的批量(读取)在我的应答缓冲区中返回0x01 0x60 。实际上我发送的东西并不重要;打开USB后,如果我尝试批量读取...我每次都有这个答案(即使是在无限循环中)。它可以是什么?
int main() {
libusb_device **devs; // Pointer to pointer of device, used to retrieve a list of devices
libusb_device_handle *dev_handle; // A device handle
libusb_context *ctx = NULL; // A libusb session
struct libusb_control_setup setup;
int r; // For return values
int i = 0;
int iCmdLen_BCR = 6;
int iCmdLen = 0;
int actual = 0; // Used to find out how many bytes were written
ssize_t cnt; // Holding number of devices in list
unsigned char sRsp[1024];
unsigned char sAux[1024];
unsigned char data[128];
unsigned char data_BCR[128]; // Data to write
memset(data, 0, sizeof(data));
memset(data_BCR, 0, sizeof(data_BCR));
memset(sRsp, 0, sizeof(sRsp));
memset(sAux, 0, sizeof(sAux));
r = libusb_init(&ctx); // Initialize the library for the session we just declared
if(r < 0) {
printf("[%04d] Init Error [%d]\n", __LINE__, r); // There was an error
return 1;
}
libusb_set_debug(ctx, 3); // Set verbosity level to 3, as suggested in the documentation
cnt = libusb_get_device_list(ctx, &devs); // Get the list of devices
if(cnt < 0) {
printf("[%04d] Get Device Error\n", __LINE__); // There was an error
return 1;
}
printf("[%04d] Devices in list.\n", __LINE__);
dev_handle = libusb_open_device_with_vid_pid(ctx, VD_ID_BCR, PD_ID_BCR); // These are vendorID and productID I found for my usb device
if(dev_handle == NULL)
printf("[%04d] Cannot open device\n", __LINE__);
else
printf("[%04d] Device Opened\n", __LINE__);
libusb_free_device_list(devs, 1); // Free the list, unref the devices in it
if(libusb_kernel_driver_active(dev_handle, 0) == 1) { // Find out if kernel driver is attached
printf("[%04d] Kernel Driver Active\n", __LINE__);
if(libusb_detach_kernel_driver(dev_handle, 0) == 0) // Detach it
printf("[%04d] Kernel Driver Detached!\n", __LINE__);
}
r = libusb_claim_interface(dev_handle, 0); // Claim interface 0 (the first) of device (mine had jsut 1)
if(r < 0) {
printf("[%04d] Cannot Claim Interface\n", __LINE__);
return 1;
}
printf("[%04d] Claimed Interface r[%d]\n", __LINE__, r);
memcpy(data_BCR, "\x04\xC8\x04\x00\xFF\x30", iCmdLen_BCR);
memcpy(data, data_BCR, iCmdLen_BCR);
iCmdLen = iCmdLen_BCR;
r = libusb_bulk_transfer(dev_handle, (2 | LIBUSB_ENDPOINT_OUT), data, iCmdLen, &actual, 0); // My device's out endpoint was 2, found with trial- the device had 2 endpoints: 2 and 129
if(r >= 0)
{
printf("[%04d] Writing Successful!\n", __LINE__);
r = libusb_bulk_transfer(dev_handle, 0x81, sRsp, 1024, &actual, 10000);
for(i=0; i<actual ; i++)
{
sprintf(sAux+strlen(sAux), "%02X ", sRsp[i]);
}
printf("[%04d] result bulk read[%d] actual[%d] Rsp[%s]\n", __LINE__, r, actual, sAux);
}
else
{
printf("[%04d] Write Error\n", __LINE__);
}
r = libusb_release_interface(dev_handle, 0); // Release the claimed interface
if(r!=0) {
printf("[%04d] Cannot Release Interface\n", __LINE__);
return 1;
}
printf("[%04d] Released Interface\n", __LINE__);
libusb_close(dev_handle); // Close the device we opened
libusb_exit(ctx); // Needs to be called to end the
//delete[] data; // Delete the allocated memory for data
return 0;
}