PySerial创建易读的换行符

时间:2013-12-01 21:10:49

标签: python pyserial

所以这是我第二次在SO上发帖,我必须说我非常感谢我从第一篇文章中得到的所有帮助。这是一个很棒的社区。

我正在使用Python和PySerial将一些数据从一个设备发送到另一个设备。这就是我得到的:

import serial
s=serial.Serial(2) #defining COM port 3 for device 1
r=serial.Serial(3) #defining COM port 4 for device 2
s.timeout=3
r.timeout=3
s.write('The time is ')
s.write('The day is ')
s.write('The temp is ')
r.readline()

我在输出上收到'时间就是这一天是温度是'

我怎么能得到输出:

时间是

这一天

临时是

我已经尝试过对每个人使用\ n,例如,s.write('时间是\ n')来创建一个换行符,但这会导致和eol,这意味着,如果我这样做了r.readline()我收到'时间是\ n'

1 个答案:

答案 0 :(得分:0)

你只做一个readline()。 readline将一直读到\n或超时。由于您正在进行三次写入,因此需要三次读取才能获取所有数据。

import serial
s=serial.Serial(2) #defining COM port 3 for device 1
r=serial.Serial(3) #defining COM port 4 for device 2
s.timeout=3
r.timeout=3
s.write('The time is ')
s.write('The day is ')
s.write('The temp is ')
# need three reads to read all three of the messages you sent
print r.readline()
print r.readline()
print r.readline()