gpt4 book ai didi

c - LIBUSB_ERROR_TIMEOUT 当我尝试使用 libusb raspberry pi 3 发送 usb 数据时

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:36:00 25 4
gpt4 key购买 nike

我正在做一个项目,我需要通过 USB 端口与指纹传感器 (gt-511c1r) 通信(Uart 不适合我)。我正在使用 libusb 库尝试与附加的简单代码进行通信。
显然一切正常,直到我尝试向传感器发送数据,然后发生 LIBUSB_ERROR_TIMEOUT 错误。
我正在使用带有更新固件的树莓派 3。我还将 lsusb -v 命令的输出附加到设备。
也用 SO ubuntu 在 PC 上测试代码,我有同样的问题,那么它将是代码问题??
任何可以给我解决问题的帮助或线索,我都会感谢你。问候

    //codigo de prueba
#include <stdio.h>
#include <libusb-1.0/libusb.h>
#include <stdint.h>
#include <string.h>


/*--------------------------------------------------------------------*/
int main(int argc, char*argv[])
{
int res = 0; /* return codes from libusb functions */
libusb_device_handle* handle = 0; /* handle for USB device */
int kernelDriverDetached = 0; /* Set to 1 if kernel driver detached*/
int numBytes = 0; /* Actual bytes transferred. */
uint8_t buffer[64]; /* 64 byte transfer buffer */
int ep_out = 0x02;
int ep_in = 0x81;

/* Initialise libusb. */
res = libusb_init(0);
if (res != 0)
{
fprintf(stderr, "Error initialising libusb.\n");
return 1;
}

/* Get the first device with the matching Vendor ID and Product ID.If
* intending to allow multiple demo boards to be connected at once,you
* will need to use libusb_get_device_list() instead. Refer to the libusb
* documentation for details. */
handle = libusb_open_device_with_vid_pid(0, 0x04d9, 0x8008);
if (!handle)
{
fprintf(stderr, "Unable to open device.\n");
return 1;
}

/* Check whether a kernel driver is attached to interface #0. If so, we'll
* need to detach it.*/
if (libusb_kernel_driver_active(handle, 0))
{
res = libusb_detach_kernel_driver(handle, 0);
if (res == 0)
{
kernelDriverDetached = 1;
}
else
{
fprintf(stderr, "Error detaching kernel driver.\n");
return 1;
}
}

/* Claim interface #0. */
res = libusb_claim_interface(handle, 0);
if (res != 0)
{
fprintf(stderr, "Error claiming interface.\n");
return 1;
}

memset(buffer, 0, 12);
buffer[0] = 0x55;
buffer[1] = 0xAA;
buffer[2] = 0x01;
buffer[3] = 0x00;
buffer[4] = 0x00;
buffer[5] = 0x00;
buffer[6] = 0x00;
buffer[7] = 0x00;
buffer[8] = 0x01;
buffer[9] = 0x00;
buffer[10] = 0x01;
buffer[11] = 0x01;

res = libusb_bulk_transfer(handle, ep_out, buffer, 12, &numBytes, 100);
if (res == 0)
{
printf("%d bytes transmitted successfully.\n", numBytes);
}
else
{
fprintf(stderr, "Error during send message: %s\n",libusb_error_name(res));
}

memset(buffer, 0, 12);

res = libusb_bulk_transfer(handle, ep_in, buffer, 12, &numBytes, 100);
if (res == 0)
{
printf("%d bytes receibed successfully.\n", numBytes);
}
else
{
fprintf(stderr, "Error during receibe response:
%s\n",libusb_error_name(res));
}



/* Release interface #0. */
res = libusb_release_interface(handle, 0);
if (0 != res)
{
fprintf(stderr, "Error releasing interface.\n");
}

/* If we detached a kernel driver from interface #0 earlier, we'll now
* need to attach it again. */
if (kernelDriverDetached)
{
libusb_attach_kernel_driver(handle, 0);
}

/* Shutdown libusb. */
libusb_exit(0);

return 0;
}

这里是 lsusb -v 的输出

     Bus 001 Device 013: ID 04d9:8008 Holtek Semiconductor, Inc. 
Device Descriptor:
bLength 18
bDescriptorType 1
bcdUSB 2.00
bDeviceClass 0 (Defined at Interface level)
bDeviceSubClass 0
bDeviceProtocol 0
bMaxPacketSize0 64
idVendor 0x04d9 Holtek Semiconductor, Inc.
idProduct 0x8008
bcdDevice 1.00
iManufacturer 1 FINGER
iProduct 2 USB-MASS STORAGE
iSerial 3 000000000001
bNumConfigurations 1
Configuration Descriptor:
bLength 9
bDescriptorType 2
wTotalLength 32
bNumInterfaces 1
bConfigurationValue 1
iConfiguration 0
bmAttributes 0xc0
Self Powered
MaxPower 100mA
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 0
bAlternateSetting 0
bNumEndpoints 2
bInterfaceClass 8 Mass Storage
bInterfaceSubClass 6 SCSI
bInterfaceProtocol 80 Bulk-Only
iInterface 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x81 EP 1 IN
bmAttributes 2
Transfer Type Bulk
Synch Type None
Usage Type Data
wMaxPacketSize 0x0040 1x 64 bytes
bInterval 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x02 EP 2 OUT
bmAttributes 2
Transfer Type Bulk
Synch Type None
Usage Type Data
wMaxPacketSize 0x0040 1x 64 bytes
bInterval 0
Device Status: 0x0001
Self Powered

最佳答案

问题解决了。所有代码本身都很好,问题是传感器不理解我发送给他的内容,因为我没有根据传感器的 usb 协议(protocol)使用发送正确的包。

关于c - LIBUSB_ERROR_TIMEOUT 当我尝试使用 libusb raspberry pi 3 发送 usb 数据时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48967615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com