tools.c update bytes2str()

数据解析同意放入 func.c 中有parseTTData() 判断
更新parseTTData函数,添加对粘包数据的支持
This commit is contained in:
CSSC-WORK\murmur 2023-07-20 16:18:42 +08:00
parent e8a8606bf3
commit 656fde2902
4 changed files with 63 additions and 35 deletions

View File

@ -244,7 +244,7 @@ void iniCnt()
LOG_W("TT server is not ready."); LOG_W("TT server is not ready.");
tcpInit(); tcpInit();
LOG_D("s=%d",isTCPok()); LOG_D("s=%d",isTCPok());
rt_thread_mdelay(5000); // rt_thread_mdelay(5000);
} }
{ {
LOG_D("TT server is ready."); LOG_D("TT server is ready.");

View File

@ -114,7 +114,7 @@ RT_WEAK int upSend(uint8_t *din, size_t len)
*/ */
void selfTest() void selfTest()
{ {
LOG_D("FUNC = selftest"); LOG_I("FUNC = selftest");
rt_uint8_t rst[100]={0x5A, 0xA5, 0x32, 0x3E, 0x0A, 0x41}; rt_uint8_t rst[100]={0x5A, 0xA5, 0x32, 0x3E, 0x0A, 0x41};
int p = 6; int p = 6;
rt_uint8_t sysSta=1,xh=0,jh=0,commSpeed=0; rt_uint8_t sysSta=1,xh=0,jh=0,commSpeed=0;
@ -581,6 +581,18 @@ void parse3SData(uint8_t *din, size_t count)
} }
} }
void chkACK(uint8_t *msg, size_t size)
{
rt_uint8_t ackgood[] = { 0x88, 0xAA, 0xBB, 0x88, 0x41, 0x43, 0x4B }; //前四字节=帧头、后三字节=ACK
if (rt_memcmp(msg, ackgood, 4) == 0 && rt_memcmp(msg + size - 3, ackgood + 4, 3) == 0)
{
LOG_I("data is ACK.");
}
else {
LOG_W("NONE FUNCTION MATCH.");
}
}
/** /**
* @brief TT数据TT收到的指令必是单指令 * @brief TT数据TT收到的指令必是单指令
* *
@ -594,21 +606,33 @@ void parseTTData(uint8_t *din, size_t len)
* | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | * | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
* +---------------------+-----------+-----------+-----------+-----------+---------+---------------------+------------+ * +---------------------+-----------+-----------+-----------+-----------+---------+---------------------+------------+
* | fstart[4] | fnum[2] | bak[2] | ftype[2] | fdlen[2] | fcrc[1] | ftccid[4] | rawData[N] | * | fstart[4] | fnum[2] | bak[2] | ftype[2] | fdlen[2] | fcrc[1] | ftccid[4] | rawData[N] |
* | 0x88,0xAA,0xBB,0x88 | 0x00,0x01 | 0x00,0x22 | 0x70,0x21 | 0x00,0xAA | 0x00 | 0x27,0x22,0x22,0x22 | | * | 0x88,0xAA,0xBB,0x88 | 0x00,0x01 | 0x00,0x00 | 0x70,0x21 | 0x00,0xAA | 0x00 | 0x27,0x22,0x22,0x22 | |
* +---------------------+-----------+-----------+-----------+-----------+---------+---------------------+------------+ * +---------------------+-----------+-----------+-----------+-----------+---------+---------------------+------------+
*/ */
uint8_t head[]={0x88,0xAA,0xBB,0x88, 0x00,0x01, 0x00,0x22, 0x70,0x21, 0x00,0xaa, 0x00, 0x27,0x22,0x22,0x22 }; uint8_t head[]={0x88,0xAA,0xBB,0x88, 0x00,0x01, 0x00,0x00, 0x70,0x21, 0x00,0xaa, 0x00, 0x27,0x22,0x22,0x22 };
//fstart[4] fnum[2]  bak[2]  ftype[2]  fdlen[2]  fcrc[1] ftccid[4] //fstart[4] fnum[2]  bak[2]  ftype[2]  fdlen[2]  fcrc[1] ftccid[4]
// uint8_t rst[10]; uint8_t rst[10];
// size_t n=isInByte(din, len, head, 10, rst); size_t n=isInByte(din, len, head, 10, rst);
// uint8_t ndin[200]; uint8_t ndin[200];
// for (size_t i = 0; i < n; i++) if (!n) {
// { LOG_W("无匹配数据");
// int n=rst[i+1] return;
// memcpy(ndin,din,rst[i+1]-rst[i]); }
// } for (size_t i = 0; i < n; i++)
{
//按帧头分割
int cnt=(i+1<n)?rst[i+1]-rst[i]:len-rst[i];
memcpy(ndin,din+rst[i],cnt);
LOG_HEX("frame",16,ndin,cnt);
uint8_t rst = memcmp(din,head,10);//只比较到ftype //判断是否为ACK
if (ndin[cnt-1] != 0xed) {
chkACK(ndin, cnt);
}
//数据
else
{
uint8_t rst = memcmp(ndin,head,10);//只比较到ftype
if (rst) if (rst)
{ {
LOG_W("帧头不匹配"); LOG_W("帧头不匹配");
@ -616,17 +640,18 @@ void parseTTData(uint8_t *din, size_t len)
} }
uint8_t id[30]=""; uint8_t id[30]="";
LOG_D("get new data: id=\"%s\", cur/all=[%d/%d]",bytes2str(din+17,7,10,"_",id),din[25],din[26]); LOG_I("data info: id=\"%s\", cur/all=[%d/%d]",bytes2str(ndin+17,7,10,"_",id),ndin[25],ndin[26]);
if (din[24] >> 7) // fcfg=数据类型。解析TT收到的数据时仅需解析“命令”“数据”传输是单向的。 if (ndin[24] >> 7) // fcfg=数据类型。解析TT收到的数据时仅需解析“命令”“数据”传输是单向的。
{ {
LOG_W("浮标端仅接受指令,暂不支持数据。"); LOG_W("浮标端仅接受指令,暂不支持数据。");
return; return;
} }
uint8_t rawData[200]; uint8_t rawData[200];
uint8_t rawDataLen=len-27; uint8_t rawDataLen=cnt-27;
memcpy(rawData, din + 27, rawDataLen); memcpy(rawData, ndin + 27, rawDataLen);
parse3SData(rawData,rawDataLen); parse3SData(rawData,rawDataLen);
}
}
// switch (din[24] & 0x7F) // 识别指令的目标地址 // switch (din[24] & 0x7F) // 识别指令的目标地址
// { // {

View File

@ -243,7 +243,9 @@ char *bytes2str(uint8_t *din, size_t count, int radix, char *sep, char *str)
strcat(rst,sep); strcat(rst,sep);
// printf("rst=%s,s=%s\n",rst,s); // printf("rst=%s,s=%s\n",rst,s);
} }
strncpy(str,rst,strlen(rst)-strlen(sep));//去掉末尾的连接符 int len = strlen(rst)-strlen(sep);
strncpy(str,rst,len);//去掉末尾的连接符
str[len]='\0';
return str; return str;
} }

View File

@ -544,15 +544,16 @@ void recTT_thread_entry()
{ //收到数据长度为0表示tcp断开 { //收到数据长度为0表示tcp断开
break; break;
} }
LOG_D("%d Bytes received from TT",msg.size); LOG_I("%d Bytes received from TT",msg.size);
LOG_HEX("TTrec", 16, msg.data, msg.size); LOG_HEX("TTrec", 16, msg.data, msg.size);
//此处处理接收到数据 //此处处理接收到数据
rt_uint8_t rec_good[] = { 0x88, 0xAA, 0xBB, 0x88, 0x41,0x43,0x4B }; //前四字节=帧头、后三字节=ACK // rt_uint8_t rec_good[] = { 0x88, 0xAA, 0xBB, 0x88, 0x41,0x43,0x4B }; //前四字节=帧头、后三字节=ACK
if (rt_memcmp(msg.data, rec_good, 4) == 0 && rt_memcmp(msg.data+msg.size-3, rec_good+4, 3) == 0) // if (rt_memcmp(msg.data, rec_good, 4) == 0 && rt_memcmp(msg.data+msg.size-3, rec_good+4, 3) == 0)
// {
// LOG_I("ack is good.");
// }
// else
{ {
LOG_I("ack is good.");
}
else {
LOG_D("try to parse data."); LOG_D("try to parse data.");
parseTTData(msg.data,msg.size); parseTTData(msg.data,msg.size);
} }