
PNI Sensor Corporation
DOC#1014688 r09.2
TCM User Manual
Page 67
// exit without sending if there is too much data to fit
// inside our packet
if(len > kBufferSize - kPacketMinSize) return;
// 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(U Int32 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--)