Manual

PNI Sensor Corporation DOC#1014688 r06
TCM User Manual July 2013 Page 64
7.4.3 CommProtocol.cpp File
#i ncl ude " CommPr ot ocol . h"
/ / i mpor t an obj ect t hat wi l l pr ovi de a 10mSec t i ck count t hr ough
// a f unct i on cal l ed Ti cks( )
#i ncl ude " Ti ckGener at or . h"
/ / Ser Por t i s an obj ect t hat cont r ol s t he physi cal ser i al
// i nt er f ace. I t handl es sendi ng out
/ / t he char act er s, and buf f er s t he char act er s r ead i n unt i l
// we ar e r eady f or t hem.
//
CommPr ot ocol : : CommPr ot ocol ( CommHandl er * handl er , Ser Por t * ser Por t )
: Pr ocess( " CommPr ot ocol " )
{
mHandl er = handl er ;
/ / st or e t he obj ect t hat wi l l par se t he dat a when i t i s f ul l y
// r ecei ved
mSer i al Por t = ser Por t ;
I ni t ( );
}
/ / I ni t i al i ze t he ser i al por t and var i abl es t hat wi l l cont r ol
// t hi s pr ocess
voi d CommPr ot ocol : : I ni t ( UI nt 32 baud)
{
Set Baud( baud) ;
mOl dI nLen = 0;
/ / no dat a pr evi ousl y r ecei ved
mSt ep = 1;
/ / got o t he f i r st st ep of our pr ocess
}
//
/ / Put t oget her t he f r ame t o send t o t he modul e
//
voi d CommPr ot ocol : : SendDat a( UI nt 8 f r ameType, voi d * dat aPt r , UI nt 32
l en)
{
UI nt 8 * dat a = ( UI nt 8 * ) dat aPt r ; / / t he dat a t o send
UI nt 32 i ndex = 0;
/ / our l ocat i on i n t he f r ame we ar e put t i ng t oget her
UI nt 16 cr c;
/ / t he CRC t o add t o t he end of t he packet
UI nt 16 count ;
/ / t he t ot al l engt h t he packet wi l l be
count = ( UI nt 16) l en + kPacket Mi nSi ze;
/ / exi t wi t hout sendi ng i f t her e i s t oo much dat a t o f i t
// i nsi de our packet
i f ( l en > kBuf f er Si ze - kPacket Mi nSi ze) r et ur n;
/ / St or e t he t ot al l en of t he packet i ncl udi ng t he l en byt es
// ( 2) , t he f r ame I D ( 1) ,
/ / t he dat a ( l en) , and t he cr c ( 2) . I f no dat a i s sent , t he
// mi n l en i s 5