全球最实用的IT互联网信息网站!

AI人工智能P2P分享&下载搜索网页发布信息网站地图

当前位置:诺佳网 > 电子/半导体 > 嵌入式技术 >

嵌入式开发中组包的过程和解析的过程一样吗

时间:2023-08-08 09:12

人气:

作者:admin

标签: 数据  组包   

导读:在嵌入式产品开发中,我们经常会遇到两个设备之间的通信、设备与服务器的通信、设备和上位机的通信等,很多时候通信协议都是自定义的,所以这就涉及到自定义协议的解析和组包...

嵌入式产品开发中,我们经常会遇到两个设备之间的通信、设备与服务器的通信、设备和上位机的通信等,很多时候通信协议都是自定义的,所以这就涉及到自定义协议的解析和组包问题。

比如针对下面的这样一个协议:

帧头1 帧头2 字段1 字段2 校验
固定值:0x55 固定值:0xAA 设备ID 电压值 前面所有数据异或值
char char short float char
1字节 1字节 2字节 4字节 1字节

数据在发送时涉及到一个大小端的概念,大小端是针对多字节数据的传输,比如上述协议中字段1,假设两字节内容为0x0001,先发送0x01后发送0x00,称为小端模式;先发送0x00后发送0x01,称为大端模式。

假设字段1内容为0x001,字段2内容为0x40533333(对应为3.3)

假设按照小端方式发送,下面是帧数据:

55 AA 01 00 33 33 53 40 ED

下面来看看如何解析:

若干年前,在第一次面对这种问题时,用的如下傻瓜式的代码方式实现:

#include

intmain()
{
unsigned char Rxbuf[9]={0x55,0xAA,0x01,0x00,0x33,0x33,0x53,0x40,0xED};

short DeviceId;
floatVoltage;

unsigned char check=0;
int i;

for(i=0;i<8;i++)
    {
        check ^= Rxbuf[i];
    }

    if(Rxbuf[0]==0x55 && Rxbuf[1]==0xAA && Rxbuf[8]==check )
    {
        DeviceId=(Rxbuf[3]<<8)|Rxbuf[2];
        Voltage= *((float *)&Rxbuf[4]);

        printf("DeviceId:%d
",DeviceId);
        printf("Voltage:%f
",Voltage);
    }

    return 0;
}
9fcfbe42-3583-11ee-9e74-dac502259ad0.png

简单来说就是硬来,按照数组的先后顺序逐个重组解析,如果协议比较长,代码里会充斥着很多的数组下标,一不小心就数错了。而且如果更改协议的话,代码要改动很多地方。

后来有人告诉我可以定义个结构体,然后使用memcpy函数直接复制过去就完事了。

#include
#include
#pragma pack(1)
struct RxFrame
{
unsigned char header1;
unsigned char header2;
short deviceId;
floatvoltage;
unsigned char check;
};


intmain()
{
unsigned char Rxbuf[9]={0x55,0xAA,0x01,0x00,0x33,0x33,0x53,0x40,0xED};

struct RxFrame RxData;

unsigned char check=0;
int i;

for(i=0;i<8;i++)
    {
        check ^= Rxbuf[i];
    }

    memcpy(&RxData,Rxbuf,sizeof(Rxbuf));

    if(Rxbuf[0]==0x55 && Rxbuf[1]==0xAA && RxData.check==check )
    {
        printf("DeviceId:%d
",RxData.deviceId);
        printf("Voltage:%f
",RxData.voltage);
    }

    return 0;
}

嗯,的确是方便了很多。不过,该方式仅适合小端传输方式。

再后来,又见到有人用如下代码实现:

#include
#include"convert.h"

intmain()
{
unsigned char Rxbuf[9]={0x55,0xAA,0x01,0x00,0x33,0x33,0x53,0x40,0xED};

short DeviceId;
floatVoltage;

unsigned char check=0;
int i;
int index=0;

for(i=0;i<8;i++)
    {
        check ^= Rxbuf[i];
    }

    if(Rxbuf[0]==0x55 && Rxbuf[1]==0xAA && Rxbuf[8]==check )
    {
        index += 2;
        ByteToShort(Rxbuf, &index, &DeviceId);
        ByteToFloat(Rxbuf, &index, &Voltage);

        printf("DeviceId:%d
",DeviceId);
        printf("Voltage:%f
",Voltage);
    }

    return 0;
}
其中convert.h如下:

#ifndef CONVERT_H
#define CONVERT_H

voidShortToByte(unsigned char*dest,int*index,short value);
voidFloatToByte(char*dest,int*index,floatvalue);

#endif//CONVERT_H
convert.c如下:
#include"convert.h"
#include
#include

static bool Endianflag=0;

void ByteToShort(const unsigned char*source,int*index,short*result)
{
int i,len=sizeof(short);
char p[len];
memset(p,0,len);

if(Endianflag==1)
{
for(i=0;i< len; i++ )
            *(p+i) = *(source + *index + len - i - 1);
    }
    else
    {
        for( i = 0; i < len; i++ )
            *(p+i) = *(source + *index + i);
    }


    *result = *((short*)p);

    *index += len;
}

void ByteToFloat(unsigned char* source, int* index, float* result)
{
    int i, len = sizeof(float);
    char p[len];
    memset(p, 0, len);


    if(Endianflag == 1 )
    {
        for( i = 0; i < len; i++ )
            *(p+i) = *(source + *index + len - i - 1);
    }
    else
    {
        for( i = 0; i < len; i++ )
            *(p+i) = *(source + *index + i);
    }


    *result = *((float*)p);

    *index += len;
}
该方法既可以支持小端模式,也可以支持大端模式,使用起来也是比较方便。

除了上述2个函数,完整的转换包含以下函数,就是将Bytes转换为不同的数据类型,以及将不同的数据类型转换为Bytes。

#ifndef CONVERT_H
#define CONVERT_H

voidByteToShort(const unsigned char*source,int*index,short*result);
voidByteToInt(unsigned char*source,int*index,int*result);
voidByteToLong(char*source,int*index,long long*result);
voidByteToFloat(unsigned char*source,int*index,float*result);
voidByteToDouble(unsigned char*source,int*index,double*result);
voidByteToString(unsigned char*source,int*index,char*result,int length);


voidShortToByte(unsigned char*dest,int*index,short value);
voidIntToByte(char*dest,int*index,int value);
voidLongToByte(char*dest,int*index,long long value);
voidFloatToByte(char*dest,int*index,floatvalue);
voidDoubleToByte(unsigned char*dest,int*index,double value);
voidStringToByte(char*dest,int*index,int length,char*value);

#endif//CONVERT_H

组包的过程和解析的过程正好相反,这里不再赘述。你在开发中遇到这种问题时,又是如何处理的呢?欢迎留言讨论!

责任编辑:彭菁

温馨提示:以上内容整理于网络,仅供参考,如果对您有帮助,留下您的阅读感言吧!
相关阅读
本类排行
相关标签
本类推荐

CPU | 内存 | 硬盘 | 显卡 | 显示器 | 主板 | 电源 | 键鼠 | 网站地图

Copyright © 2025-2035 诺佳网 版权所有 备案号:赣ICP备2025066733号
本站资料均来源互联网收集整理,作品版权归作者所有,如果侵犯了您的版权,请跟我们联系。

关注微信