Manual

PNI Sensor Corporation DOC#1014688 r06
TCM User Manual July 2013 Page 66
swi t ch( mSt ep)
{
case 1:
{
/ / wai t f or l engt h byt es t o be r ecei ved by t he ser i al obj ect
i f ( i nLen >= 2)
{
/ / Read bl ock wi l l r et ur n t he number of r equest ed ( or avai l abl e)
// byt es t hat ar e i n t he ser i al obj ect s i nput buf f er .
/ / r ead t he byt e count
mSer i al Por t - >ReadBl ock( mI nDat a, 2) ;
/ / byt e count i s ALWAYS t r ansmi t t ed i n bi g endi an, copy byt e
// count t o mExpect edLen t o nat i ve endi aness
mExpect edLen = ( mI nDat a[ 0] << 8) |
mI nDat a[ 1] ;
/ / Ti cks i s a t i mer f unct i on. 1 t i ck = 10msec.
/ / wai t up t o 1/ 2s f or t he compl et e f r ame ( mExpect edLen) t o be
// r ecei ved
mTi me = Ti cks( ) + 50 ;
mSt ep++ ;
/ / got o t he next st ep i n t he pr ocess
}
br eak ;
}
case 2:
{
/ / wai t f or msg compl et e or t i meout
i f ( i nLen >= mExpect edLen - 2)
{
UI nt 16 cr c, cr cRecei ved;
/ / cal cul at ed and r ecei ved cr cs.
/ / Read bl ock wi l l r et ur n t he number of
// r equest ed ( or avai l abl e) byt es t hat ar e i n t he
/ / ser i al obj ect s i nput buf f er .
mSer i al Por t - >ReadBl ock( &mI nDat a[ 2] ,
mExpect edLen - 2) ;
/ / i n CRC ver i f i cat i on, don' t i ncl ude t he CRC i n t he r ecal cul at i on
( - 2)
cr c = CRC( mI nDat a, mExpect edLen - 2) ;
/ / CRC i s al so ALWAYS t r ansmi t t ed i n bi g endi an
cr cRecei ved = ( mI nDat a[ mExpect edLen - 2] <<
8) | mI nDat a[ mExpect edLen - 1] ;
i f ( cr c == cr cRecei ved)
{
/ / t he cr c i s cor r ect , so pass t he f r ame up f or pr ocessi ng.
i f ( mHandl er ) mHandl er -
>Handl eComm( mI nDat a[ 2] , &mI nDat a[ 3] , mExpect edLen - kPacket Mi nSi ze) ;
}
el se
{
/ / cr c' s don' t mat ch so cl ear ever yt hi ng t hat i s cur r ent l y i n t he
// i nput buf f er si nce t he dat a i s not r el i abl e.
mSer i al Por t - >I nCl ear ( ) ;
}