截取usb数据包,控制usb设备----Relay设备
2014-09-24 23:21
453 查看
在项目开发当中,我们需要一个usb转继电器的设备当开关控制无线发射设备,采购部采购时并未详细了解Relay设备的运行环境就买了一批设备,之后发现设备厂家只提供了windows库,而我们是要在linux中开发。无语中。。。。。。
Relay设备虽然是无驱的,可我并不知道它的协议,怎么办呢? I have no choice ,but I have bus hound,LOL.
厂家提供了windows的管理工具,可以实现Relay的开断,因此我通过Bus Hound截取usb数据包,得到通信协议。 LOL 可以编程咯。。。。。。
Bus Hound截取的数据包如下:
open device:
lock relay:
unlock relay:
Codes如下,只是个简单的测试程序,并未讲究编程中的那些xxxxxxxx
后记:
哈哈,开心啊,终于实现了控制Relay设备。
一句话:没有解决不了的问题!致自己,致大家,希望在挣扎中和大家一起进步。
Relay设备虽然是无驱的,可我并不知道它的协议,怎么办呢? I have no choice ,but I have bus hound,LOL.
厂家提供了windows的管理工具,可以实现Relay的开断,因此我通过Bus Hound截取usb数据包,得到通信协议。 LOL 可以编程咯。。。。。。
Bus Hound截取的数据包如下:
open device:
lock relay:
unlock relay:
Codes如下,只是个简单的测试程序,并未讲究编程中的那些xxxxxxxx
/* It is a simple testing */ #include </usr/local/include/libusb-1.0/libusb.h> // libusb head file #include <stdio.h> #include <sys/types.h> #include <string.h> #define VID 0x16c0 // get of lsusb #define PID 0x05df // get of lsusb struct libusb_device_handle *devh = NULL; //unsigned char openstr[] = {0xa1, 0x01, 0x00, 0x03, 0x00, 0x00, 0x08, 0x00}; int main() { /* usb init before libusb_open* */ int ret = libusb_init(NULL); if (ret < 0) { perror("libusb_init"); return ret; } /* end */ /* open device with vid and pid, must after libusb_init */ devh = libusb_open_device_with_vid_pid(NULL, VID, PID); if (!devh) { perror("libusb_open_device_with_pid_vid"); libusb_exit(NULL); } /* end */ /* claim interface */ ret = libusb_claim_interface(devh, 0); if (ret < 0) { perror("libusb_claim_interface"); devh = NULL; libusb_close(devh); return ret; } /* end */ /* open device data */ unsigned char open_data[8]; memset(open_data, 0, sizeof(open_data)); if ( 0 > libusb_control_transfer(devh, 0xa1, 0x01, 0x3000, 0x00, open_data, 0x08, 1000)) { perror("libusb_control_transfer"); } printf("receive data: %s\n", open_data); int i = 0; for(i = 0; i < 8; i++) { printf("%02x\t", open_data[i]); } putchar(10); /* end */ /* lock relay */ unsigned char lock_data[] = {0xff, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; if (0 > libusb_control_transfer(devh, 0x21, 0x09, 0x0000, 0x00, lock_data, 0x08, 1000)) { perror("libusb_control_transfer"); } /* end */ /* delay */ sleep(2); /* unlock relay */ unsigned char unlock_data[] = {0xfd, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; if (0 > libusb_control_transfer(devh, 0x21, 0x09, 0x3000, 0x00, unlock_data, 0x08, 1000)) { perror("libusb_control_transfer"); } /* end */ /* release claim interface */ libusb_release_interface(devh, 0); /* end */ /* close device */ libusb_close(devh); return 0; }
后记:
哈哈,开心啊,终于实现了控制Relay设备。
一句话:没有解决不了的问题!致自己,致大家,希望在挣扎中和大家一起进步。
相关文章推荐
- USB存储设备单向控制的研究与实现
- USB存储设备单向控制的研究与实现
- 浅析libusb控制接口和mountd守护进程处理uevent切换usb设备的实现
- Win7系统权限控制USB设备的自动运行
- usb设备控制驱动
- 调用devcon.exe控制usb设备
- 控制域内USB存储设备的使用(GFI LANguard Portable Storage Control 2.0)
- .Net控制USB设备相关内容
- vc++实现控制USB设备启用与否
- Android设备通过usb控制zedboard产生pwm波实验
- 关于Android设备USBHID通信控制传输相关接口说明
- vc++实现控制USB设备启用与否
- Win7中利用系统权限控制禁止USB设备的自动运行
- vc++实现控制USB设备启用与否
- 远程控制同网段计算机USB设备是否可用
- vc++实现控制USB设备启用与否
- 【Windows编程】基于USB设备的开机锁
- 关于禁止USB移动设备,而不禁止USB打印机(扫描仪、鼠标等)
- 监听USB设备的插入和弹出
- linux下配置USB设备