Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
692 views
in Technique[技术] by (71.8m points)

Use libusb_bulk_transfer to read from a USB HID device, and get libusb_error_io

I am working on a small project to read the value from Mitutoyo digital indicator through USB. By adopting libUSB, I am able to open the device with correct PID and VID, and get the following information:

Reading device descriptor:
            length: 18
      device class: 0
               S/N: 3
           VID:PID: 0FE7:4001
         bcdDevice: 1100
   iMan:iProd:iSer: 1:2:3
          nb confs: 1

Reading BOS descriptor: no descriptor

Reading first configuration descriptor:
             nb interfaces: 1
              interface[0]: id = 0
interface[0].altsetting[0]: num endpoints = 1
   Class.SubClass.Protocol: 03.00.00
       endpoint[0].address: 81
           max packet size: 0008
          polling interval: 0A

Reading string descriptors:
   String (0x01): "Mitutoyo"
   String (0x02): "USB-ITN"
   String (0x03): "60026369"

Then, I tried the following code

int transferred = 0; // actual size for write
int received = 0; // actual size for read
const int TRANSFER_SIZE = 9;  // transfer size
unsigned char buffer[TRANSFER_SIZE]; // USB buffer

char _cmd[TRANSFER_SIZE]; // command to be sent to device
strncpy_s(_cmd, "", sizeof(""));
strncat_s(_cmd, "R
", sizeof("R
"));
printf("_cmd %s 
", _cmd);

const int command_len = strlen(_cmd); // Get the length of the command string we are sending
if (command_len > TRANSFER_SIZE)
{
    printf("Error: command is larger than our limit of %i
", TRANSFER_SIZE);
    return -1;
}

memset(buffer, 0, TRANSFER_SIZE); // Zero out buffer to pad with null values 
memcpy(&buffer, _cmd, strlen(_cmd)); // put command into buffer

// write to device
int e = libusb_bulk_transfer(handle, 0, buffer, TRANSFER_SIZE, &transferred, 1500);
if (e >= 0)
{
    printf("
Write successful!");
    printf("
Sent %d bytes with string: %s
", transferred, buffer);
}
else
    printf("
Error in write! e = %d and transferred = %d
", e, transferred);

// read from device
e = libusb_bulk_transfer(handle, BULK_EP_IN, buffer, TRANSFER_SIZE, &received, 1000);
if (e >= 0)
{
    printf("
Received: ");
    printf("%c", buffer[i]); //Will read a string from LPC2148
}
else
    printf("
Error in read! e = %d and received = %d
", e, received);

However, I am getting

Error in write! e = -1 and transferred = 0
Error in read! e = -1 and received = 0

which corresponds to libusb_error_io. I didn't find any details on the error type. So I am wondering if the error indicates the wrong I/O address, or the wrong protocol that I send to the device, or missing some system dependencies for the libUSB?

BTW, I am randomly trying out the protocols for the device because I didn't find any documentations for this type. The command "R " originates from a different type of device from Mitutoyo. If anyone happens to know the protocol for the Mitutoyo digital indicator Series ID-S, I would appreciate if more information is provided.

Another question, is it necessary to put a "" at the start of the buffer? If the standard transfer size for low speed device is 8, the buffer that I should send to the device is 8+1. Is that correct?

Thank you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...