简体   繁体   English

Raspberry PI 如何从 Arduino 接收结构?

[英]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.

相关问题 如何通过 USB 电缆将消息从 Raspberry Pi 发送到 Arduino - How send messages from Raspberry Pi to Arduino via USB cable 如何从Arduino上的脚本读取树莓派温度来控制风扇 - How to read raspberry pi temperature from script on an Arduino to control fan 如何使用python从树莓派向arduino发送字符串 - How to send string to arduino from raspberry pi using python 在使用 NRF24L01+ 从 Arduino 发送的 Raspberry pi 3b+ 上读取 Python 中的结构时出错 - Error reading Struct in python on Raspberry pi 3b+ sent from Arduino using NRF24L01+ 如何通过I2C使用Raspberry pi从Arduino读取数据 - How to Read Data from Arduino with Raspberry pi via I2C 将带有 Python 的树莓派字符串发送到 Arduino 以启动 LED 灯条。 怎么把select正确的USB端口? - Send string from raspberry pi with Python to Arduino for starting led strip. How to select the right USB port? 一段时间后,从Arduino到Raspberry Pi的串行接收与PySerial停止 - Serial Receiving from Arduino to Raspberry Pi with PySerial stops after a while 从Raspberry PI 3到Arduino UNO的串行通信丢失字节 - Losing bytes over Serial communication from Raspberry PI 3 to Arduino UNO 通过XBee模块将Raspberry Pi中的字符串发送到Arduino - Sending a String from Raspberry Pi to Arduino via XBee modules Arduino,Raspberry Pi和HTML页面 - Arduino, Raspberry Pi, and HTML page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM