Ben Kilada
Applications Engineer
#audiotest #APx500software #python

Controlling APx500 Software with Python

For engineers looking to develop API programs using recent versions of Python, this post will support users in developing those solutions. Consider this a "Getting Started Guide" for APx500 Software and Python.

Controlling APx500 Using Python

When writing a script to control the APx500 software, the APx500 API (Application Programming Interface) must be referenced so that API commands can successfully compile at run-time. Since the API libraries are packaged into .NET assemblies (dll files) that can only be executed in a .NET runtime environment, the standard Python language cannot be used. Support for .NET libraries must be added using a custom version of the language (IronPython) or an add-on package (Pythonnet).

While both IronPython and Pythonnet are workable solutions, Pythonnet may be preferred in situations where using standard Python or a newer version of Python is required. As of December 2020, IronPython is still based on Python 2.7, whereas Pythonnet supports multiple versions, up to version 3.8.

Note that although Python is a cross-platform language, API control of APx500 is only available on operating systems supported by the APx500 application. A list of supported operating systems can be found here.

To get started controlling APx500 using Python and Pythonnet, follow the steps below.

Step 1: Install Python

Before installing Python, the list of versions supported by Pythonnet should be referred to using the Pythonnet GitHub repository available at: Pythonnet GitHub Repository.

Scroll down and look for a section indicating the list of supported Python versions. As of December 2020, the list of supported versions (as shown in the image below) are 2.7, 3.5, 3.6, 3.7, and 3.8.

Figure 1: Supported versions of Python.

Once compatibility has been confirmed, download and install the relevant version of Python from www.Python.org.

Step 2: Add Python to the Windows Path

Once Python has been installed, the Windows Path must be modified to allow Python commands to be executed from the Windows command prompt. To do so (in Windows 10):

1. Use the Windows taskbar to search “Edit the system environment variables”. Press enter to Open the System Properties dialog.

Figure 2: Open the System Properties dialog.

2. Click the “Environment Variables…” button.

Figure 3: Select "Edit Variables".

3. Under “System Variables”, find “Path” and click Edit…”.

Figure 4: Under “System Variables”, find “Path” and click Edit…”.

4. To find the Python installation directory that will be added to the Path: using the Windows taskbar, search for “Python”. Right-click the version of Python you intend to use and select “Open file location”.

Figure 5: Select “Open file location”.

5. Select the shortcut to Python and click the “Open location” button on the “Manage Shortcut Tools” tab of the Windows file explorer.

Figure 6: Select the shortcut to Python and click the “Open location” button.

6. Copy the full file path from the Explorer address bar.

7. Go back to the “Edit environment variable” dialog and click “New”.

8. Paste the copied Python path into the new path entry.

Figure 7: Create a new path entry.

9. Click “New” again, paste the copied path and append “\Scripts\” to the end.

10. Click OK to confirm changes in both the “Edit environment variable” and “Environment Variables” windows.

To confirm that Python was successfully added to the path, open a Windows Command Prompt and type “python --version” (two dashes). The Command Prompt should print out the version of Python added to the Windows path (e.g., Python 3.7.4).

Step 3: Install Pythonnet using a package manager

Once Python has been installed and added to the Windows path, the Pythonnet package can be installed using pip, a package manager for Python. Pip is automatically installed with newer versions of Python.

To install Pythonnet using pip:

1. Open a Windows Command Prompt.

2. Type “pip install pythonnet”.

You are now ready to write code to control APx500 using Python and Pythonnet.

Click here to download (AP.com account required) an example Python script demonstrating how to reference the APx500 API, initiate the software, load a project file, run a sequence, and get data acquired in a sequence.

For steps on developing an API program using IronPython, read this article: Controlling APx500 with IronPython.