The problem in how you hare handling the length of the data buffer. When you retrieve the CRC of the buffer from plData[lLength-1]
the length is interpreted as the number of elements in the array. Later when you iterate over the buffer the length is assumed to be the number of bytes in the buffer.
If lLength
represents the number of bytes you will need to compensate for this when retrieving the existing CRC value from the buffer. You will also need to adjust the size of the buffer so that the existing CRC itself is not included when you iterate over the buffer.
uint32_t lMsgCRC = *((const uint32_t*)((const char*)plData + lLength) - 1);
lLength -= sizeof(plData[0]);
If lLength
represents the number of elements in the array rather than the number of bytes you will need to adjust the size. Again you need to take into consideration that the existing CRC is at the end of the buffer.
// Adjust length by size of elements
lLength = (lLength - sizeof(plData[0])) * sizeof(plData[0]);
while (lLength--)
{
lCalcCRC = (lCalcCRC >> 8) ^ crc_table[(lCalcCRC & 0xFF) ^ *current++];
}