Python Examples

This section illustrates using Python 3.9 to control FlexDCA. Even if you don't use Python, these scripts will help you to understand the program requirements. The examples were all tested using the following environment:

  • Windows 10 PC
  • Keysight IO Libraries Suite installed for VISA (Virtual Instrument Software Architecture) library. Go to http://www.keysight.com/find/iosuite.
  • PyVISA which is a Python front end for the VISA library. To learn about PyVISA, go to https://sourceforge.net/projects/pyvisa/ and to pyvisa.readthedocs.org.

In any of the script topics, you'll find the script's code as shown in the following picture. Click the Copy button to copy the code to Windows clipboard and paste the code into your coding application.

General Notes

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

Some firewall applications might block SICL/LAN communications.

DCA-X modules must be calibrated before running any script. For TDR modules, a TDR step calibration must also be performed.

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 – 2023 Keysight Technologies Inc. All rights reserved. You have a royalty-free right to use, modify, reproduce and distribute this 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.

Python Environment

The following Python script can be used to confirm your Python environment while connecting to N1010A FlexDCA on your PC. The script connects to FlexDCA on your PC and lists any local DCA-Xs that are available in Keysight's connection expert.

Copy

pyvisa-test-and-show.py

# -*- coding: utf-8 -*-
""" Requires FlexDCA running on a PC or instrument. Edit the ADDRESS constant
if not running on a PC. Python and the VISA library must have the same
"bitness": 32 or 64-bit. Both 32 and 64-bit versions the VISA library are
installed by Keysight Connection Expert. PyVISA is the frontend for the
VISA library.
This script prints the installed Python and VISA versions and
checks the VISA environment and opens a connection with
FlexDCA that is running on a PC. """

import sys
import pyvisa as visa  # PyVISA library

ADDRESS = 'TCPIP0::localhost::hislip0,4880::INSTR'

rm = visa.ResourceManager(r'C:\WINDOWS\system32\visa64.dll')
print('\nVISA library version:\n  ', rm)
print('\nPython version:\n  ', sys.version)
print('\nList of instruments discovered by Keysight Connection Expert:')
i = 1
for key, value in rm.list_resources_info().items():
    print('\nInstrument ', str(i), ': ', key)
    print('  Interface type: ', value.interface_type)
    print('  Interface board number: ', value.interface_board_number)
    print('  Resource class: ', value.resource_class)
    print('  Resource name: ', value.resource_name)
    print('  Resource alias: ', value.alias)
    i += 1
FlexDCA = rm.open_resource(ADDRESS)
FlexDCA.timeout = 10000  # 10s
FlexDCA.write_termination = '\n'
FlexDCA.read_termination = '\n'
print('\nVISA termination string (write) set to newline: ASCII ',
      ord(FlexDCA.write_termination))
print('VISA termination string (read) set to newline: ASCII ',
      ord(FlexDCA.read_termination))
print('FlexDCA ID string:\n  ', FlexDCA.query('*IDN?'), flush=True)
FlexDCA.query('*RST;*OPC?')
FlexDCA.write(':SYSTem:GTLocal')
FlexDCA.close()

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()
Flex = 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')
Flex = rm.open_resource('TCPIP0::localhost::hislip0,4880::INSTR')