//gliethttp_20080403 libusb实际测试代码
//0x02为bulk类型的OUT端点
//0x81为bulk类型的IN端点
usb_dev_handle *tavor_udev;
BOOL OpenUsbPort (long int DelayTime)
{
struct usb_bus *bus;
struct usb_device *dev;
usb_init();
usb_find_busses();
usb_find_devices();
tavor_udev = NULL;
for(bus = usb_busses;bus; bus = bus->next)
{
for (dev = bus->devices;dev; dev = dev->next)
{
if( (dev->descriptor.idVendor == 0x8086) && (dev->descriptor.idProduct == 0xd001) )
{
tavor_udev = usb_open(dev);
return tavor_udev;
}
}
}
fprintf(stderr,"tavor usb could not be found\n");
return tavor_udev;
}
BOOL WriteUsb (WTPCOMMAND WtpCmd,DWORD dwCmdSize)
{
int write_bytes;
write_bytes = usb_bulk_write(tavor_udev,0x02,(char*)&WtpCmd,dwCmdSize,200);
return write_bytes;
}
BOOL ReadUsb(WTPSTATUS *WtpStatus, DWORD dwSize, PDWORD pBytesRead)
{
int read_bytes;
read_bytes = usb_bulk_read(tavor_udev,0x81,(char*)WtpStatus,dwSize,0);
if(pBytesRead)*pBytesRead = read_bytes;
return read_bytes;
}
BOOL ReadUsbCharBuffer (char *WtpMessage,DWORD dwSize,PDWORD pBytesRead)
{
int result;
if (tavor_udev == NULL)
{
printf (" Error: OpenUsbPort must be called prior to calling ReadUsb!");
return FALSE;
}
result = usb_bulk_read(tavor_udev,0x81,(char*)WtpMessage,dwSize,100);
if (result < 0 )
{
printf (" Error: ReadFile failed with error\n");
return FALSE;
}
*pBytesRead = result;
if (*pBytesRead != dwSize) return FALSE;
return TRUE;
}
void CloseUsbPort ()
{
if(tavor_udev)usb_close(tavor_udev);
}
|
使用libusb进行一些操作必须使用sudo超级用户才有权限,所以必须首先sudo su
比如:
usb_get_string_simple(udev, dev->descriptor.iManufacturer, string, sizeof(string));
usb_get_string_simple(udev, dev->descriptor.iProduct, string, sizeof(string));
int usb_bulk_write(usb_dev_handle *dev, int ep, char *bytes, int size,int timeout);
int usb_bulk_read(usb_dev_handle *dev, int ep, char *bytes, int size,int timeout);
int usb_interrupt_write(usb_dev_handle *dev, int ep, char *bytes, int size,int timeout);
int usb_interrupt_read(usb_dev_handle *dev, int ep, char *bytes, int size,int timeout);
等都需要sudo超级用户下才能正常,所以需要sudo su来执行我们的ntwptp.exe程序
阅读(24928) | 评论(5) | 转发(0) |