Using PySerial is it possible to wait for data?

前端 未结 3 1961
萌比男神i
萌比男神i 2020-12-08 08:00

I\'ve got a Python program which is reading data from a serial port via the PySerial module. The two conditions I need to keep in mind are: I don\'t know ho

3条回答
  •  不知归路
    2020-12-08 08:30

    def cmd(cmd,serial):
        out='';prev='101001011'
        serial.flushInput();serial.flushOutput()
        serial.write(cmd+'\r');
        while True:
            out+= str(serial.read(1))
            if prev == out: return out
            prev=out
        return out
    

    call it like this:

    cmd('ATZ',serial.Serial('/dev/ttyUSB0', timeout=1, baudrate=115000))
    

提交回复
热议问题