During the project development, we need a USB-to-relay device to control the wireless transmitter device when the switch is enabled. When purchasing the device, the sourcing department buys a batch of devices without knowing the operating environment of the relay device, later, we found that the device manufacturer only provides the Windows library, and we want to develop it in Linux. Speechless ......
Although the relay device has no drive, I don't know its protocol. What should I do? I have no choice, but I have bus hound, LOL.
The manufacturer provides a Windows management tool to enable relay interruption. Therefore, I use bus hound to intercept USB packets and obtain the communication protocol. LOL can be programmed ......
The packet intercepted by bus hound is as follows:
Open device:
Lock relay:
Unlock relay:
Codes is as follows. It is just a simple test program, but it does not focus on the XXXXXXXX in programming.
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 lsusb10 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\n", open_data);50 int i = 0;51 for(i = 0; i < 8; i++) {52 printf("%02x\t", 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 }
Postscript:
Haha, I am so happy that I have finally achieved control over the relay device.
One sentence: No problem can be solved! To myself, to everyone, and to make progress together in the struggle.
Capture USB packets and control USB device ---- Relay Device