Files
checker_host/interface/codec_m4.cpp
2023-11-26 23:05:35 +08:00

92 lines
1.5 KiB
C++

#include "codec_m4.h"
#include "QDebug"
bool codec_m4::recvByte(int byte)
{
bool ack = false;
if (fullFrame == true)
{
return fullFrame;
}
data.append(byte);
switch (data.size()) {
case 1:
if(uint8_t(data[0])==0xff)
{
num_to_read=2;
}
else
{
data.remove(0,1);
num_to_read=0;
}
break;
case 2:
{
int len=data[1];
num_to_read=len+2;
}
break;
default:
break;
}
// 此时一帧数据已完成
if(num_to_read>0&&num_to_read==data.size())
{
data.remove(0,2);
qDebug()<<"recv_m4:"<<QString(data)<<endl;
ack=true;
num_to_read=0;
}
fullFrame=ack;
return fullFrame;
}
bool codec_m4::packCheck(myarray data)
{
if(data[0]!=0xff){
return false;
}
if(data.size()>=2&&(data.size()==(data[1]+2))){
return true;
}
return false;
}
myarray codec_m4::encode(int srcAddr, int dstAddr, int cmd, myarray data)
{
myarray r;
r.append(0xff);
r.append(char(data.size()));
r.append(data);
return r;
}
myarray codec_m4::decode(int &srcAddr, int &dstAddr, int &cmd, myarray data)
{
if(packCheck(data)){
return myarray(data.mid(2));
}
}
Codec *codec_get()
{
static Codec *codec_=nullptr;
if(codec_==nullptr){
codec_=new codec_m4();
}
return codec_;
}
codec_export(codec_m4,codec_get);