一段时间后,使用 PySerial 从 Arduino 到 Raspberry Pi 的串行接收停止
问题描述
我正在进行一个项目,我必须一次接收大约 25 个字符的数据才能在 Raspberry Pi 中处理它.这是生成我想从 Arduino 接收的一些数据的示例代码:
I'm working on a project in which I have to receive some 25 character data at a time in order to process it in Raspberry Pi. Here is the example code that generates some data I want to receive from 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('
');
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);
}
}
它会发送这样的一行 'lllldddddllldddd...' 这行是 25 个字符的长度.现在,我想用 Raspberry Pi 接收这个.这是我正在尝试工作的代码:
It sends a line like this 'llllddddllldddd...' This line is 25 characters length. Now, I want to receive this with Raspberry Pi. Here is the code I'm trying to work:
ser = serial.Serial('/dev/AMA0',9600,timeout=1)
ser.open()
try:
serial_data = ser.readline()
print serial_data
except serial.serialutil.SerialException:
pass
这段代码非常正确地接收数据大约 5 秒钟,然后突然停止接收.
This code receives data very correctly for like 5 seconds, and then suddenly stops receiving.
此外,当我尝试以下操作时,我没有得到任何输出或输入/输出错误.
Moreover, when I try the following, I get no output, or Input/output errors.
serial_data = ser.readline()
print serial_data
好的,我现在评论了异常.它给出了以下错误:
Okay, I commented the exception now. It gives the following error:
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?)
通过 PySerial 将 25 个字符的数据从 arduino 接收到 raspberry 的正确方法是什么?任何帮助将不胜感激.
What is the correct way to receive a 25 character data from arduino into raspberry via PySerial? Any help will be greately appreciated.
解决方案
我也遇到了同样的问题,头疼了好久,试试这个
I had the same problem and was breaking my head for a good time, try this
运行
ps -ef | grep tty
如果输出看起来像任何东西
If the output looks anything like
root 2522 1 0 06:08 ? 00:00:00 /sbin/getty -L ttyAMA0 115200 vt100
那么你需要禁止 getty 尝试向该端口发送数据
Then you need to disable getty from trying to send data to that port
为了使用树莓派的串口,我们需要通过在文件/etc/inittab中找到这一行来禁用getty(显示登录屏幕的程序)
In order to use the Raspberry Pi’s serial port, we need to disable getty (the program that displays login screen) by find this line in file /etc/inittab
T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
并在前面加#注释掉
#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100)
要防止树莓派在启动时向串口发送数据,请转到文件/boot/cmdline.txt 并找到该行并将其删除
To prevents the Raspberry Pi from sending out data to the serial ports when it boots, go to file /boot/cmdline.txt and find the line and remove it
console=ttyAMA0,115200 kgdboc=ttyAMA0,115200
重启树莓派
信用到期:http://blog.oscarliang.net/raspberry-pi-and-arduino-connected-serial-gpio/ 帮助我弄清楚如何禁用 getty
Credit where credit is due: http://blog.oscarliang.net/raspberry-pi-and-arduino-connected-serial-gpio/ helped me figure out how to diable getty
相关文章