User Tools

Site Tools




u16 getCrc (u8* targetArray, u16 length)

targetArray: Array of bytes to compute a CRC for
length: The number of bytes in the array
returns the CRC

Computes a Modbus 16 bit CRC for an array of data targetArray of length bytes. This function is provided should it be necessary to create a custom Modbus frame to send using the RS-232 communication library functions.

u8 txFrame[8];
u16 crc, targetAdr, numberOfCoils;
targetAdr = 0x0000;
numberOfCoils = 1;
txFrame[0] = 1;                        //Slave address 1
txFrame[1] = 1;                        //Function Code 1
txFrame[2] = targetAdr >> 8;           //Target Address MSB
txFrame[3] = targetAdr;                //Target Address LSB
txFrame[4] = numberOfCoils >> 8;       //Length MSB
txFrame[5] = numberOfCoils;            //Length LSB
crc = getCrc(txFrame, 6);              //Compute CRC
txFrame[6] = crc >> 8;                 //CRC MSB
txFrame[7] = crc;                      //CRC LSB


go MOACON home

moacon/getcrc/index.txt · Last modified: 2016/04/14 11:07 (external edit)