原创:USB HID读卡器数据解析(R321-13.56MHZ读卡器)

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消息

posted @ 2020-09-11 10:33  Tony.Jia  阅读(1099)  评论(0编辑  收藏  举报