为什么需要转换字节序-例子
机智云的协议: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呀