
PNI Sensor Corporation
DOC#1014688 r09.2
TCM User Manual
Page 69
UInt16 crc, crcReceived;
// calculated and received crcs.
// Read block will return the number of
// requested (or available) bytes that are in the
// serial objects input buffer.
mSerialPort->ReadBlock(&mInData[2],
mExpectedLen - 2);
// in CRC verification, don't include the CRC in the recalculation
(-2)
crc = CRC(mInData, mExpectedLen - 2);
// CRC is also ALWAYS transmitted in big endian
crcReceived = (mInData[mExpectedLen - 2] <<
8) | mInData[mExpectedLen - 1] ;
if(crc == crcReceived)
{
// the crc is correct, so pass the frame up for processing.
if(mHandler) mHandler-
>HandleComm(mInData[2], &mInData[3], mExpectedLen - kPacketMinSize);
}
else
{
// crc's don't match so clear everything that is currently in the
// input buffer since the data is not reliable.
mSerialPort->InClear();
}
// go back to looking for the length bytes.
mStep = 1 ;
}
else
{
// Ticks is a timer function. 1 tick = 10msec.
if(Ticks() > mTime)
{
// Corrupted message. We did not get the length we were
// expecting within 1/2sec of receiving the length bytes. Clear
// everything in the input buffer since the data is unreliable
mSerialPort->InClear();
mStep = 1 ;
// Look for the next length bytes
}
}
break ;
}
default:
break ;
}
}