Search code examples
pythonvisagpib

Listening for GPIB events


I am controlling a test system using PyVisa/GPIB. The system is comprised of two separate testers (A and B) and a laptop. The the laptop passively listens for a GPIB message from tester A, when received the laptop triggers tester B.

I am using the following code to passively listen for events from tester A:

rm = visa.ResourceManager()
con = "GPIB0::3"
tester_A = rm.get_instrument(con, timeout=5000)
while True:
    event = None
    try:
        event = tester_A.read_raw()
    except VisaIOError:
        logger.warning("Timeout expired.")
    if event != None:
        # Do something

Is there a better way to listen and respond to events from tester A? Is there a better way to control this system via GPIB?


Solution

  • The approach you describe will work, but as you are experiencing, is not ideal if you are not quite sure when the instrument is going to respond. The solution lies in using the GPIB's service request (SRQ) functionality.

    In brief, the GPIB connection also provides various status registers that allow you to quickly check, for example, whether the instrument is on, whether an error has occurred, etc. (pretty picture). Some of the bits in this register can be set so that they turn on or off after particular events, for example when an operation is complete. This means you tell the instrument to execute a series of commands that you suspect will take a while, and to then flip a bit in the status register to indicate it is done.

    From within your software you can do a number of things to make use of this:

    • Keep looping through a while loop until the status bit indicates that the operation is complete - this is very crude and I wouldn't recommend it.
    • VISA has a viWaitOnEvent function that allows you to wait until the status bit indicatesthat the operation is complete - a good solution if you need all execution to stop until the instrument has taken a measurement.
    • VISA also allows you to create an event that occurs when the status bit has flipped - This is a particularly nice solution as it allows you to write an event handler to handle the event.