更新部分函数
更新parseRS232逻辑,支持ASCII与HEX
This commit is contained in:
parent
3d9fb6a6bc
commit
cee3c8ae2d
@ -271,7 +271,9 @@ static int getAndCheckLoc(uint8_t *dout, size_t pairCnt)
|
||||
cnt = getLoc(loc,pairCnt);
|
||||
if (i++ > 20) {
|
||||
LOG_E("位置信息获取异常");
|
||||
break;
|
||||
// break;
|
||||
memset(dout,0x37,pairCnt*10);
|
||||
return pairCnt*10;
|
||||
}
|
||||
}
|
||||
memcpy(dout,loc,cnt);
|
||||
@ -285,12 +287,20 @@ static int getAndCheckLoc(uint8_t *dout, size_t pairCnt)
|
||||
*/
|
||||
int reportDepth(uint8_t *din,size_t len)
|
||||
{
|
||||
uint8_t rst[len];
|
||||
memcpy(rst,din,len);
|
||||
// 000000000000000000005AA53E320608000A170407101B33FFFFFFFF24ED
|
||||
|
||||
getAndCheckLoc(rst+0,1);//有10个位置的空余字节,尚不清楚定义
|
||||
upSend(rst, len);
|
||||
//获取并更新位置信息
|
||||
uint8_t loc[10];
|
||||
uint8_t dout[200];
|
||||
size_t nlen = getAndCheckLoc(dout, 1);
|
||||
|
||||
rt_memcpy(dout+10, din, len);//5aa5从第11字节开始,共len个字节
|
||||
LOG_HEX("depth",16,dout,len+10);
|
||||
//加密。因加密后数据长度会变化,故不能只加密位置数据。
|
||||
nlen = cryptLocMsg(din, len+10, dout);
|
||||
LOG_HEX("crypt",16,dout,nlen);
|
||||
LOG_D("位置数据加密完成");
|
||||
upSend(dout, nlen);
|
||||
}
|
||||
//天通指令类型:0x70 0x01
|
||||
|
||||
@ -337,7 +347,7 @@ static void d_packLocMsg(void)
|
||||
* @param dout 存储加密结果的数组
|
||||
* @return 加密后的长度
|
||||
*/
|
||||
static int cryptLocMsg(uint8_t *din, size_t len, uint8_t *dout)
|
||||
int cryptLocMsg(uint8_t *din, size_t len, uint8_t *dout)
|
||||
{ //加密。因加密后数据长度会变化,故不能只加密位置数据。
|
||||
uint8_t cd[200]={0xAB,0xAB};
|
||||
size_t nlen = 0;
|
||||
@ -373,19 +383,20 @@ int packAndSendLoc()
|
||||
* @param dout
|
||||
* @return
|
||||
*/
|
||||
int packDepthMsg(uint8_t *din, int len)
|
||||
int depthAlert(uint8_t *din, int len)
|
||||
{
|
||||
//获取并更新位置信息
|
||||
uint8_t loc[10];
|
||||
uint8_t dout[200];
|
||||
size_t nlen = getAndCheckLoc(loc, 1);
|
||||
|
||||
rt_memcpy(din+0, loc, nlen);//位置数据从【】字节开始,共len个字节
|
||||
LOG_HEX("depth",16,din,len);
|
||||
//加密。因加密后数据长度会变化,故不能只加密位置数据。
|
||||
nlen = cryptLocMsg(din, len, dout);
|
||||
LOG_D("位置数据加密完成");
|
||||
upSend(dout, nlen);
|
||||
// uint8_t loc[10];
|
||||
// uint8_t dout[200];
|
||||
// size_t nlen = getAndCheckLoc(dout, 1);
|
||||
//
|
||||
// rt_memcpy(dout+10, din, len);//5aa5从第11字节开始,共len个字节
|
||||
// LOG_HEX("depth",16,dout,len+10);
|
||||
// //加密。因加密后数据长度会变化,故不能只加密位置数据。
|
||||
// nlen = cryptLocMsg(din, len+10, dout);
|
||||
// LOG_HEX("crypt",16,dout,nlen);
|
||||
// LOG_D("位置数据加密完成");
|
||||
// upSend(dout, nlen);
|
||||
|
||||
}
|
||||
|
||||
@ -507,7 +518,7 @@ void ttRunCMD(uint8_t *din, size_t len)
|
||||
clearAllData();
|
||||
break;
|
||||
default:
|
||||
LOG_W("未支持的指令。");
|
||||
LOG_W("0x%04X=未支持的指令。",cmd);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -599,7 +610,7 @@ void parse3SData(uint8_t *din, size_t count)
|
||||
switch (cmd)
|
||||
{
|
||||
case _CMD_DEPTH_REQUEST:
|
||||
packDepthMsg(din, count);
|
||||
reportDepth(din, count);
|
||||
break;
|
||||
default:
|
||||
cacheData(din, count);
|
||||
@ -755,46 +766,67 @@ void d_cacheData()
|
||||
void parseRS232(uint8_t *din, size_t len)
|
||||
{
|
||||
//有HEX有ASCII,统一按HEX解析
|
||||
uint8_t head[]={0x41, 0x54, 0x2B, 0x53, 0x4E, 0x44, 0x20 };//"AT+SND "
|
||||
uint8_t ndin[400];
|
||||
//如果有无"AT+SND "头的数据混发则难以处理粘包,如果所有数据均以"AT+SND "开头则可以按以下方法(未完全实现)处理
|
||||
//是否存在混发需要核实
|
||||
#ifdef _MIX_DATA
|
||||
uint8_t index[10];
|
||||
size_t n=isInByte(din, len, head, sizeof(head), index);
|
||||
//部分数据以10字节(20字符)的0数据开始,如深度查询,心跳包等
|
||||
//处理思路是先不管前导0,对应指令手动加前导数据
|
||||
uint8_t asciiHead[]={0x41, 0x54, 0x2B, 0x53, 0x4E, 0x44,};//"AT+SND"
|
||||
uint8_t hexHead[]={0x5a, 0xa5};//"5AA5"
|
||||
//由于帧头有多种,且ascii和hex混发,无法处理粘包
|
||||
|
||||
if (!n) {
|
||||
LOG_W("无匹配数据");
|
||||
return;
|
||||
}
|
||||
for (size_t i = 0; i < n; i++)
|
||||
{
|
||||
//按帧头分割
|
||||
int cnt=(i+1<n)?index[i+1]-index[i]:len-index[i];
|
||||
memcpy(ndin,din+index[i],cnt);
|
||||
LOG_HEX("frame",16,ndin,cnt);
|
||||
#endif
|
||||
|
||||
#ifndef _MIX_DATA
|
||||
int cnt=len;
|
||||
memcpy(ndin,din,len);
|
||||
#endif
|
||||
//parse
|
||||
size_t offset = 0;
|
||||
if (memcmp(ndin,head,sizeof(head)) == 0) {
|
||||
//有“AT+SND ”帧头,需要处理
|
||||
offset = sizeof(head);
|
||||
//check
|
||||
if (memcmp(din,asciiHead,sizeof(asciiHead)) == 0) {
|
||||
//ascii
|
||||
LOG_I("type = ASCII");
|
||||
uint8_t index[10];
|
||||
uint8_t tmpHead[]={0x35, 0x41, 0x41, 0x35};
|
||||
size_t n=isInByte(din, len, tmpHead, sizeof(tmpHead), index);
|
||||
// LOG_D("n=%d",n);
|
||||
if (!n) {
|
||||
LOG_W("无匹配数据");
|
||||
return;
|
||||
}
|
||||
for (size_t i = 0; i < n; i++)
|
||||
{
|
||||
//按帧头分割
|
||||
uint8_t ndin[400];
|
||||
int cnt=(i+1<n)?index[i+1]-index[i]:len-index[i];
|
||||
memcpy(ndin,din+index[i],cnt);
|
||||
ndin[cnt]='\0';
|
||||
uint8_t tmp[200];
|
||||
size_t ncnt = str2Byte(ndin, 2, 16, tmp);
|
||||
LOG_HEX("frame",16,tmp,ncnt);
|
||||
if (chk3SDataValid(tmp, ncnt) != RT_EOK) {
|
||||
return;
|
||||
}
|
||||
parse3SData(tmp,ncnt);
|
||||
}
|
||||
}
|
||||
ndin[cnt]='\0';
|
||||
size_t ncnt = str2Byte(ndin+offset, 2, 16, ndin);
|
||||
LOG_HEX("decode",16,ndin,ncnt);
|
||||
if (chk3SDataValid(ndin, ncnt) != RT_EOK) {
|
||||
return;
|
||||
else//如果不是ASCII则统一按HEX计// if (memcmp(din,hexHead,sizeof(hexHead)) == 0)
|
||||
{
|
||||
//hex
|
||||
LOG_I("type = HEX");
|
||||
|
||||
uint8_t index[10];
|
||||
size_t n=isInByte(din, len, hexHead, sizeof(hexHead), index);
|
||||
if (!n) {
|
||||
LOG_W("无匹配数据");
|
||||
return;
|
||||
}
|
||||
for (size_t i = 0; i < n; i++)
|
||||
{
|
||||
//按帧头分割
|
||||
uint8_t ndin[200];
|
||||
int cnt=(i+1<n)?index[i+1]-index[i]:len-index[i];
|
||||
memcpy(ndin,din+index[i],cnt);
|
||||
LOG_HEX("frame",16,ndin,cnt);
|
||||
if (chk3SDataValid(ndin, cnt) != RT_EOK) {
|
||||
return;
|
||||
}
|
||||
parse3SData(ndin,cnt);
|
||||
}
|
||||
}
|
||||
parse3SData(ndin,ncnt);
|
||||
#ifdef _MIX_DATA
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -30,7 +30,7 @@
|
||||
#define SAMPLE_UART_NAME "uart3" /* 串口设备名称 */
|
||||
#endif
|
||||
#define MAX_SIZE_TO_SAVE 1024*2
|
||||
#define MIN_FRAME_LEN 27
|
||||
#define MIN_FRAME_LEN 11
|
||||
/* 串口接收消息结构*/
|
||||
struct rx_msg
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user