Claudi Claudi - 1 month ago 18
C Question

Convert CRC calculation from C# to C

I am currently working on a serial monitor, to ensure some data integrity I am trying to implement a CRC8 checksum, below is the calculation i do on any messages before i send them.

public byte Checksum(params byte[] val)
{
if (val == null)
throw new ArgumentNullException("val");
byte c = 0;
foreach (byte b in val)
{
c = table[c ^ b];
}
return c;
}


I generate a table using 0xD8:

public byte[] GenerateTable(CRC8_POLY polynomial)
{
byte[] csTable = new byte[256];
for (int i = 0; i < 256; ++i)
{
int curr = i;
for (int j = 0; j < 8; ++j)
{
if ((curr & 0x80) != 0)
{
curr = (curr << 1) ^ (int)polynomial;
}
else
{
curr <<= 1;
}
}
csTable[i] = (byte)curr;
}
return csTable;
}


This is a code i have used for testing the setup:

private void btnSend_Click(object sender, EventArgs e)
{
ProtoFrame rxFrame = new ProtoFrame();
if (cboParam.Text == "test")
{
rxFrame.Start = 0x73;
rxFrame.Size = 9;
rxFrame.Command = 01;
rxFrame.Unused = 0;
rxFrame.ParamId = 0x0100;
rxFrame.Param = 8000;
}

byte[] rxBuffer = getBytes(rxFrame); //call to byte array formatter
rxBuffer[rxBuffer.Length-1] = Checksum(rxBuffer); //append crc at end of array
ComPort.Write(rxBuffer, 0, rxBuffer.Length);
}
static byte[] getBytes(object str) //input struct
{
int size = Marshal.SizeOf(str) + 1;
byte[] arr = new byte[size];
IntPtr ptr = Marshal.AllocHGlobal(size);

Marshal.StructureToPtr(str, ptr, true);
Marshal.Copy(ptr, arr, 0, size);
Marshal.FreeHGlobal(ptr);

return arr;
}


As far as i know this code work as intendended, and im using the table generator to implement a hardcoded table in my microcontroller, to speed up the process.

What i dont quite get is how i implement a function to calculate the CRC in a similar way as i do here.
Any help or guides in the right direction is aprreciated.
So far i have come up with this function:

uint8_t crc8(uint8_t *crc)
{
uint8_t crcVal;
int m;
for (m = 0; m < PacketSize ;m++ )startbyte
{
*crc = crc8_table[(*crc) ^ m];
*crc &= 0xFF;
}
}


where table is:

uint8_t crc8_table[256] = {0,24,48,40,96,120,80,72,192,216,240,232,160,184,144,136,88,64,104,112,56,32,8,16,
152,128,168,176,248,224,200,208,176,168,128,152,208,200,224,248,112,104,64,88,16,8,
32,56,232,240,216,192,136,144,184,160,40,48,24,0,72,80,120,96,184,160,136,144,216,
192,232,240,120,96,72,80,24,0,40,48,224,248,208,200,128,152,176,168,32,56,16,8,64,
88,112,104,8,16,56,32,104,112,88,64,200,208,248,224,168,176,152,128,80,72,96,120,48,
40,0,24,144,136,160,184,240,232,192,216,168,176,152,128,200,208,248,224,104,112,88,
64,8,16,56,32,240,232,192,216,144,136,160,184,48,40,0,24,80,72,96,120,24,0,40,48,120,
96,72,80,216,192,232,240,184,160,136,144,64,88,112,104,32,56,16,8,128,152,176,168,224,
248,208,200,16,8,32,56,112,104,64,88,208,200,224,248,176,168,128,152,72,80,120,96,40,
48,24,0,136,144,184,160,232,240,216,192,160,184,144,136,192,216,240,232,96,120,80,72,
0,24,48,40,248,224,200,208,152,128,168,176,56,32,8,16,88,64,104,112
};


and PacketSize is found from rxFrame.Size

Answer

So you simply need to port your C# function to C

public byte Checksum(params byte[] val)
{
    if (val == null)
        throw new ArgumentNullException("val");
    byte c = 0;
    foreach (byte b in val)
    {
        c = table[c ^ b];
    }
    return c;
}

I. there are no exceptions in C. Use return value to indicate errors and add an argument that you'll use as a return value. It's up to you to decide whether to pass a message length as a parameter or leave it at the global scope:

int checksum(uint8_t const *msg, size_t msglen, uint8_t *result)

II. foreach loop is converted to for loop, where i is index and msg[i] is the b from the foreach:

int checksum(uint8_t const *msg, size_t msglen, uint8_t *result)
{
    if (msg == NULL || msglen == 0)
        return 0;

    uint8_t crc = 0;
    for (int i = 0; i < msglen; i++)
    {
        crc = table[crc ^ msg[i]];
    }

III. Store the result and return a success code:

int checksum(uint8_t const *msg, size_t msglen, uint8_t *result)
{
    if (msg == NULL || msglen == 0)
        return 0;

    uint8_t crc = 0;
    for (int i = 0; i < msglen; i++)
    {
        crc = table[crc ^ msg[i]];
    }

    *result = crc;
    return 1;
}

IV. Usage:

uint8_t crc;
if (!checksum(message, PacketSize, &crc))
    report_error();