python-serial OSError: [Errno 11] Resource temporarily unavailable

python-serial OSError: [Errno 11] Resource temporarily unavailable

我正在使用Arduino Nano与ODROID进行串口通信(单板机安装Ubuntu 14.04)。 Arduino 代码:

void setup() {
   Serial.begin(9600); // set the baud rate
   Serial.println("Ready"); // print "Ready" once
}
void loop() {
  char inByte = ' ';
  if(Serial.available()){ // only send data back if data has been sent
    char inByte = Serial.read(); // read the incoming data
  Serial.println(inByte); 
}
  delay(100); // delay for 1/10 of a second
}   

ODROID中的Python代码:

#!/usr/bin/env python

from time import sleep
import serial
ser = serial.Serial('/dev/LIDAR', 9600, timeout=1) # Establish the connection on a specific port

sleep(1)
print "Arduino is initialized"

counter = 32 # Below 32 everything in ASCII is gibberish

while True:
    if (ser.inWaiting()>0):
      counter +=1
      ser.write(str(chr(counter))) # Convert the decimal number to ASCII then send it to the Arduino
      print ser.readline() # Read the newest output from the Arduino
      sleep(.1) # Delay for one tenth of a second
      if counter == 255:
          counter = 32

ser.close  

回溯(最近最后一个):

File "./serial_test1.py", line 16, in <module>        
    print ser.readline() # Read the newest output from the Arduino       
File "/usr/lib/python2.7/dis-package/serial/serialposix.py", line 43, in read   
    buf = os.read(self.fd, size-len(read))     
OSError: [Errno 11]Resource temporarily unavailable 

然后我在打印一些值后遇到了这个问题,我知道这个问题可能是当前没有可用的数据。但是我怎样才能弄清楚这个问题。感谢您的帮助。

我不知道这是否适用于 ODROID,但我发现了 post 关于 similar problem with Raspberry PI. In that post one of the answers redirected to this link

那里说问题是由Raspberry Pi串行端口引起的,它默认用于使用系统控制台,当您尝试将其用于您自己的目的时会发生冲突

要禁用控制台的串行 por,您必须编辑文件 /etc/inittab 并注释行 T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100(您在行的开头用 # 注释它,例如在 python)。您必须重新启动 ODROID,它应该可以工作

我建议您阅读我链接的答案,因为它更多地解释了如何替换串行端口来访问命令行(它建议使用 ssh),另一件事是 Raspberry PI(并假设 ODROID 的工作方式类似)在启动时通过串行端口发送一条消息,Arduino 将接收该消息。您可以删除该消息,那里有解释

希望对您有所帮助

您收到此错误是因为您的串行设备正被 os 本身使用。您应该停止 os 才能使用此设备。

Serial getty 现在是一项服务,您应该停止 and/or 禁用它:

sudo systemctl stop serial-getty@ttyAMA0.service
sudo systemctl disable serial-getty@ttyAMA0.service

请注意,我的本机串行设备 ID 是 ttyAMA0。

要永久禁用串行服务,请使用 sudo systemctl 掩码 serial-getty@ttyAMA0.service 在这种情况下,串行服务即使在重新启动时也不会启动。