[英]python serial data read code with carriage return \r
This is my working code to read the data from the Artix-7 serial port. 这是我从Artix-7串口读取数据的工作代码。 But it comes form the carriage return.
但它来自回车。 Like;
喜欢;
'FFFDB03\r'
'FFFFB606'
'\r00006C0'
'D\rFFFFD8'
'1B\rFFFFB'
'037\r0000'
'606F\rFFF'
'FC0DF\rFF'
'FF81BE\r0'
'000037D\r'
how to solve this, can I set the CR and LF in the serial.Serial() settings. 如何解决这个问题,我可以在serial.Serial()设置中设置CR和LF。 If anyone knows please reply
如果有人知道请回复
#!/usr/bin/python
import serial, time
ser = serial.Serial()
ser.port = "/dev/ttyUSB0"
ser.baudrate = 115200
ser.bytesize = serial.EIGHTBITS
ser.parity = serial.PARITY_NONE
ser.stopbits = serial.STOPBITS_ONE
ser.xonxoff = False
ser.rtscts = False
ser.dsrdtr = False
try:
ser.open()
except Exception, e:
print "error open serial port: " + str(e)
exit()
if ser.isOpen():
try:
ser.flushInput()
ser.flushOutput()
while True:
print repr(ser.read(8))
except Exception, e1:
print "error communicating...: " + str(e1)
else:
print "cannot open serial port "
The solution to the synchronization problem is to read until the \\r
. 同步问题的解决方案是读到
\\r
。 That can be done by replacing your inner loop with: 这可以通过用以下内容替换你的内部循环来完成:
max_packet = 8
while True:
data = ser.read_until(terminator='\r', size=max_packet*2)
print repr(data[:-1])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.