zoukankan      html  css  js  c++  java
  • 截取usb数据包,控制usb设备----Relay设备

    在项目开发当中,我们需要一个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

     1 /* It is a simple testing */
     2 
     3 #include </usr/local/include/libusb-1.0/libusb.h>  // libusb head file
     4 #include <stdio.h>
     5 #include <sys/types.h>
     6 #include <string.h>
     7 
     8 #define VID 0x16c0      // get of lsusb
     9 #define PID 0x05df      // get of lsusb
    10 
    11 struct libusb_device_handle *devh = NULL;
    12 
    13 //unsigned char openstr[] = {0xa1, 0x01, 0x00, 0x03, 0x00, 0x00, 0x08, 0x00};
    14 
    15 int main()
    16 {
    17     /* usb init before libusb_open* */
    18     int ret = libusb_init(NULL);
    19     if (ret < 0) {
    20         perror("libusb_init");
    21         return ret;
    22     }
    23     /* end */
    24 
    25     /* open device with vid and pid, must after libusb_init */
    26     devh = libusb_open_device_with_vid_pid(NULL, VID, PID);
    27     if (!devh) {
    28         perror("libusb_open_device_with_pid_vid");
    29         libusb_exit(NULL);
    30     }
    31     /* end */
    32 
    33     /* claim interface */
    34     ret = libusb_claim_interface(devh, 0);
    35     if (ret < 0) {
    36         perror("libusb_claim_interface");
    37         devh = NULL;
    38         libusb_close(devh);
    39         return ret;
    40     }
    41     /* end */
    42 
    43     /* open device data */
    44     unsigned char open_data[8];
    45     memset(open_data, 0, sizeof(open_data));
    46     if ( 0 > libusb_control_transfer(devh, 0xa1, 0x01, 0x3000, 0x00, open_data, 0x08, 1000)) {
    47         perror("libusb_control_transfer");
    48     }
    49     printf("receive data: %s
    ", open_data);
    50     int i = 0;
    51     for(i = 0; i < 8; i++) {
    52         printf("%02x	", open_data[i]);
    53     }
    54     putchar(10);
    55     /* end */
    56 
    57     /* lock relay */
    58     unsigned char lock_data[] = {0xff, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
    59     if (0 > libusb_control_transfer(devh, 0x21, 0x09, 0x0000, 0x00, lock_data, 0x08, 1000)) {
    60         perror("libusb_control_transfer");
    61     }
    62     /* end */
    63 
    64     /* delay */
    65     sleep(2);
    66 
    67     /* unlock relay */
    68     unsigned char unlock_data[] = {0xfd, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
    69     if (0 > libusb_control_transfer(devh, 0x21, 0x09, 0x3000, 0x00, unlock_data, 0x08, 1000)) {
    70         perror("libusb_control_transfer");
    71     }
    72     /* end */
    73 
    74     /* release claim interface */
    75     libusb_release_interface(devh, 0);
    76     /* end */
    77 
    78     /* close device */
    79     libusb_close(devh);
    80 
    81     return 0;
    82 }

    后记:

      哈哈,开心啊,终于实现了控制Relay设备。

      一句话:没有解决不了的问题!致自己,致大家,希望在挣扎中和大家一起进步。

  • 相关阅读:
    WPF如何判断PNG中的点是透明的
    Silverlight DataGrid自适应数据
    DEVExpress For WPF 中GridControl如何实现滚动分页(延迟查询)
    如何通过样式来处理根据自身其他属性内容赋值其他属性值的方法研究
    最近涉及到的一些需要备忘的东西
    (转载)Setup Factory 会话变量
    (转载)解决WPF动画属性锁死问题
    WPF InkCanvas MouseDown及MouseLeftButtonDown事件不触发的代替事件
    在win7与XP系统下 C#缺省路径不同
    Xamarin笔记
  • 原文地址:https://www.cnblogs.com/Daniel-G/p/3991785.html
Copyright © 2011-2022 走看看