0

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()
8
  • Your script looks ok. Are you sure that you have disconnected your termite app whilst running this code? Could you experiment with a read() function which attempts to read chars one at a time, printing them? Commented Dec 20, 2023 at 18:43
  • Thanks quamrana - 100% certain that the port is disconnected. If it is not, then the python console will produce an error message. I will work on a read function that does single characters. Commented Dec 20, 2023 at 18:45
  • "Read output from serial device" is contradictory. "In" and "out" are relative to the host processor, so "output" is the data sent by the processor/device. But you seem to mean that your program has problems reading what should have been received. Commented Dec 20, 2023 at 20:10
  • Since you are using two way serial data, at the moment you cannot tell if 1. you are transmitting, 2. if the other device can receive this data, 3. the other device is responding and 4. if you are receiving anything. You need some extra diagnostics here. An oscilloscope would be handy, so would a loopback cable. Commented Dec 20, 2023 at 21:41
  • 2
    Thanks - I set up a logic analyser to monitor the tx and rx lines. Using termite, I can see serial data being transferred. However, nothing is visible when using the python script, it appears nothing is sent. Commented Dec 20, 2023 at 21:54

0

Your Answer

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.