Search code examples
python-3.xpyserial

Writing to serial port works via Putty but not PySerial


I am able to send a command string to the serial port via Putty and get a response. However, when I try the same using Python PySerial read/write I am not able to send read/write commands.

Putty terminal: example-1:

 <command_string>
    response = Success

Example-2:

<incorrect_command_string>
response = Fail

Python code:

serialData = serial.Serial(port=2, baudrate=921600, parity=serial.PARITY_NONE,
                                   stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS)
serialData.write(b'<command_string>')
print(serialData.in_waiting)
print(serialData.read(serialData.in_waiting))

Output of the code:

0  
b''

Any suggestions?


Solution

  • Found it: The response string has '\r'. So I am able to use read_until as below and eliminate the sleep.

    serialData.read_until("\r".encode('utf-8'))