I am trying to use a python script to control a rotating valve. I am able to do so through PuTTY but not using my script. Code is shown below. I also included PuTTY settings and a link to the valve positioner user manual that gives example commands.
import serial
ser = serial.Serial()
ser.port = 'COM7'
ser.baudrate = 9600
ser.bytesize = serial.SEVENBITS
ser.parity = serial.PARITY_ODD
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))
out = ser.readline()
print('Receiving... ' + str(out))
DaveL17
2,08112 gold badges30 silver badges48 bronze badges
1 Answer 1
The manual that you link to says that it needs a <CR> at the end of each command.
Also your putty configuration says that it adds crlf.
You could change your code like this:
ser.write(str.encode(cmd + '\r\n'))
answered Mar 29, 2021 at 15:29
quamrana
39.5k13 gold badges57 silver badges77 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py
cmd + '\n'?<CR>cmd + '\r\n'to make it the same as thePUTTYsetting ofImplicit LF in every CR