문제

I am trying to send two lines of ASCII text to a device, but the device reads these two lines as one line appended. Is it matter of delay or wrong end char? I cant really seem to see the problem.

import serial
ser = serial.Serial('/dev/cu.usbserial-FTVFV0X7', 19200, timeout=10)
ser.write("x0")
ser.flush()
ser.write("11")
ser.flush()

The end device receives this as x011 instead of x0 and 11.

When tested from ordinary terminal program there is no problem receiving the two lines seperately.

도움이 되었습니까?

해결책

A lot of hardware serial devices use a carriage return to signal the end of a line or instruction. Without knowing what device you are trying to communicate wth (or the protocol it expects), I would probably suggest trying

ser.write("x0\r")
ser.flush()
ser.write("11\r")

As indicated in the documentation, pySerial does not support EOL parameters for readline() any longer so if the device is returning replies to you (and is doing so with \r terminated lines) then you should do as advised and use io.TextIOWrapper and appropriate timeouts to parse incoming data as well.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top