pyserial:我收到我发送的

pyserial: I received that I send

我试图通过 serial 的 com 从一台机器上得到答案。 但我不知道为什么我收到我发送的!

with serial.Serial(port,
                       baudrate=9600,
                       bytesize=serial.EIGHTBITS,
                       parity=serial.PARITY_NONE,
                       stopbits=serial.STOPBITS_ONE,
                       #timeout=0.5,
                       xonxoff=False,
                       rtscts=False,
                       dsrdtr=False
                       ) as ser:
        print(port,'opened!')

        ser.reset_input_buffer()
        o1='at+cgmr\r\n'
        x=ser.write(o1.encode()) #without encode() error!
        print('sended',x,'bytes','-->',o1)
        sleep(1)
        y=ser.readline(10) #ser.readline() is the same and read(10) as well

        print('answer',y)

输出为:

COM3 opened!

sended 8 bytes --> at+cgmr

respuesta b'at+cgmr\r\r\n'

理论上我必须收到 'ok' 或 'not ok'。 有什么想法吗?

可能设备会回显它收到的内容,然后发送回复。您是否尝试过增加接收的字节数?

如果你这样做 y = ser.read(100)(启用超时),你有可能得到你想要的一切。

另一种方法是读取尽可能多的可用字节:

y = ser.read(ser.in_waiting)

y = ser.read(ser.inWaiting())

(取决于pyserial的版本)。