【发布时间】:2013-12-05 20:55:22
【问题描述】:
我需要在一些德州仪器 CC2530 节点之间发送一些浮点数。
这种架构只能发送一个uint8数组。
我已经尝试做的是将带有强制转换的浮点指针分配给 uint8 指针。
然后我发送这些字节,当接收到它们时,它们被复制到一个 uint8 数组中。
最后,我创建了另一个浮点指针,该指针设置了先前创建的数组的强制转换。
实际上,这个技巧适用于我的电脑(使用 unsigned char 而不是 uint8),但不适用于接收到的数字始终为 0 的节点。
这是传输事件中使用的代码部分(它在那里创建msa_Data1,即传输的数据包,其中要发送的数字是valMed(全局变量)):
void Data_create(){
uint8 *sends;
sends = (uint8 *) &valMed;
uint8 rec_index;
for(rec_index=0; rec_index < sizeof(float); ++rec_index){
msa_Data1[rec_index]= sends[rec_index];
}
}
在接待区我有:
uint8 receiveds[sizeof(float)];
uint8 rec_index;
for(rec_index=0; rec_index < sizeof(float); ++rec_index){
receiveds[rec_index]= pData->dataInd.msdu.p[rec_index];
}
float *received= (float *)receiveds;
来自传输的数据被pData->dataInd.msdu.p[rec_index]接收
我在pc上试过的传输模拟是:
main(){
float send= -3.14;
unsigned char *sends;
sends = (unsigned char *)&send;
printf("1:%d, 2:%d, 3:%d, 4:%d \nfloat sent:%f \n", sends[0], sends[1], sends[2], sends[3], send);
unsigned char receiveds[sizeof(float)];
int rec_index;
for(rec_index=0; rec_index < sizeof(float); ++rec_index){
receiveds[rec_index]= sends[rec_index];
}
float *received= (float *)receiveds;
printf("float received:%f\n", *received);
}
输出:
alex@aspire7738G ~/test $ ./test
1:195, 2:245, 3:72, 4:192
float sent:-3.140000
float received:-3.140000
在这种情况下,我可以看到测试在 pc 上运行良好,但在节点上却没有。 怎么了?
提前感谢您的帮助!
亚历克斯
【问题讨论】:
-
接收端能否“异相”接收来自第一个数据包的结束字节和下一个数据包的开始字节?如果可能,您的数据包需要增加大小以包含一些内容,以确保在通信期间正确对齐。
-
数据包大小应该足以满足该范围,我有 20 个字节的数据包,而浮点数只有 4 个字节。此外,创建的数据包将数据设置在要发送的阵列的正确位置,这甚至消除了反向重新排序问题(串行通信的经典问题)。谢谢!
标签: c msp430 texas-instruments