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

前端 未结 4 1888
清歌不尽
清歌不尽 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:28

    In order to read you need to open a listening port(with a timeout) first, for example:

    ser = serial.Serial('/dev/cu.usbserial-A901HOQC', 19200, timeout=5)
    x = ser.read()          # read one byte
    s = ser.read(10)        # read up to ten bytes (timeout)
    line = ser.readline()   # read a '\n' terminated line
    ser.close()
    

    See more details here.

提交回复
热议问题