I am trying to write a python script to interface with an AR488 GPIB to serial converter (documentation: here). I can write/read to the device perfectly fine using a terminal program (termite), but when I try to implement communication using a python script, I do not receive anything.
I verified functionality of the device and comms settings using termite:
successful tx and rx with device in termite
And verified the serial settings being used:
termite settings used for successful tx and rx
These match with the settings I use in my python script:
import serial
import time
def send(port, command):
time.sleep(0.1)
eol_char = '\r\n'
port.write((command+eol_char).encode("ASCII"))
def read(port):
time.sleep(0.1)
reply = port.readline()
decoded = reply.decode("ASCII")
return decoded.strip()
#Create an Serial instance
ins1 = serial.Serial('COM9',
baudrate = 115200,
bytesize=8,
timeout=1,
stopbits = serial.STOPBITS_TWO,
parity = serial.PARITY_NONE,
)
#Request ver
send(ins1,'++ver')
#Read and print the response
ans = read(ins1)
print('ver: ' + ans)
#Close the serial port when finished.
ins1.close()
However, when I run the script, the console only writes "ver:". My variable explorer tells me ans is a str of size 0. What am I missing?
EDIT: following quamrana's suggestion, I edited the script to the following, which I think should read in byte by byte. However, using breakpoints, I notice that the while loop (line 18) in readsingle, is skipped. Does this mean there are no bytes in the input buffer?
import time
def send(port, command):
time.sleep(0.1)
eol_char = '\r\n'
port.write((command+eol_char).encode("ASCII"))
def read(port):
time.sleep(0.1)
reply = port.readline()
decoded = reply.decode("ASCII")
return decoded.strip()
def readsingle(port):
time.sleep(0.1)
out = ''
while ins1.inWaiting() > 0:
out += ins1.read(1)
return out
#Create an Serial instance
ins1 = serial.Serial('COM9',
baudrate = 115200,
bytesize=8,
timeout=1,
stopbits = serial.STOPBITS_TWO,
parity = serial.PARITY_NONE,
)
#Request ver
send(ins1,'++ver')
#Read and print the response
#ans = read(ins1)
#print('ver: ' + ans)
ans2 = readsingle(ins1)
print('ver: ' + ans2)
#Close the serial port when finished.
ins1.close()
termiteapp whilst running this code? Could you experiment with aread()function which attempts to read chars one at a time, printing them?