Question

I am trying to modify that piece of code to make it work with arbitrary values and lengths.

My modification so far:

public static int ModRTU_CRC(final byte[] buf, int crc)
{
    final int len = buf.length;

    for (int pos = 0; pos < len; pos++)
    {
        crc ^= buf[pos]; // XOR byte into least sig. byte 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
            }
        }
    }
    // Note, this number has low and high bytes swapped, so use it
    // accordingly (or swap bytes)
    return crc;
}

I'm testing my code in the following way:

final String a = "1101011011";
final String crc = "10011";
final int parsedA = Integer.parseInt(a, 2);
final ByteBuffer parsedBytes = ByteBuffer.allocate(4).putInt(parsedA);
final byte[] array = parsedBytes.array();
final int parsedCRC = Integer.parseInt(crc, 2);

System.out.println(Integer.toBinaryString(ModRTU_CRC(array, parsedCRC)));

I get 1000111101000101 but the correct answer is 11010110111110. Even if i swap bytes i don't get to the goal. Can you help me please to figure out where i made a mistake?

Was it helpful?

Solution

You are seriously confused. Your attempted example is directly from Ross William's CRC tutorial. You need to read that tutorial again in its entirety and more slowly, working out everything as you go along.

The CRC and the CRC polynomial are two different things. Your 10011 is the CRC polynomial from the tutorial, x4+x+1. Your 1101011011 is the message. From the tutorial, the remainder of dividing the message with 0000 appended by the CRC polynomial is 1110. That is the CRC. It is a four-bit CRC, since it uses a fourth-degree polynomial.

Then what you think you should get is the message with the CRC appended, i.e. 11010110111110. That is not the CRC. That is the message with the CRC appended. That message and CRC appended has the property that if you divide it by the CRC polynomial, you will get a remainder of zero. That is what is normally transmitted once the CRC has been computed.

For some reason you are attempting to perform this operation with a CRC routine that uses a different polynomial for a 16-bit CRC (the 0xA001 is for the polynomial x16+x15+x2+1, reversed), feeding the CRC polynomial from the example in the tutorial to where the initial CRC should go in the routine (recall that the CRC and the CRC polynomial are two different things), and providing a 10-bit message as 32 bits. Furthermore you are presenting the message in big-endian order (assuming that you didn't change order of that byte buffer), which feeds a bunch of zeros in first, and you are using a routine that processes eight bits at a time, which makes it impossible to process just ten bits.

You should step away from the CRC routine, and start over with the tutorial.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top