# Python Example Scripts

Python Example Scripts

- Python Example Scripts
- Connection Scripts
- Measurement Scripts
- Marker Scripts
- Math Operator Scripts
- Data Transfer Scripts
- Miscellaneous Scripts

This section illustrates using Python 3 to control Infiniium 2026. Even if you don't use Python, these programs demonstrate the commands needed to perform specific tasks. The examples are not provided to teach Python. All of the examples are written for Python 3 and were all tested using the following environment:

- Personal computer with the Windows 11 operating system

- Keysight IO Libraries Suite installed for VISA (Virtual Instrument Software Architecture) library. Go to www.keysight.com/find/iosuite

- Python 3.The Python language can be downloaded from the web at https://www.python.org/

- PyVISA package which is a Python front end for the VISA library. To learn about PyVISA, go to https://pyvisa.readthedocs.io/

- Some examples use the matplotlib package for plotting captured data. To learn about matplotlib, go to https://matplotlib.org/stable/

All of the examples shown in this section present a Python script that establishes a LAN connection using the HiSLIP interface.

Some firewall applications might block SICL/LAN communications.

Real-time oscilloscopes must be calibrated before running any script.

Do not modify the line indents of any lines within the script. The indentation of code lines in Python identifies code blocks and is critical to the ability of the code to run.

Copyright © 2016-2025 Keysight Technologies Inc. All rights reserved. You have a royalty-free right to use, modify, reproduce and distribute these example files (and/or any modified version) in any way you find useful, provided that you agree that Keysight has no warranty, obligations or liability for any sample application files.

The following Python script can be used to confirm your Python environment while connecting to Infiniium 2026 Offline on your PC. The script connects to Infiniium on your PC and lists any local oscilloscopes that are available in Keysight's connection expert.

pyvisa-test-and-idn.py

    #!python3

    """This program prints the installed Python and VISA versions,
    checks the VISA environment, and opens a connection with the
    Infiniium XR8 oscilloscopes.
    """

    # Import modules.
    import pyvisa
    import sys

    # Global variables.
    oscilloscope_visa_address = "TCPIP0::localhost::hislip0,4880::INSTR"
    timeout = 20000  # 20 seconds.

    def exit_program():
        """Exit program."""

        # Close connection to oscilloscope.
        sys.stdout.write("Closing oscilloscope connection.\n")
        Infiniium.write(":SYSTem:GTLocal")  # Unlock GUI.
        Infiniium.clear()  # Clear oscilloscope communications interface.
        Infiniium.close()  # Close communications interface to oscilloscope.
        rm.close()  # Close resource manager.
        print("End of program.")
        sys.exit()

    # ==========================================================
    # Main program:
    # ==========================================================

    # Connect and initialize oscilloscope.
    rm = pyvisa.ResourceManager("C:\\Windows\\System32\\visa64.dll")
    try:
        Infiniium = rm.open_resource(oscilloscope_visa_address)
    except Exception:
        print(
            f"Unable to connect to oscilloscope at {oscilloscope_visa_address}. Aborting program."
        )
        sys.exit()

    Infiniium.timeout = timeout  # Set global timeout.
    Infiniium.clear()  # Clear the instrument bus.
    # Lock GUI happens automatically.

    # Print Python and PyVISA versions.
    print(f"Python version: {sys.version}")
    print(f"PyVISA version: {pyvisa.__version__}")

    # Check VISA environment.
    print("List of instruments discovered by Keysight Connection Expert:")
    i = 1
    for key, value in rm.list_resources_info().items():
        print(f"Instrument {str(i)}: {key}")
        print(f"  Interface type: {value.interface_type}")
        print(f"  Interface board number: {value.interface_board_number}")
        print(f"  Resource class: {value.resource_class}")
        print(f"  Resource name: {value.resource_name}")
        print(f"  Resource alias: {value.alias}")
        i += 1

    # Get and display the device's *IDN? string.
    idn_string = Infiniium.query("*IDN?").strip()
    print(f"Identification string: '{idn_string}'")

    # Load the default setup.
    Infiniium.write("*RST")

    # Exit program.
    exit_program()

Although uncommon, you may run into a situation where the "byteness" of the Python installation does not match the "byteness" of the Visa library. If this happens, you can force the instantiation of the PyVISA object with the proper Visa version. For example, instead of using:

    rm = visa.ResourceManager()
    Infiniium = rm.open_resource('TCPIP0::localhost::hislip0,4880::INSTR')

Pass an argument to include the correct dll:

    rm = visa.ResourceManager(r'C:\WINDOWS\system32\visa64.dll')
    Infiniium = rm.open_resource('TCPIP0::localhost::hislip0,4880::INSTR')