Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.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
Python Arduino Serial.readBytes与RPi通信时的奇怪行为_Python_Linux_Serialization_Arduino - Fatal编程技术网

Python Arduino Serial.readBytes与RPi通信时的奇怪行为

Python Arduino Serial.readBytes与RPi通信时的奇怪行为,python,linux,serialization,arduino,Python,Linux,Serialization,Arduino,我正试图通过串行USB从RPI3向Arduino Uno发送6个号码。它们应该是两个long,一个int,在Arduino端有三个独立的字节,这使得使用Python3从RPi3发送13个字节。为了调试,我让它们保持不变。Python代码: import serial import struct import time ser = serial.Serial('/dev/ttyACM0',9600) time.sleep(2) b1 = 36 l1 = 2000 l2 = 2000 i1

我正试图通过串行USB从RPI3向Arduino Uno发送6个号码。它们应该是两个long,一个int,在Arduino端有三个独立的字节,这使得使用Python3从RPi3发送13个字节。为了调试,我让它们保持不变。Python代码:

import serial
import struct
import time

ser = serial.Serial('/dev/ttyACM0',9600)
time.sleep(2)

b1 = 36
l1 = 2000 
l2 = 2000 
i1 = 120
b2 = 255
b3 = 2 

b1_struc = struct.pack("B",b1)
l1_struc = struct.pack("i",l1)
l2_struc = struct.pack("i",l2)
i1_struc= struct.pack("h",i1) 
b2_struc = struct.pack("B",b2)
b3_struc = struct.pack("B",b3)

order = b1_struc + l1_struc + l2_struc + i1_struc + b2_struc + b3_struc
# gives in total b'$\xd0\x07\x00\x00\xd0\x07\x00\x00x\x00\xff\x02'

while True:
    try:
        ser.write(order)
    except KeyboardInterrupt:
        quit()
我使用union/struc在Arduino上读到了如下内容:

union Container {
  byte by[13];
  struct {
    byte b1;
    long l1;
    long l2;
    int i1;
    byte b2;
    byte b3;
  } ordercollection;
} order;

void setup(){
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop(){

  if (Serial.available() > 0) {
    while (Serial.available()) {
      long check1;
      check1 = 4413;
      Serial.readBytes(order.by,13);
      if (check1 == receivecheck() || check1 == -receivecheck()) {       
        blinkLED();        
      } 
      else {

      }
    }
  }
}

void blinkLED(){
    digitalWrite(LED_BUILTIN, HIGH);  
    delay(1000);                       
    digitalWrite(LED_BUILTIN, LOW);   
    delay(1000); 
}

long receivecheck() {
  long sum;
  sum = order.ordercollection.b1 + order.ordercollection.l1 + order.ordercollection.l2
    + order.ordercollection.i1 + order.ordercollection.b2 + order.ordercollection.b3;   
  return sum;
}
因为我无法通过串行监视器进行调试(它会干扰传输),所以我正在使用某种校验和,将所有发送的数字相加,如果正确,LED开始闪烁。现在谈谈我的问题。如果我在Linux上使用Arduino IDE 1.8.1(肉桂18.1 64位)将上述代码上传到Arduino上,传输将精确工作6次,之后,数字将不再正确传输(校验和从4431变为不同的数字,最后变为-795081763,并保持该值)。我将Arduino连接到我的RPi,并尝试从Arduino IDE加载相同的代码(版本2:1.0.5+dsfg2-4),但是它抛出了
错误:从'byte*{aka unsigned char*}'到'char*'[-fmpermissive]
的无效转换,可以使用

Serial.readBytes((char*)orders.by,13);
结果是一样的:例如,6次正确的传输,然后搞砸了。排除波特率、定时错误等后,我更改了线路:

   Serial.readBytes((char*)orders.by,15);
奇怪的是,它起了作用。但是,当我现在用这一行修改后的代码从我的linux笔记本加载到Arduino时,它根本不起作用(一次传输都不起作用)!由于我希望尽可能少地修改Arduino代码(因为有几个人正在使用他们自己的项目访问它),我想知道,为什么它没有像我在上面的代码中用
Serial.readBytes(orders.by,13)表示的那样工作是不是有符号/无符号字节/字符的东西,这样我就可以通过更改python代码来修复它

谢谢

编辑


我解开了一个谜团:LinuxMint上的ArduinoIDE似乎产生了有缺陷的结果。我将工作代码从RPi迁移到Windows机器,并获得了与
Serial.readBytes((char*)orders.by,15)相同的结果
串行.readBytes(orders.by,15)然而,我想为什么我必须设置15个字节的长度,即使我的订单只有13个字节长。

在LED闪烁后,您的代码在2秒钟内无法从序列中读取。同时,您不断地从Python代码发送数据。这会溢出串行缓冲区,并且串行流会失去同步

您应该通过添加超过2秒的延迟来降低Python代码的速度

为True时:
尝试:
ser.write(订单)
睡眠时间(3);
除键盘中断外:
退出

将您的解决方案发布为答案,而不是对问题的编辑。嘿!我没有将其作为解决方案发布,因为它只是部分解决了我的问题。我仍然不知道,为什么我需要在
readBytes
中读取15个字节才能在更长的时间内正确接收13个传输的字节