我正在使用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,但我发现了一篇关于 similar problem with Raspberry PI 的帖子。在那篇文章中,答案之一 redirected to this link
那里说问题是由 Raspberry Pi 串行端口引起的,默认情况下使用系统控制台,当您尝试将其用于自己的目的时会发生冲突
要禁用控制台的串行端口,您必须编辑文件 /etc/inittab
并注释行 T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
code> (在行首用 #
对其进行注释,就像在 python 中一样)。您必须重新启动 ODROID,它应该可以工作
我建议你阅读我链接的答案,因为它更多地解释了如何替代串行端口来访问命令行(建议使用 ssh),另一件事是 Raspberry PI(并假设 ODROID 的工作原理类似) )在启动时通过串行端口发送一条消息,该消息将由 Arduino 接收。您可以删除该消息,那里有解释
希望这对你有帮助
关于python-serial OSError : [Errno 11] Resource temporarily unavailable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36133620/