pyserial - 如何阅读从串行设备发送的最后一行 [英] pyserial - How to read the last line sent from a serial device
问题描述
我有一个Arduino连接到我的计算机上运行一个循环,发送通过串行端口值回计算机每100 毫秒。
我想打一个Python脚本,它将从串口只每隔几秒钟看,所以我希望它只是看到从阿尔杜伊诺发送的最后一件事。
如何在Pyserial这样做吗?
这里的code我试图简化版,它的工作。它顺序读取行。
导入序列
进口时间SER = serial.Serial('COM4,9600,超时= 1)
而1:
time.sleep(10)
打印ser.readline()#如何我从设备发送的最新行?
也许我误解你的问题,但因为它是一个串口线,你必须阅读一切从Arduino的顺序发送的 - 它会被缓存起来Arduino的,直到你读它。
如果你想有一个状态显示,显示发送最新的东西 - 使用的集成了code在你的问题一个线程(减去睡眠),并保持阅读从最新的行的最后一个完整产品线Arduino的。
更新: mtasic
的例子code是相当不错的,但如果阿尔杜伊诺派出了部分行,当 inWaiting()
被调用时,你会得到一个截断线。相反,你想要做的就是把最后的完整的行成 last_received
,并保持在局部线缓冲
,以便它可以被附加到下一次轮循环。事情是这样的:
高清接收(SER):
全球last_received buffer_string =''
而真正的:
buffer_string = buffer_string + ser.read(ser.inWaiting())
如果的'\\ n'在buffer_string:
线= buffer_string.split('\\ n')#保证至少有2项
last_received =行[-2]
#如果Arduino的发送的空行地段,你会失去
#last填充线,所以你可以作上述表示的条件
#like这样:如果线[-2]:last_received =行[-2]
buffer_string =行[-1]
关于使用的ReadLine()
的:这是什么Pyserial文档有说(略编辑的清晰度和附带一提改成readlines()):
使用的ReadLine时要小心。做
打开时指定超时
串行端口,否则它可以阻止
永远如果没有换行符
好评。还注意到,readlines方法()
仅适用于超时。它
取决于有超时和
除$ P $其中pts,作为EOF(文件结束)。
块引用>这似乎是很有道理的我!
I have an Arduino connected to my computer running a loop, sending a value over the serial port back to the computer every 100 ms.
I want to make a Python script that will read from the serial port only every few seconds, so I want it to just see the last thing sent from the Arduino.
How do you do this in Pyserial?
Here's the code I tried which does't work. It reads the lines sequentially.
import serial import time ser = serial.Serial('com4',9600,timeout=1) while 1: time.sleep(10) print ser.readline() #How do I get the most recent line sent from the device?
解决方案Perhaps I'm misunderstanding your question, but as it's a serial line, you'll have to read everything sent from the Arduino sequentially - it'll be buffered up in the Arduino until you read it.
If you want to have a status display which shows the latest thing sent - use a thread which incorporates the code in your question (minus the sleep), and keep the last complete line read as the latest line from the Arduino.
Update:
mtasic
's example code is quite good, but if the Arduino has sent a partial line wheninWaiting()
is called, you'll get a truncated line. Instead, what you want to do is to put the last complete line intolast_received
, and keep the partial line inbuffer
so that it can be appended to the next time round the loop. Something like this:def receiving(ser): global last_received buffer_string = '' while True: buffer_string = buffer_string + ser.read(ser.inWaiting()) if '\n' in buffer_string: lines = buffer_string.split('\n') # Guaranteed to have at least 2 entries last_received = lines[-2] #If the Arduino sends lots of empty lines, you'll lose the #last filled line, so you could make the above statement conditional #like so: if lines[-2]: last_received = lines[-2] buffer_string = lines[-1]
Regarding use of
readline()
: Here's what the Pyserial documentation has to say (slightly edited for clarity and with a mention to readlines()):Be careful when using "readline". Do specify a timeout when opening the serial port, otherwise it could block forever if no newline character is received. Also note that "readlines()" only works with a timeout. It depends on having a timeout and interprets that as EOF (end of file).
which seems quite reasonable to me!
这篇关于pyserial - 如何阅读从串行设备发送的最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!