python and serial. how to send a message and receive an answer

前端 未结 4 1856
清歌不尽
清歌不尽 2021-01-22 13:05

I have to send ZANE:1:00004:XX_X.X_XXXX_000XX:\\r\\nvia serial communication with python.

here is my code:

import serial
ser = serial.Seria         


        
4条回答
  •  北荒
    北荒 (楼主)
    2021-01-22 13:41

    Your device is probably not terminating its response with a newline character. the .readline() method is expecting a newline terminated string. See here: http://pyserial.sourceforge.net/shortintro.html#readline for more info.

    try setting a timeout on your serial connection

    ser.timeout = 10
    

    and replace the ser.readline() with ser.read(n) where n is the number of characters you wish to read. ser.read(100) will try to read 100 characters. If 100 characters don't arrive within 10 seconds, it will give up and return whatever it has received.

提交回复
热议问题