请教读取USB HID设备的程序,以及源代码,简单点即可
现要用C++ Builder来做一个程序,来读取USB HID设备,比较盲目,没弄过USB硬件方面的设备,需要一个简单的程序(主要是源码)来读取该设别,请教大虾们,我的邮箱:keri_syml@sina.com
非常感谢....
[解决办法]
用ccFelix推荐的Libusb-win32来做,不过还是需要你去了解USB协议。
如下例子是读取USB设备的设备描述符,仅供参考
usb_dev_handle *open_dev(void)
{
struct usb_bus *bus;
struct usb_device *dev;
for(bus = usb_get_busses(); bus; bus = bus->next)
{
for(dev = bus->devices; dev; dev = dev->next)
{
if(dev->descriptor.idVendor == MY_VID
&& dev->descriptor.idProduct == MY_PID)
{
return usb_open(dev);
}
}
}
return NULL;
}
void __fastcall TForm1::Button3Click(TObject *Sender)
{
usb_dev_handle *dev = NULL; /* the device handle */
char tmp[BUF_SIZE] = {0};
char y = 2 ;
int temp = -1 ;
usb_init(); /* initialize the library */
usb_find_busses(); /* find all busses */
temp = usb_find_devices(); /* find all connected devices */
Memo1->Lines->Add(temp);
if(!(dev = open_dev()))
{
Memo1->Lines->Add("error: device not found!\n");
return ;
}
if(usb_set_configuration(dev, 1) < 0)
{
Memo1->Lines->Add("error: setting config 1 failed\n");
usb_close(dev);
return ;
}
if(usb_claim_interface(dev, 0) < 0)
{
Memo1->Lines->Add("error: claiming interface 0 failed\n");
usb_close(dev);
return ;
}
if(usb_control_msg(dev, 258, 12, 0, 1, &y,1, 1) < 0)
{
Memo1->Lines->Add("error: 读取设备描述符失败\n");
}
for (int i = 0; i < 64; i++)
{
Memo1->Lines->Add(IntToHex((unsigned char)tmp[i],2)) ;
}
usb_release_interface(dev, 0);
usb_close(dev);
}
[解决办法]
//函数功能:打开USB 2012-11-26
bool USBAsp_open(void)
{
struct usb_bus *bus;
struct usb_device *dev = 0;
if(usbhandle!=NULL)
{
usb_close(usbhandle);
usbhandle=NULL;
}
usb_init();
usb_find_busses();
usb_find_devices();
for(bus=usb_get_busses(); bus; bus=bus->next)
{
for(dev=bus->devices; dev; dev=dev->next)
{
if((dev->descriptor.idVendor == USBDEV_VENDOR) && (dev->descriptor.idProduct == USBDEV_PRODUCT))
break;
}
if(dev)
break;
}
if(!dev)
{
usbhandle=NULL;
return false;
}
usbhandle = usb_open(dev);
if(!usbhandle)
{
ShowMessage("不能打开USB!");
usbhandle=NULL;
return false;
}
usb_set_configuration(usbhandle, 1); //写数据前的初始化
usb_claim_interface(usbhandle, 0);
InitializeCriticalSection(&m_CommunicationSyn);
UsbRead = new Usb_Port(false); //挂起线程
IsRun=true;
return true;
}