[英]How can a Raspberry PI receives a struct from Arduino?
I am trying to find a solution to this problem for a few days and nothing worked for me so far.几天来,我一直在努力寻找解决此问题的方法,但到目前为止对我没有任何帮助。 I have to mention thou that Python is not my strong point.我不得不提一下,Python 不是我的强项。
Anyway, I am trying to send a typedef struct
using a LoRa transceiver(RFM95) that I programmed with the Radiohead
library.无论如何,我正在尝试使用我用Radiohead
库编程的 LoRa 收发器(RFM95)发送一个typedef struct
。 The structure looks like this:结构如下所示:
typedef struct {
double temp;
double hum;
} Payload;
I am able to receive the data on the raspberry side, but like an array of bytes.我能够在覆盆子端接收数据,但就像一个字节数组。
Received: [0, 0, 228, 65, 154, 153, 65, 66]
How can I convert this into an object like {temp: VALUE_OF_TEMP, hum: VALUE_OF_HUM}
?如何将其转换为 object 像{temp: VALUE_OF_TEMP, hum: VALUE_OF_HUM}
?
Is there a way to do so like in C?有没有办法像 C 那样做? Example:例子:
typedef struct {
double temp;
double hum;
} ReceivedData;
ReceivedData data;
data = *(Payload*)receivedBuffer;
Please help!请帮忙! I ran out of ideeas!我没有主意了!
You didn't mention what kind of Arduino you are using, I'm assuming that you are using an Arduino Uno or something with an 8-bit MCU.您没有提到您使用的是哪种 Arduino,我假设您使用的是 Arduino Uno 或带有 8 位 MCU 的东西。 For Arduino with 8-bit MCU, the double
is just like float
consists of 4 bytes instead of 8 bytes in 32-bit MCU or Raspberry Pi.对于带有 8 位 MCU 的 Arduino, double
就像float
由 4 个字节组成,而不是 32 位 MCU 或 Raspberry Pi 中的 8 个字节。
So to unpack the struct on the RPI with Python, I think this is what you are looking for:因此,要使用 Python 解压缩 RPI 上的结构,我认为这就是您要查找的内容:
import struct
data = [0, 0, 228, 65, 154, 153, 65, 66]
bstr = bytearray(data)
result = struct.unpack("<ff", bstr)
print(result)
This would product the result as:这将产生结果:
(28.5, 48.400001525878906)
I think these are the two values that you sent over the Arduino.我认为这是您通过 Arduino 发送的两个值。
Update更新
If you are receiving data that consists of multiple data structs, you can use iter_unpack()
method:如果您正在接收由多个数据结构组成的数据,您可以使用iter_unpack()
方法:
import struct
data = [0, 0, 228, 65, 154, 153, 65, 66, 0, 0, 228, 65, 154, 153, 65, 66, 0, 0, 228, 65, 154, 153, 65, 66]
bstr = bytearray(data)
results = struct.iter_unpack("<ff", bstr)
for result in results:
print(result)
I would suggest you read the documentation of Python struct in more details to take the full advantage of the library.我建议您更详细地阅读 Python结构的文档,以充分利用该库。
That's fairly easy to do with the struct module:使用struct模块很容易做到这一点:
In your case, you are decoding two doubles, so the format is dd
.在你的情况下,你正在解码两个双打,所以格式是dd
。
from struct import unpack
def parse_payload(data_buffer):
as_tuple = unpack('dd', data_buffer)
return {
"temp": as_tuple[0],
"hum": as_tuple[1]
}
Obviously, you should still do some validation and sanity checking beforehand, and you may have to deal with endianness issues, but this should be enough to get you going.显然,您仍然应该事先进行一些验证和完整性检查,并且您可能必须处理字节顺序问题,但这应该足以让您继续前进。
try to use #pragma pack(push, 1) struct.尝试使用 #pragma pack(push, 1) 结构。 . . . . #pragma pack(pop) #pragma 包(流行音乐)
for prevent byte arrange为了防止字节排列
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.