Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Raspberry pi 3b+;上读取python结构时出错;使用NRF24L01和x2B从Arduino发送;_Python_Struct_Arduino_Raspberry Pi3 - Fatal编程技术网

在Raspberry pi 3b+;上读取python结构时出错;使用NRF24L01和x2B从Arduino发送;

在Raspberry pi 3b+;上读取python结构时出错;使用NRF24L01和x2B从Arduino发送;,python,struct,arduino,raspberry-pi3,Python,Struct,Arduino,Raspberry Pi3,我一直在努力将传感器值从arduino发送到raspberry pi 3b+。我使用NRF24L01+模块进行通信。我正在尝试将加速度计值(双类型)从arduino发送到raspberry pi。 发送值的Arduino代码的一部分: typedef struct { double rollx; double pitchy; }myStruct; myStruct duomenys; duomenys.rollx = kalAngleX; duomenys.pitchy =

我一直在努力将传感器值从arduino发送到raspberry pi 3b+。我使用NRF24L01+模块进行通信。我正在尝试将加速度计值(双类型)从arduino发送到raspberry pi。 发送值的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

然而,在raspberry方面,我无法解压缩接收到的结构(注意:我对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'
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

我得到一个错误:

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'
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
回溯(最近一次呼叫最后一次):
文件“/home/pi/Desktop/NRF24L01/receiveardino.py”,第41行,在
数据=结构解包('ff',字节(双字节))
struct.error:解包需要长度为8的字节对象
如果有人对如何在python中读取接收到的结构有任何建议,欢迎分享

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

编辑:这是我正在使用的NRF24L01库。
嘿,我不知道你是否解决了它。我遇到了同样的问题,我成功地解决了这个问题:

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


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

您是否检查了
duomenys
的值?如果您在谈论arduino端的输出,我的错在那里,从另一个版本的代码(它是int-type而不是double)获得输出.我只是想问一下Duomeys的内容是否符合您的期望。提示:使用调试器或打印它,如果你还没有。是的,它们正是我所期望的。这里的问题在覆盆子方面。我无法在python中解压
duomenys
的值。请尝试
result=radio.read(duomenys,radio.getDynamicPayloadSize())
并打印
result
的内容。您也可以发布
radio.read的声明,或者如果它是从库发布其文档。可能是,你没有像预期的那样称呼它。