0

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))

PuTTY Settings 1

PuTTY Settings 2

User Manual

PuTTY Settings 3

DaveL17
2,08112 gold badges30 silver badges48 bronze badges
asked Mar 26, 2021 at 17:47
8
  • Did you mean to send: cmd + '\n'? Commented Mar 26, 2021 at 17:54
  • Is that necessary? I just tried it and it didn't resolve the issue Commented Mar 26, 2021 at 19:21
  • The manual mentions the command ending in <CR> Commented Mar 26, 2021 at 20:08
  • Have you tried running the Python script against COM1 to see if that makes a difference? Commented Mar 27, 2021 at 12:17
  • You could also try cmd + '\r\n' to make it the same as the PUTTY setting of Implicit LF in every CR Commented Mar 27, 2021 at 12:37

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.