How to run PuTTY serial commands in Python/ PySerial

2.1k Views Asked by At

I'm trying to communicate with probe by serial COM port. Manufacturer make some commands in PuTTY ec. change measurement units or read some values. I write peace of code in python but I received nothing or I don't know what I recieved. Here is PuTTY configuration

1

2

3

Next is example of commands from manufacturer for PuTTY.

4

Here is code in PuTTY terminal:

5

My code in Python:

import serial

ser = serial.Serial()
ser.port = 'COM5'
ser.baudrate = 19200
ser.bytesize = serial.EIGHTBITS
ser.parity = serial.PARITY_NONE
ser.xonxoff = 0
ser.rtscts = 0
ser.dsrdtr = 0
ser.stopbits = 1
ser.timeout = 1
ser.open()

if ser.isOpen():
    print(ser.name + ' is open...')
    while True:
        cmd = input("Enter command or 'exit':")
        if cmd == 'exit':
            ser.close()
            break
        else:
            # ser.write(cmd.encode('ascii'))
            # ser.write(bytes(cmd, 'utf-8'))
            ser.write(str.encode(cmd + '\r\n')) #
            out = ser.readline().decode("utf-8").strip()
            print('Receiving... ' + str(out))

And here is what I received:

Enter command or 'exit':UNIT
Receiving...
Enter command or 'exit':exit
1

There are 1 best solutions below

0
On

Edit: you can pass in a timeout value to your serial instance. Try increasing this although i don't think thats the problem here. More likely the device your communicating with doesn't send newline / carriage return characters at the end of it's messages. In that case you'd have to use read(). But you should be able to check what characters are sent by the device by checking a box that says something like display newline characters or display CR/LF. I'm not sure if putty supports this, since i don't really use it as a serial monitor, but it probably does.