Can't write multiple times to same port (pyserial)

浪尽此生 提交于 2020-01-23 12:37:06

问题


Solved: dsrdtr=True shouldn't have been used for software, only on the hardware being used

Hi I'm trying to write telegrams to serial port and can send one successfully. If I send more than one nothing happens. The script has to be closed at which point the first telegram is successfully received.

The manufacturer suggests a break of 50ms between telegrams, even with breaks >5s it still fails.

s = serial.Serial(
port='COM3',
baudrate=9600,
parity=serial.PARITY_ODD,
stopbits=serial.STOPBITS_ONE,
timeout=0,
bytesize=serial.SEVENBITS,
dsrdtr=True             
)

buttonUP=b'\x54\x30\x34\x0D'

s.write(buttonUP)
time.sleep(0.05)
s.write(buttonUP)
time.sleep(0.05)
s.write(buttonUP)

If instead I write

s.write(buttonUP)
s.close()
s.open()
s.write(buttonUP)

This works but the delay caused by closing/opening is too long for our requirements.

Does anyone have any ideas about what could be causing this issue? Thank you very much for your help


回答1:


If you are trying to read from the port by using a readline() function, it might be a problem that you don't terminate each message with a newline. Try changing it to

buttonUP=b'\x54\x30\x34\x0D\n' 

You can read more here



来源:https://stackoverflow.com/questions/49030196/cant-write-multiple-times-to-same-port-pyserial

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!