/* * SCSProtocol.cpp * SCS串行舵机协议程序 [scs serial servo portocol program] * 日期: 2016.8.25 * 作者: 谭雄乐 [Tam Hung Lok] */ #include #include "SCSProtocol.h" SCSProtocol::SCSProtocol() { Level = 1;//除广播指令所有指令返回应答 [except broadcast instruction, all instructions return reply] End = 1;//舵机处理器与控制板处理器端结构不一致 [servo processor is different from control boardside structure not identical] } //1个16位数拆分为2个8位数 [1 16-bit integer split into 2 8-bit integer] //DataL为低位,DataH为高位 [DataL is low, DataH is High] void SCSProtocol::Host2SCS(u8 *DataL, u8* DataH, int Data) { if(End){ *DataL = (Data>>8); *DataH = (Data&0xff); }else{ *DataH = (Data>>8); *DataL = (Data&0xff); } } //2个8位数组合为1个16位数 [2 8-bit numbers combined into 1 16-bit integer] //DataL为低位,DataH为高位 [DataL is low, DataH is high] int SCSProtocol::SCS2Host(u8 DataL, u8 DataH) { int Data; if(End){ Data = DataL; Data<<=8; Data |= DataH; }else{ Data = DataH; Data<<=8; Data |= DataL; } return Data; } void SCSProtocol::writeBuf(u8 ID, u8 MemAddr, u8 *nDat, u8 nLen, u8 Fun) { u8 msgLen = 2; u8 bBuf[6]; u8 CheckSum = 0; bBuf[0] = 0xff; bBuf[1] = 0xff; bBuf[2] = ID; bBuf[4] = Fun; if(nDat){ msgLen += nLen + 1; bBuf[3] = msgLen; bBuf[5] = MemAddr; writeSCS(bBuf, 6); }else{ bBuf[3] = msgLen; writeSCS(bBuf, 5); } CheckSum = ID + msgLen + Fun + MemAddr; u8 i = 0; if(nDat){ for(i=0; i