16bit CRC-ITU calculation for Concox tracker

房东的猫 提交于 2019-12-08 10:37:34

问题


I am creating C# code for a server program that receives data from a Concox TR06 GPS tracker via TCP:

http://www.iconcox.com/uploads/soft/140920/1-140920023130.pdf

When first starting up, the tracker sends a login message, which needs to be acknowledged before it will send any position data. My first problem is that, according to the documentation, the acknowledge message is 18 bytes long, yet the example they provide is only 10 bytes long:

P.s. in the table above, the "bits" column I'm pretty sure should be labelled "bytes" instead...

Now, my main problem is in calculating the Error Check. According to the documentation:

The check code is generated by the CRC-ITU checking method. The check codes of data in the structure of the protocol, from the Packet Length to the Information Serial Number (including "Packet Length" and "Information Serial Number"), are values of CRC-ITU.

Ok, so in the above example, I need to calculate CRC on 0x05 0x01 0x00 0x01

Now, I'm guessing it's 16 bit CRC, as according to the diagram above, the CRC is 2 bytes long. I've implemented two different CRC implementations I found online at http://www.sanity-free.org/134/standard_crc_16_in_csharp.html and http://www.sanity-free.org/133/crc_16_ccitt_in_csharp.html but neither give me the answer that, according to the diagram above I am supposed to be getting - 0xD9 0xDC. I've even used this site - https://www.lammertbies.nl/comm/info/crc-calculation.html - to manually enter the 4 bytes, but nothing gives me the result I'm supposed to be getting according to the diagram above...

Any ideas where I might be going wrong? Any pointers/hints would be greatly appreciated. Thank you


回答1:


The ITU CRC-16 is also called the X-25 CRC. You can find its specification here, which is:

width=16 poly=0x1021 init=0xffff refin=true refout=true xorout=0xffff check=0x906e name="X-25"

My crcany code will take that specification and generate C code to compute the CRC.

Here is the bit-wise (slow) code thusly generated:

#include <stddef.h>

unsigned crc16x_25_bit(unsigned crc, void const *data, size_t len) {
    if (data == NULL)
        return 0;
    crc = ~crc;
    crc &= 0xffff;
    while (len--) {
        crc ^= *(unsigned char const *)data++;
        for (unsigned k = 0; k < 8; k++)
            crc = crc & 1 ? (crc >> 1) ^ 0x8408 : crc >> 1;
    }
    crc ^= 0xffff;
    return crc;
}


来源:https://stackoverflow.com/questions/39459262/16bit-crc-itu-calculation-for-concox-tracker

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!