通讯传输需要测试一下是按大端序还是小端序传输:
uint32_t num = 0x00000012;
uint8_t *ptr = (uint8_t *)#
for (uint8_t i = 0; i < 4; ++i)
{
printf("%x,", ptr[i]);
}
打印显示:
12,0,0,0,
低字节在前面,高字节在后面,是小端序。
如果要求发送小端序(8个byte):
可以对数据进行合并,利用位运算的方式:
uint8_t func = 0x2B;
uint16_t ind = 0x6040;
uint8_t subind = 0x00;
uint32_t index = func | (ind << 8) | (subind << 24);
// 发送index
进一步的发送的时候可以设置一个结构体:
typedef struct _msg
{
uint32_t index;
uint32_t data;
} msg;
msg m;
uint8_t func = 0x2B;
uint16_t ind = 0x6040;
uint8_t subind = 0x00;
uint32_t index = func | (ind << 8) | (subind << 24);
m.index = index;
m.data = 0x00000006;
// 发送msg
完整测试代码,可以使用打印的方法查看:
typedef struct _msg
{
uint32_t index;
uint32_t data;
} msg;
int main()
{
uint8_t func = 0x2B;
uint16_t ind = 0x6040;
uint8_t subInd = 0x00;
msg m;
m.index = func | (ind << 8) | (subInd << 24);
m.data = 0x00000006;
uint8_t *ptr = (uint8_t *)&m;
for (uint8_t i = 0; i < 8; ++i)
{
printf("%x,", ptr[i]);
}
}