PNI TCM 5 User Manual
Page 51
PNI Sensor Corporation
Doc #1007537 r12
TCM3 & TCM5 User Manual
– Sept 2012
Page 51
// Store the total len of the packet including the len bytes (2), the frame ID (1),
// the data (len), and the crc (2). If no data is sent, the min len is 5
mOutData[index++] = count >>
8
;
mOutData[index++] = count &
0xFF
;
// store the frame ID
mOutData[index++] = frameType ;
// copy the data to be sent
while
(len--) mOutData[index++] = *data++;
// compute and add the crc
crc = CRC(mOutData, index);
mOutData[index++] = crc >>
8
;
mOutData[index++] = crc &
0xFF
;
// Write block will copy and send the data out the serial port
mSerialPort->WriteBlock(mOutData, index);
}
//
// Call the functions in serial port necessary to change the baud rate
//
void
CommProtocol::SetBaud(UInt32 baud)
{
mSerialPort->SetBaudRate(baud);
mSerialPort->InClear();
// clear any data that was already waiting in the
buffer
}
//
// Update the CRC for transmitted and received data using the CCITT 16bit algorithm (X^16 + X^12
+ X^5 + 1).
//
UInt16 CommProtocol::CRC(
void
* data, UInt32 len)
{
UInt8 * dataPtr = (UInt8 *)data;
UInt32 index =
0
;
UInt16 crc =
0
;
while
(len--)
{
crc = (
unsigned
char
)(crc >>
8
) | (crc <<
8
);
crc ^= dataPtr[index++];
crc ^= (
unsigned
char
)(crc &
0xff
) >>
4
;