我是靠谱客的博主 香蕉酒窝,最近开发中收集的这篇文章主要介绍遇到的问题(一):智能家居中WIFI模块设备在接收底层家具设备串口数据时由于断帧而出现的指令丢失的情况处理,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">今天在工作中遇到一个问题:</span>
之前的WIFI模块在处理家居设备发起的数据原理是按帧接收串口数据,然后从该帧中根据不同的设备协议摘取相对应的模块指令,在处理较短帧数据的时候基本没有出现问题,但是 在接受较长帧数据的时候,出现了断帧引起的指令丢失的情况,比如说一条包含很长数据的指令,在前一帧只传递了包含包头、命令、一部分的数据,后一帧传来了其余数据包括校验数据的情况。造成这种情况的发生,本人预计可能由于以下情况:
(1)由于串口自动成帧模式引发的问题:自动成帧模式可根据需要设置根据字节长度还是时间设置自动成帧模式,无论哪一种模式都可能造成,指令在发送的过程中出现断帧发送的情况(一个指令从两条或者三条帧来发送)
(2)间断性的接触不良,串口发送的数据底层硬件基础是两条全双工的RX TX总线构成,间断性的接触不良可能也会导致这个原因,这个是同事提出的疑惑,但是个人觉得这个理由值得商榷。
解决方法:
之前的额处理模式是只从一帧数据中摘取指令,现在要做到摘取过程中要存在“记忆性”,就是遇到无效的字节,要把它记下来,等到下一个指令出现时,将之前的无效指令合集进行相同规则的检查,看看有没有存在完成指令的情况,若有,就将其放到指令队列里面,否则就清空这个无效指令。代码处理如下:
之前的由串口回调处理情况:
static void USER_FUNC preDealTheUartData_Tcl_Air_Condition(uint8_t *data, uint32_t len)
{
if(data == NULL || len <= 0)
{
u_printf("FUNCTION[%s] data == NULL||len[%d]!!!,so ignore it!!!!n", __FUNCTION__, len);
return;
}
static int cmdBegin = 0;
<span style="white-space:pre"> </span>static uint8_t curPacket[ONE_PACKET_LEN] = {0};
static int curPacketIndex = 0;
static uint8_t cOtherBytes[100] = {0};
int otherindex = 0;
int datalength = 0;
uint8_t verify_data= 0;
for(int i=0; i<len; ++i)
{
if(cmdBegin == 1)
{
if(curPacketIndex < ONE_PACKET_LEN)
curPacket[curPacketIndex++] = data[i];
if(i<datalength-1)
continue;
else //成功获取到一条数据
{
if(g_uartCMDNum >= PACKET_NUM)
{
u_printf("FUNCTION[%s] now we has[%d]cmd not execute, so this cmd ignore!!!!n", __FUNCTION__, g_uartCMDNum);
}
else
{
//进行数据校验
verify_data = BBCverify(curPacket, curPacketIndex-1);
if(-1 == verify_data)
{
return; //校验函数返回错误,退出
}
if(curPacketIndex > 2)
{
if(verify_data == curPacket[curPacketIndex-1])
{
memcpy(g_packets[g_uartCMDNum], curPacket, sizeof(g_packets[g_uartCMDNum]));//放到指令包里面
<span style="white-space:pre"> </span>g_uartCMDNum++;
}
else
{
u_printf("error:verify data error!!rn");
}
}
}
memset(curPacket, 0x0, sizeof(curPacket));
<span style="white-space:pre"> </span>curPacketIndex = 0;
<span style="white-space:pre"> </span>cmdBegin = 0;
datalength = 0;
}
}
else
{
if(data[i] == 0xbb)<span style="white-space:pre"> </span>//开启一帧数据的标志
{
cmdBegin = 1;
curPacket[curPacketIndex++] = data[i];
datalength = i+data[i+1]+5;
}
else
{
if(otherindex < OTHER_BYTES_LEN)
cOtherBytes[otherindex++] = data[i];
}
}
}
if(otherindex > 0)
{
uint8_t cbase64[150] = {0};
int cbase64len = 0;
drm_Base64Encode(cOtherBytes, otherindex, cbase64, &cbase64len);
otherindex = 0;
u_printf("FUNCTION[%s] some data not in CMD format[%s],so ignore it!!!!n", __FUNCTION__, cbase64);
}
}
void USER_FUNC preDealTheUartData(uint8_t *data, uint32_t len)
{
if(data == NULL || len <= 0)
{
u_printf("FUNCTION[%s] data == NULL||len[%d]!!!,so ignore it!!!!n", __FUNCTION__, len);
return;
}
static uint8_t curPacket[ONE_PACKET_LEN] = {0};
static uint8_t cOtherBytes[200] = {0}; // 杂乱数据记录,帧的分包重新拼接回
static int otherindex = 0;
int packetLen = 0;
int packetLen2 = 0;
uint8_t cbase64[300] = {0}; //将串口数据进行base64编码后方便打印日志查问题
int cbase64len = 0;
int i = 0;
for(i = 0; i < len; i++)
{
packetLen = isValiadPacket(data, i, len); //判断从该数据开始的字节能否凑成一个完整的命令,如果可以就放到指令包,并且跳过这么多长度的数据
if(packetLen > 0)
{
//遇到正常帧数据后查看之前杂乱数据看是否有可以拼帧成功的情况
for (int j = 0; j < otherindex; j++)
{
if(j == 0)
{
drm_Base64Encode(cOtherBytes, otherindex, cbase64, &cbase64len);
u_printf("FUNCTION[%s] the cOtherBytes is [%s]n", __FUNCTION__, cbase64);
}
packetLen2 = isValiadPacket(cOtherBytes, j, otherindex);
if(packetLen2 > 0)
{
memcpy(curPacket, &(cOtherBytes[j]), packetLen2);
setOneCmdPacket(curPacket);
j += packetLen2-1;
}
}
memset(cOtherBytes, 0, sizeof(cOtherBytes));
otherindex = 0;
memcpy(curPacket, &(data[i]), packetLen);
setOneCmdPacket(curPacket);
i += packetLen-1; //跳过本帧其余数据
drm_Base64Encode(curPacket, packetLen, cbase64, &cbase64len);
u_printf("FUNCTION[%s] receive the [%d]st data[%s] n", __FUNCTION__, g_uartCMDNum, cbase64);
memset(curPacket, 0x0, sizeof(curPacket));
packetLen = 0;
}
else
{
cOtherBytes[otherindex++] = data[i];
}
}
if(otherindex > 0)
{
drm_Base64Encode(cOtherBytes, otherindex, cbase64, &cbase64len);
// otherindex = 0;
u_printf("FUNCTION[%s] some data not in CMD format[%s],so ignore it!!!!n", __FUNCTION__, cbase64);
}
}
最后
以上就是香蕉酒窝为你收集整理的遇到的问题(一):智能家居中WIFI模块设备在接收底层家具设备串口数据时由于断帧而出现的指令丢失的情况处理的全部内容,希望文章能够帮你解决遇到的问题(一):智能家居中WIFI模块设备在接收底层家具设备串口数据时由于断帧而出现的指令丢失的情况处理所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复