尝试计算Modbus RTU CRC。怎么了?

时间:2018-03-29 16:59:26

标签: c++ c checksum modbus crc16

我试图在PIC的应用程序中计算crc modbus,但与在线模拟器相比,返回的crc总是不正确的..按照代码

void CRC (unsigned char * msg, unsigned char * out)
{
    // char CRC16 [2] = {0xFF, 0xFF};
    unsigned int CRC16 = 0xffff;
    unsigned int poly = 0xA001;
    unsigned char data [14];
    unsigned char crc [2];

    for (int i = 0; i <14; i ++)
    {
        CRC16 = (unsigned int) msg ​​[i] ^ CRC16; // change date to msg
        for (int j = 0; j <8; j ++)
        {
            CRC16 >> = 1;

            if (CRC16 & 0x0001 == 1)
            {
                CRC16 = poly;
            }
        }
    }
    crc [0] = CRC16 >> 8;
    crc [1] = CRC16 & lt; / RTI & gt;
    strcpy (data, msg);
    strcat (data, crc);
    strcpy (out, date);


}

我输入一个12字节的缓冲区用于计算,最后应该得到crc旁边的缓冲区..但是crc本身的计算返回了错误的值..代码中可能出错了什么? / p>

以防我的邮件返回8C0C但返回68FE

0 个答案:

没有答案