[英]Raspberry Arduino communication via pyserial stops after a day
[英]Serial Receiving from Arduino to Raspberry Pi with PySerial stops after a while
我正在开发一个项目,我必须一次收到大约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);
}
}
它发送一行像这样的'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
此代码非常正确地接收数据5秒钟,然后突然停止接收。
此外,当我尝试以下操作时,我没有输出或输入/输出错误。
serial_data = ser.readline()
print serial_data
编辑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?)
通过PySerial从arduino到raspberry接收25个字符数据的正确方法是什么? 任何帮助将非常感激。
我遇到了同样的问题,并且好好打破了我的脑袋,试试这个
跑
ps -ef | grep tty
如果输出看起来像
root 2522 1 0 06:08 ? 00:00:00 /sbin/getty -L ttyAMA0 115200 vt100
然后,您需要禁用getty尝试将数据发送到该端口
为了使用Raspberry Pi的串口,我们需要通过在文件/ etc / inittab中找到这一行来禁用getty(显示登录界面的程序)
T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
并通过在其前面添加#来评论它
#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100)
要防止Raspberry Pi在引导时将数据发送到串行端口,请转到文件/boot/cmdline.txt并找到该行并将其删除
console=ttyAMA0,115200 kgdboc=ttyAMA0,115200
重新启动Raspberry Pi
信用到期的信用: http : //blog.oscarliang.net/raspberry-pi-and-arduino-connected-serial-gpio/帮助我弄清楚如何获得贷款
在raspberry pi中读取gps数据时,我不得不挣扎。 输出将在大约10秒后停止并报告
device reports readiness to read but returned no data (device disconnected?)
几乎所有论坛中提供的解决方案都是针对带有wheezy的raspberry pi 2。 通过执行以下操作,我终于通过jessie在我的覆盆子pi3中获得了连续的gps流:
enable_uart=1
并在dtoverlay=pi3-disable-bt
添加dtoverlay=pi3-disable-bt
。 然后重新启动 我不得不将/boot/cmdline.txt更改为
dwc_otg.lpm_enable=0 console=tty1 console=serial0,9600 root=/dev/mmcblk0p7 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
然后重新启动
停止getty: sudo systemctl stop serial-getty@ttyAMA0.service
禁用: sudo systemctl stop serial-getty@ttyAMA0.service
你需要小心/boot/cmdline.txt。 文件中的任何错误都可能使您的raspberry pi无法启动。 最好在编辑之前保留文件的备份。 同时正确设置波特率。 在我的情况下,它是9600.希望这有帮助!
我曾经经常这么做,然后一位朋友告诉我从python中提示arduino获取数据。
考虑让arduino只在你的python程序提示时发送数据:
prompt.py
#!/usr/bin/python
import serial, time
ser = serial.Serial('/dev/ttyACM0', 115200, timeout = 0.1)
#if you only want to send data to arduino (i.e. a signal to move a servo)
def send( theinput ):
ser.write( theinput )
while True:
try:
time.sleep(0.01)
break
except:
pass
time.sleep(0.1)
#if you would like to tell the arduino that you would like to receive data from the arduino
def send_and_receive( theinput ):
ser.write( theinput )
while True:
try:
time.sleep(0.01)
state = ser.readline()
print state
return state
except:
pass
time.sleep(0.1)
f = open('dataFile.txt','a')
while 1 :
arduino_sensor = send_and_receive('1')
f.write(arduino_sensor)
f.close()
f = open('dataFile.txt','a')
prompt.ino
void setup () { pinMode(13, OUTPUT); Serial.begin(115200); }
void loop() {
if (Serial.available()) {
ch = Serial.read();
if ( ch == '1' ) {
Serial.println(analogRead(A0)); // if '1' is received, then send back analog read A0
}
else if (ch == '2') {
digitalWrite(13,HIGH); // if '2' is received, turn on the led attached to 13
}
else if (ch == '3') {
digitalWrite(13,LOW); // if '3' is received then turn off the led attached 13
} else {
delay(10);
}
}
}
此外,我创建了一个github存储库,其中包含一些用于python-arduino通信的示例:
https://github.com/gskielian/Arduino-DataLogging/blob/master/PySerial/README.md
在你的Arduino
代码中的loop
函数期间,你永远不会结束换行符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);
}
}
你的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()
尝试sudo rasbpi-config
,选择接口选项,对第一条消息说“不”(“内核登录”),对第二条消息说“是”(“串行通信开启”)。 我发现如果第一条消息在它上面就会抛出错误。 说“不”修复了它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.