'Can't read serial response using pyserial

Trying to use pyserial to talk to a Korad KD3005P power supply. (Using Python 3.6)

import serial
ser = serial.Serial('/dev/ttyACM0', baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=1)
ser.write(b'VSET1:2')
ser.write(b'VOUT1?')
response = ser.readline()
print(response)

VSET1:2 sets the voltage to 2 volts

VOUT1? tells the device to return the current voltage.

But all I get from the VOUT1? part seems to be:

b''

Stuff I've checked or tried without success:

  • The device works - I can talk to the device and get responses successfully with these commands through a serial terminal like CuteCom
  • Pyserial is kinda working - if I run the above program, the VSET1:2 does change the voltage.
  • I've tried different timeouts
  • I've tried ser.read() instead with various bit lengths
  • I've tried putting the read commands in a while loop

Any suggestions?



Solution 1:[1]

Oh.

It's a timing thing of some kind. Removing the VSET command or putting a time.sleep() command in between them and voila.. I get a response.

I'm going to mark this resolved but if anyone can enlighten me as to the nuances of this, thanks.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ti7mq