Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ CRC16 modbus计算返回的长数据包值错误_C++_Qt_Crc_Crc16 - Fatal编程技术网

C++ CRC16 modbus计算返回的长数据包值错误

C++ CRC16 modbus计算返回的长数据包值错误,c++,qt,crc,crc16,C++,Qt,Crc,Crc16,我正在通过串行端口连接到设备。该设备以big-endian模式将CRC16标签附加到数据包的末尾。在软件方面,检查CRC的代码如下: bool Protocol::checkCRC(const QByteArray &buf) { if(buf.size()<3){ return false; } int len = buf.size()-2; // Exclude CRC token quint16 crc = 0xFFFF;

我正在通过串行端口连接到设备。该设备以big-endian模式将CRC16标签附加到数据包的末尾。在软件方面,检查CRC的代码如下:

bool Protocol::checkCRC(const QByteArray &buf) {
    if(buf.size()<3){
        return false;
    }
    int len = buf.size()-2; // Exclude CRC token
    quint16 crc = 0xFFFF;
    quint16 claim = static_cast<uchar>(buf.at(buf.size()-1));
    claim*=0x100;
    claim+=static_cast<uchar>(buf.at(buf.size()-2));

    for (int pos = 0; pos < len; pos++)
    {
        crc ^= (quint16)buf[pos];         // XOR byte into LSB of crc
        for (int i = 8; i != 0; i--) {    // Loop over each bit
            if ((crc & 0x0001) != 0) {    // If the LSB is set
                crc >>= 1;                // Shift right and XOR 0xA001
                crc ^= 0xA001;
            }
            else                          // Else LSB is not set
                crc >>= 1;                // Just shift right
        }
    }
    return crc==claim;
}
报告的CRC为
0xC13D
,函数也计算
0xC13D
。但对于大数据包(在我的例子中是53字节),函数无法计算正确的CRC:

0x34, 0x02, 0x02, 0x08, 0x14, 0x00, 0x00, 0x00,
0x00, 0x00, 0x10, 0x0a, 0xdf, 0x07, 0x0a, 0x39, 
0x1b, 0x02, 0x02, 0x79, 0x61, 0xbf, 0x34, 0xdd, 
0x0b, 0x83, 0x0f, 0x10, 0x03, 0x1b, 0x11, 0x02, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0xfc, 0x98

报告的CRC是
0x98FC
,但计算值是
0xDFC4

我不知道
QByteArray
类型是什么,但我敢打赌它是一个有符号字符数组。因此,当字节的高位为1时,当转换为整数时,该符号位将被扩展,该整数在
CRC^=(quint16)buf[pos]处以异或方式进入CRC。因此,当您到达
0xdf
时,
crc
0xffdf
是独占的,而不是预期的
0xdf

因此,问题不在于长度,而在于设置高位字节的可能性


您需要提供无符号字节,或修复转换,或在与CRC进行异或运算之前对结果字节执行
&0xff

我不知道
QByteArray
类型是什么,但我敢打赌它是一个有符号字符数组。因此,当字节的高位为1时,当转换为整数时,该符号位将被扩展,该整数在
CRC^=(quint16)buf[pos]处以异或方式进入CRC。因此,当您到达
0xdf
时,
crc
0xffdf
是独占的,而不是预期的
0xdf

因此,问题不在于长度,而在于设置高位字节的可能性


您需要提供无符号字节,或修复转换,或在与CRC进行异或运算之前对结果字节执行
&0xff

来自Qt源:
typedef unsigned short quint16
尽管
QByteArray
保留
char
它应该可以工作。问题是
quint16
crc^=(quint16)buf[pos]
中的转换,它应该是
quint8
Ah,对,事实上,quint16的签名性并不重要,因为当签名字符转换为更大的类型时,损坏已经是一个了。
(quint8)
在转换为更大的类型之前使其无符号。从Qt源:
typedef unsigned short quint16
尽管事实上
QByteArray
保留
char
它应该可以工作。问题是
quint16
crc^=(quint16)buf[pos]
中的转换,它应该是
quint8
Ah,对,事实上,quint16的签名性并不重要,因为当签名字符转换为更大的类型时,损坏已经是一个了。
(quint8)
在转换为更大的类型之前使其未签名。
0x34, 0x02, 0x02, 0x08, 0x14, 0x00, 0x00, 0x00,
0x00, 0x00, 0x10, 0x0a, 0xdf, 0x07, 0x0a, 0x39, 
0x1b, 0x02, 0x02, 0x79, 0x61, 0xbf, 0x34, 0xdd, 
0x0b, 0x83, 0x0f, 0x10, 0x03, 0x1b, 0x11, 0x02, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0xfc, 0x98