[Python] pyvisa script only works when stepping through code

Discussão em 'Python' iniciado por Stack, Outubro 2, 2024.

  1. Stack

    Stack Membro Participativo

    I have written a small python script to enable the output of a HAMEG HMG4040 triple power-supply. I am using pyvisa 1.14.1 along with NI-VISA 20.0

    import pyvisa
    import time

    # Initialize the PyVISA resource manager
    pyvisa.log_to_screen()
    rm = pyvisa.ResourceManager()

    # List all connected VISA devices
    def list_devices():
    devices = rm.list_resources()
    if devices:
    print("Available VISA devices:")
    for idx, device in enumerate(devices):
    print(f"{idx + 1}: {device}")
    else:
    print("No VISA devices found.")
    return devices

    # Function to query the device
    def query_device(resource_name, command):
    try:
    # Open the device resource
    with rm.open_resource(resource_name) as instrument:
    instrument.timeout = 5000 # Set timeout to 1000 ms (1 seconds)
    # Write the query command to the device
    instrument.write(command)
    # Read the response from the device
    response = instrument.read()
    return response
    except Exception as e:
    print(f"Error querying device: {e}")
    return None

    # Function to query the device
    def send_cmd_device(resource_name, command):
    try:
    # Open the device resource
    with rm.open_resource(resource_name) as instrument:
    instrument.timeout = 5000 # Set timeout to 1000 ms (1 seconds)
    # Write the query command to the device
    instrument.write(command)
    return 1
    except Exception as e:
    print(f"Error querying device: {e}")
    return None

    # Main function
    if __name__ == "__main__":
    # List available devices
    devices = list_devices()

    if devices:
    # Choose the device you want to query
    resource_name = devices[0] # Use the first detected device for this example

    # Command to send to the device (replace with actual command)
    command = "*IDN?"

    # Query the device and get the response
    response = query_device(resource_name, command)

    if response:
    print(f"Response from HMG4040: {response}")
    else:
    print("No response from the device.")

    state = input("specify PSUP state: [ON/OFF]\n")

    #select and activate output 1,2 and 4

    with rm.open_resource(resource_name) as instrument:
    instrument.timeout = 1000 # Set timeout to 1000 ms (1 seconds)
    instrument.write("INST OUT1")
    instrument.write("OUTP:SEL " + state)
    instrument.write("INST OUT3")
    instrument.write("OUTP:SEL " + state)
    instrument.write("INST OUT4")
    instrument.write("OUTP:SEL " + state)
    instrument.write("OUTP:GEN " + state)

    send_cmd_device(resource_name, "INST OUT1")
    send_cmd_device(resource_name, "OUTP:SEL " + state)
    send_cmd_device(resource_name, "INST OUT3")
    send_cmd_device(resource_name, "OUTP:SEL " + state)
    send_cmd_device(resource_name, "INST OUT4")
    send_cmd_device(resource_name, "OUTP:SEL " + state)
    send_cmd_device(resource_name, "OUTP:GEN " + state)

    else:
    print("No devices to query.")


    If I comment out the "send_cmd_device()" lines the script is working fine either in debug mode or normal python execution.

    However, if I comment out this part:

    with rm.open_resource(resource_name) as instrument:
    instrument.timeout = 1000 # Set timeout to 1000 ms (1 seconds)
    instrument.write("INST OUT1")
    instrument.write("OUTP:SEL " + state)
    instrument.write("INST OUT3")
    instrument.write("OUTP:SEL " + state)
    instrument.write("INST OUT4")
    instrument.write("OUTP:SEL " + state)
    instrument.write("OUTP:GEN " + state)


    The script only works if I place a breakpoint and step, in debug mode, through each line including the "send_cmd_device()" lines.

    Could someone explain why the script doesn't work in normal full speed exection when using the method call? I have tried adding delays everywhere but that didn't help.

    Continue reading...

Compartilhe esta Página