一段时间后,从Arduino到Raspberry Pi的串行接收与PySerial停止

moz*_*ors 10 python serial-port arduino pyserial raspberry-pi

我正在开发一个项目,我必须一次收到大约25个字符的数据,以便在Raspberry Pi中处理它.下面是生成我想从Arduino接收的一些数据的示例代码:

char i =0;
char  a =0;
char b=0;


void setup(){

 Serial.begin(9600);
 for(i=0;i<25;i++){

    Serial.print('l');}
    Serial.print('\n');
    delay(2000);
}


void loop(){

 for(i=0;i<25;i++){
     for(a=0;a<i;a++){
      if((a==9)||(a==19)||(a==24))
          Serial.print('l');
      else
          Serial.print('d');   
     }
     for(b=0;b<25-i;b++){
          Serial.print('l');
     }


     delay(2000);
  }
}
Run Code Online (Sandbox Code Playgroud)

它发送一行像这样的'llllddddllldddd ...'这行是25个字符的长度.现在,我希望通过Raspberry Pi获得此功能.这是我正在尝试的代码:

ser = serial.Serial('/dev/AMA0',9600,timeout=1)
ser.open()

try:
   serial_data = ser.readline()
   print serial_data
except serial.serialutil.SerialException:
   pass
Run Code Online (Sandbox Code Playgroud)

此代码非常正确地接收数据5秒钟,然后突然停止接收.

此外,当我尝试以下操作时,我没有输出或输入/输出错误.

serial_data = ser.readline()
print serial_data
Run Code Online (Sandbox Code Playgroud)

编辑1: 好的,我现在评论了这个例外.它给出以下错误:

 raise SerialException('device reporst rediness to read but returned no data (device disconnected?)')
serial.serialutil.SerialException: device reports readiness to read but returned no data (device disconnected?)
Run Code Online (Sandbox Code Playgroud)

通过PySerial从arduino到raspberry接收25个字符数据的正确方法是什么?任何帮助将非常感激.

小智 12

我遇到了同样的问题,并且好好打破了我的脑袋,试试这个

ps -ef | grep tty
Run Code Online (Sandbox Code Playgroud)

如果输出看起来像

root      2522     1  0 06:08 ?        00:00:00 /sbin/getty -L ttyAMA0 115200 vt100
Run Code Online (Sandbox Code Playgroud)

然后,您需要禁用getty尝试将数据发送到该端口

为了使用Raspberry Pi的串口,我们需要通过在文件/ etc/inittab中找到这一行来禁用getty(显示登录界面的程序)

T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
Run Code Online (Sandbox Code Playgroud)

并通过在其前面添加#来评论它

#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100)
Run Code Online (Sandbox Code Playgroud)

要防止Raspberry Pi在引导时将数据发送到串行端口,请转到文件/boot/cmdline.txt并找到该行并将其删除

console=ttyAMA0,115200 kgdboc=ttyAMA0,115200
Run Code Online (Sandbox Code Playgroud)

重新启动Raspberry Pi

信用到期的信用:http://blog.oscarliang.net/raspberry-pi-and-arduino-connected-serial-gpio/帮助我弄清楚如何获得贷款


Far*_*Joe 0

loop在代码中的函数期间,Arduino您永远不会结束换行符 char \n,这只是一个问题ser.readline(),因为它会读取直到一个\n字符。

在您的setup函数期间,您正确发送了一个\n字符,该字符可以解释正在发送的初始值,但不能解释数据。

也许像这样修改你的 Arduino 代码:

void loop(){
    for(i=0;i<25;i++){
        for(a=0;a<i;a++){
            if((a==9)||(a==19)||(a==24)) {
              Serial.print('l');
            } else {
                Serial.print('d');   
            }
        } /*end for loop a*/
        for(b=0;b<25-i;b++){
            Serial.print('l');
        } /*end for loop b*/

        Serial.print('\n'); // CODE EDITED HERE
        delay(2000);
    }    
}
Run Code Online (Sandbox Code Playgroud)

你的Python代码就像这样......

ser = None
try:
    ser = serial.Serial('/dev/AMA0',9600,timeout=3)
    ser.open()

    while True:
        try:
            serial_data = ser.readline()
            print serial_data
        except:
            pass
except:
    pass    
finally:
    if ser:
        ser.close()
Run Code Online (Sandbox Code Playgroud)