读取Arduino使用NRF24L01 +从Arduino发送的Raspberry pi 3b +上的python中的Struct时出错,

问题描述 投票:0回答:1

我一直在努力将传感器值从arduino发送到树莓派3b +。我正在使用NRF24L01 +模块进行通信。我正在尝试从arduino向树莓派发送加速度计值(双精度型)。发送值的Arduino代码的一部分:

typedef struct {
  double rollx;
  double pitchy;
}myStruct;

myStruct duomenys;

  duomenys.rollx = kalAngleX;
  duomenys.pitchy = kalAngleY;
  radio.write(&duomenys,sizeof(duomenys));

  Serial.print("Roll: ");
  Serial.println(duomenys.rollx);
  Serial.print("Pitch: ");
  Serial.println(duomenys.pitchy);
  Serial.print("\t");

这是arduino串行监视器的输出:

Pitch: -12.98
    Roll: 89.85
Pitch: -12.97
    Roll: 89.85
Pitch: -12.96
    Roll: 89.86

但是在覆盆子方面,我无法解压缩接收到的结构(注意:我对python很陌生)。我尝试过的是:

while True:

    while not radio.available(0):
      ##  print("Nepareina")
        time.sleep(1)

    duomenys = []

    radio.read(duomenys, radio.getDynamicPayloadSize())
    data = struct.unpack('ff',duomenys)
    rollx = data [0]
    pitchy = data[1]
    print(rollx)
    print("                              ")
    print(pitchy)


编译此代码时,出现错误:

Traceback (most recent call last):
  File "/home/pi/Desktop/NRF24L01/receiveArduino.py", line 41, in <module>
    data = struct.unpack('ff',duomenys)
TypeError: a bytes-like object is required, not 'list'

如果我换行

data = struct.unpack('ff',duomenys)

to

data = struct.unpack('ff',bytes(duomenys))

我收到一个错误:

Traceback (most recent call last):
  File "/home/pi/Desktop/NRF24L01/receiveArduino.py", line 41, in <module>
    data = struct.unpack('ff',bytes(duomenys))
struct.error: unpack requires a bytes object of length 8

如果有人对如何使用python阅读接收到的结构有任何建议,请随时分享。

编辑:更新了arduino串行监视器输出。以前发布了错误的输出。

编辑:这是我正在使用的NRF24L01库。https://github.com/matmany/lib_nrf24

python struct arduino raspberry-pi3
1个回答
0
投票

嘿,我不知道你是否解决了。我遇到了同样的问题,因此设法解决了这个问题:

而不是声明duomenys = [] ...试试duomenys = bytearray(nbBytesData),其中nbBytesData应该是您期望的字节数(我假设是8个字节)

然后data = struct.unpack('ff',duomenys)应该可以工作。

© www.soinside.com 2019 - 2024. All rights reserved.