需要字节转化的实例-机智云

为什么需要转换字节序-例子
机智云的协议:WIFI->MCU
1 固 定 包 头 2 0xFFFF
2 包 长度     2 len(命 令 ...校验和 )
3 命 令       1 0x01
4 包 序 号    1 0x##
5 flags       2 0x0000
++++++++上面是头++++++
6 校验和      1 0x##
可以看到我们的定义中有一个特殊的!
typedef struct
{
    uint8_t                 head[2];                ///< The head is 0xFFFF
    uint16_t                len;                    ///< From cmd to the end of the entire packet occupied by the number of bytes
    uint8_t                 cmd;                    ///< command
    uint8_t                 sn;                     ///< 
    uint8_t                 flags[2];               ///< flag,default is 0
} protocolHead_t;
没错就是len 它2个字节 它不是数组 他是uint16_t
WIFI给你发的是0X1234 其实它想发的是0X3412
这就需要转化!

protocolBuff[protocolCount] = tmpData;
protocolCount++;

if(protocolCount > 4)//接受数据超过4个head[2]; len;  已经有了! 
{
	head = (protocolHead_t *)protocolBuff;
	tmpCount = exchangeBytes(head->len)+4;//+4是因为前面4个没有计算在内
	if(protocolCount == tmpCount)//接受完成!
	{
		break;
	}
}

 

 

 

测试:

我发:
FF FF 00 05 0A 0B 11 22 33

实际代码看到过去的U16是0500
0500--0005--
转化成了0005

FF-FF-00-05-0A-0B-11-22-33
 

WHY不直接呢?我就是发的 00 05呀

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值