我使用5v脉冲对USB相机进行脉冲拍摄并拍照。脉冲相机然后将USB信号发送回覆盆子pi。我正在编写一个程序来收集通过USB发送的图像。下面是我的代码,用于在触发后开始从相机获取输入的功能。
void opendevice()
{
libusb_device_handle* dev;
struct libusb_device_descriptor* desc;
int r;
int err;
int transfered;
libusb_init(NULL);
dev = libusb_open_device_with_vid_pid( NULL, 0x2a0b, 0x00f8);
if (dev == NULL)
{
printf("device not found\n");
}
if(libusb_kernel_driver_active(dev, 0) == 1)
{
printf("Driver active\n");
if(libusb_detach_kernel_driver(dev, 0) == 0)
{
printf("Kernel Driver Detached\n");
}
}
libusb_set_configuration(dev,1);
err = libusb_claim_interface(dev, 0);
if(err != 0)
{
printf("cannot claim interface\n");
}
unsigned char usb_data[4];
int size = sizeof(unsigned int) *1280 *960;
unsigned *data;
data = (unsigned int *)malloc(size);
r = libusb_bulk_transfer(dev, LIBUSB_ENDPOINT_IN | 0x83, usb_data, sizeof(data), &transfered, 0);
if(r == 0)
{
printf("data has been transfered\n");
}
else{
printf("error code %d\n", r);
printf("bytes transfered %d\n", transfered);
}
}
我找到设备分离检查以查看内核是否正在使用它,然后我是否将其分离。在分离之后,我声称接口然后等待在批量传输内发生转移。但是,我从未收到转移数据。即使从usb端口r拔出相机,返回值为-1,我的传输大小为0.有人能告诉我这里缺少什么吗?
如果您需要更多信息,请与我们联系。
答案 0 :(得分:2)
您的代码有缓冲区溢出:
unsigned char usb_data[4]; // <---
int size = sizeof(unsigned int) *1280 *960;
unsigned *data; // <-- not used!
data = (unsigned int *)malloc(size);
r = libusb_bulk_transfer(dev, LIBUSB_ENDPOINT_IN | 0x83, usb_data //<
,sizeof(data), &transfered, 0)
您可能想要使用&#34;数据&#34;作为libusb_bulk_transfer()
调用中的接收缓冲区,但实际上使用的usb_data
只有4个字节长。