1.工具准備
USB 監視軟件:Device Monitoring Studio7.25
PC端軟件:單片機多功能調試助手
2.發送數據包
接收數據包
3.數據分析
usb hid(pc軟件)發送幀(payload)數據解析:
payload總長度為64字節
0c 7e 55 09 00 00 01 00 16 00 00 78 77 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0c -->發送一幀數據的長度(0c為發送的實際有效數據長度)
7e 55 -->幀頭是指示一數據的開始,值為0x7E55
09 -->長度是從源地址開始到CRC結束(包含 CRC )的字節數
00 00 -->源地址指示發出本幀數據的設備
01 00 -->目標地址指示接收本幀數據的設備
16 -->讀取 ISO14443A標簽UID
00 -->保留固定為0x00
00 -->讀取空閑標簽
78 77 -->CRC為從幀長度開始(含)到參數區結束的CRC校驗,具體算法見附錄 A
usb hid(R321-13.56MHZ讀卡器)響應幀(payload)數據解析:
payload總長度為64字節
1c 7e 55 19 01 00 00 00 1f 16 00 04 00 04 76 b9
c7 4a 00 00 00 00 00 00 08 00 00 87 f4 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1c -->接收一幀數據的長度(1c為接收到的實際有效數據長度)
7e 55 -->幀頭是指示一數據的開始,值為0x7E55
19 -->長度是從源地址開始到CRC結束(包含 CRC )的字節數
01 00 -->源地址指示發出本幀數據的設備
00 00 -->目標地址指示接收本幀數據的設備
1f -->響應幀標志
16 -->讀取 ISO14443A標簽UID
00 -->保留固定為0x00
04 00 04 76 b9 c7 4a 00 00 00 00 00 00 08 00 00 -->標簽進入場內響應幀參數
87 f4 -->CRC為從幀長度開始(含)到參數區結束的CRC校驗,具體算法見附錄 A
#include <stdio.h> #include <string.h> //附錄 A CRC16校驗C程序代碼 // #define RUF_MASK 0x 80 //x^16 + 12 5 1 // #define POLYNOMIAL 0x8408 #define PRESET_VALUE 0xFFFF #define CHECK_VALUE 0xF0B8 #define CALC_CRC 0x1 #define CHECK_CRC 0x0 unsigned short calc_crc(unsigned char byte_len, unsigned char *data_byte); // unsigned short calc_crc(unsigned char byte_len, unsigned char *data_byte) { unsigned short current_crc_value; unsigned short i, j; current_crc_value = PRESET_VALUE; for (i = 0; i < byte_len; i++) { current_crc_value = current_crc_value ^ data_byte[i]; for (j = 0; j < 8; j++) { if (current_crc_value & 0x0001) { current_crc_value = (current_crc_value >> 1) ^ POLYNOMIAL; } else { current_crc_value = (current_crc_value >> 1); } } } current_crc_value = ~current_crc_value; return (current_crc_value); } int main(int argc, char const *argv[]) { unsigned short res1 = 0, res2 = 0; /* code */ //0c 7e 55 09 00 00 01 00 16 00 00 78 77 unsigned char send_buffer[] = {0x09, 0x00, 0x00, 0x01, 0x00, 0x16, 0x00, 0x00}; //1c 7e 55 19 01 00 00 00 1f 16 00 04 00 04 76 b9 c7 4a 00 00 00 00 00 00 08 00 00 87 f4 unsigned char recv_buffer[] = {0x19, 0x01, 0x00, 0x00, 0x00, 0x1f, 0x16, 0x00, 0x04, 0x00, 0x04, 0x76, 0xb9, 0xc7, 0x4a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00}; res1 = calc_crc(sizeof(send_buffer), send_buffer); res2 = calc_crc(sizeof(recv_buffer), recv_buffer); printf("send crc result is msb-->%#2X lsb-->%#2X\n", res1 / 256, res1 % 256); printf("recv crc result is msb-->%#2X lsb-->%#2X\n", res2 / 256, res2 % 256); getchar(); return 0; }
4.總結
usb通訊需要進行兩次數據交互才能讀取到數據
1.usb hid發送者(PC或其他設備)發送數據包給usb hid接收者(PC或其他設備),usb hid接收者發送接收到發送者發來的消息的確認信息給發送者
2.usb hid接收者(PC或其他設備)回復數據包給usb hid發送者(PC或其他設備),usb hid發送者回復接收者發來的數據包的確認信息給接收者
注:這里主要關心發送者和接收者發送數據包中的payload消息