Feb 23, 2015
Rick Eis
[email protected]
Python / VISA Development Steps for W7
This document was published in December 2014. and updated in January 2015.
Please note that there may be newer versions of the .msi and .exe installation programs listed below.
This document intends to serve as a help guide to set up a system for Python Development on a Windows 7 machine
An underlining theme that permeates throughout this document is the overall goal of monitoring test equipment such as oscilloscopes, temperature chambers etc using National Instuments VISA
command language.
Currently there are four parts to this document
Part 1 describes the installation of Python and the steps to convert a python GUI created in QT to a python file.
Part 2 describes the process of installing the libraries necessary to interface with National Instruments VISA protocol.
Part 3 describes how to set up matrplotlib; a third party plotter which has the ability to display waveforms as well as other graphical
content as seen on the Oscilloscope display.
Part 4 explains how to convert a python project into an executable using the cx_Freeze library
Part 5 illustrates how to add an Icon as a resource file to Qt
Part 1
There are several different development configurations for running Python. This document describes the one which appears to be the most
preferred by developers at the time of this writing. (well as far as I could gather..) This document will go over theinstallation of this configuration in this first section.
The installations for this configuration (see table below) can easily be found on the web but I have also copied them to U:\eisric\Python\Py 3.4.0
First; Install Python3.4 , then the other three can be installed in any order.
Python is a programming language and Python 3.4 is one of the latest versions. To begin developing Python application, install Python 3.4
PySide is an add-on that is used with PyQt or Qt
PyQt is a Graphical User Interface development environment. The .ui file it creates can be converted to a .py as is shown later in this document.
Python3.4
Python
python-3.4.0.amd64.msi
Python 3.4 PySide-1.2.2(64 bit)
Support for Qt
PySide-1.2.2.win-amd64-py3.4.exe
PyQt GPL v4.11.2 for Python 3.4 (x64) Qt is a GUI development environment for Python
PyQt4-4.11.2-gpl-Py3.4-Qt4.8.6-x64.exe
Pycharm-community-3.4.1.exe
pycharm-community-3.4.1.exe
Nice (optional) IDE for Python development
For Windows machines make sure your environment path contains the following .
C:\Python34;
C:\Python34\Scripts;
C:\Python34\Lib\site-packages\PyQt4;
To do this you will need to update the PATH in your environment variables which can be found under advanced system settings. Make sure you reboot your computer after making the change.
After all installations are complete, Create a temporary directory where files for the following example in this document can be saved
then
follow the steps below to
1. Create a gui in Qt
2. Convert it to a .py
3. Edit it in PyCharm and then build the application and run it.
1. Open the Qt designer and Create a Main Window
Misc Page 1
2. Using controls from the Widget Box, drag any controls for this example over to working area
3. Save the file and name it window.ui for this example and save it to the directory you ceated above
4. Now follow these steps to convert the window.ui file into wiindowGUI .py using the PySide utility
program called pyside-uic
a. Open up a cmd window (any windows system32 or Visual Studio Command Prompt is fine)
b. Navigate to project directory where window.ui resides
c. Make sure that the path to C:\Python34\Scripts|pyside.uic is current or type it out (explicitly)
followed by the output file.
Misc Page 2
5. In this example the output file is called windowGUI.py and the project directory is C: \PythonExample
6. Run this from the command line: "pyside-uic window.ui -o windowGUI.py" as shown above
7. At this point we have converted the window.ui into windowGUI.py and need to create a new py file to
import the code from the windowGUI.py file. We cannot edit the windowGUI.py since any future GUI
changes would lose any updates when we re-ran pyside-uic command. Instead we create another .py file
to handle the code behind logic of the GUI. We'll call this file integrator .py for lack of a better name.
8. So, open up the IDE called pyCharm and create a new project as shown below. Make sure the location is
set to the same directory as above and use the default project name, then click OK.
If a different project name is given that is other than the default location, a new sub -directory will be
created to hole the project files.
9. Click Yes to create a project from the existing sources (these are the files we just created: window.ui and
windowGUI.py)
10. Open the project in a new window (This may not be an option on a new project when run the first (initial)
time.
11. If the project window is not displayed, select it
Misc Page 3
12. Right click on directory name and create a new python file; Integrator
13. Add the following code to Integrator.py:
-----------------------------------------------------------------------------------------------from PySide.QtCore import *
from PySide.QtGui import *
import sys
import windowGui
class MainW(QMainWindow, windowGui.Ui_MainWindow):
def __init__(self, parent=None):
super(MainW, self).__init__(parent)
self.setupUi(self)
app = QApplication(sys.argv)
form = MainW()
form.show()
app.exec_()
--------------------------------------------------------------------------------------------------
14. Finished . Now run the code
Part 2
Procedure to install libraries for VISA interface via Python
These installations can easily be found on the web but I also have copied them to U:\eisric\Python\Py
3.4.0
Python Visa Interface to NI's VISA library
pyvisa
PyWin32
PyWin32 provides bindings for the Win32 API
functions
pywin32-219.win-amd64py3.4.exe
VISA
National Instruments VISA library
NIVISA541full_downloader.exe
pip
installs packages; an easy install replacement.
Installed with Python 3.4
1. Install pyVisa which is the Python interface to National Instruments VISA library
Open up a command window and run the following
Misc Page 4
2. Next install pywin32-219.win-amd64-py3.4.exe
PyWin32 provides bindings for the Win32 API functions for which there are many,
So for example you can use win32 API calls from your python program
3. Run NIVISA541full_downloader.exe to install VISA library
Misc Page 5
( win32file.CopyFileEx(...)
4. Next attach a device (USB - GPIB) and run the following python script
import visa
rm = visa.ResourceManager()
print (rm.list_resources())
And you should receive an output similar to.. But if instead there is no GPIB device listed
you will need to load the National Instruments USB-GPIB driver ; NI4882_140_downloader.exe
which also can be found at U:\eisric\Python\Py 3.4.0\NI USB-GPIB Driver For W7
Part 3
Procedure to install matplotlib.
These binaries used for running Python on Windows can also be found at U:\eisric\Python\Py 3.4.0\Matplotlib
Misc Page 6
These binaries used for running Python on Windows can also be found at U:\eisric\Python\Py 3.4.0\Matplotlib
After installing matplotlib, install the other binaries or Python libraries which matplotlib requires
MatPlotLib a 2D plotting library
matplotlib-1.4.2.win-amd64-py3.4.exe
Numpy
a fundamental package needed for scientific computing with Python
numpy-MKL-1.9.1.win-amd64-py3.4.exe
Pillow
is a replacement for PIL, the Python Image Library, which provides image processing functionality and
supports many file formats.
Pillow-2.6.1.win-amd64-py3.4.exe
PyParsing
creates and executes simple grammars
pyparsing-2.0.3.win-amd64-py3.4.exe
DateUtil
extends the standard datetime module.
python-dateutil-2.3.win-amd64-py3.4.exe
Pytz
provides world time zone definitions, modern and historical.
pytz-2014.10.win-amd64-py3.4.exe
Setuptools downloads, builds, installs, upgrades, and uninstalls Python packages.
setuptools-5.8.win-amd64-py3.4.exe
Six
six-1.8.0.win-amd64-py3.4.exe
is a Python 2 and 3 compatibility library.
First, display a waveform on channel 2 and then
Run the following code to display the waveform
import visa
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def animate(i):
yInt = []
global ax1
tdsData = inst.query_binary_values('CURVe?', datatype='b', is_big_endian=True)
for currentY in tdsData:
yInt.append(currentY)
ax1.clear()
ax1.tick_params(axis='x', colors='b')
ax1.tick_params(axis='y', colors='b')
ax1.spines['bottom'].set_color('b')
ax1.spines['top'].set_color('b')
ax1.spines['left'].set_color('b')
ax1.spines['right'].set_color('b')
ax1.xaxis.label.set_color('b')
ax1.yaxis.label.set_color('b')
ax1.set_title("TDS 3054", color='b')
ax1.set_autoscaley_on(False)
ax1.set_ylim([-100, 100])
plt.ylabel('value')
plt.xlabel('points')
rect.set_facecolor('w')
ax1.plot(yInt, 'c', linewidth=3)
fig = plt.figure()
rect = fig.patch
ax1 = fig.add_subplot(1,1,1, axisbg='black')
rm = visa.ResourceManager()
inst = rm.open_resource('GPIB0::10::INSTR')
inst.write('DATa:SOUrce CH2')
inst.write('DATa:ENCdg RIBinary')
inst.write('DATa:WIDth 1')
inst.write('DATA:START 1')
inst.write('DATA:STOP 10000')
ani = animation.FuncAnimation(fig, animate, interval=100)
plt.show()
Part 4
Procedure to convert a python project into an executable.
First, install this binary which peforms the conversio. The file can be downloaded from the web but can
also be found at U:\eisric\Python\Py 3.4.0\cx_Freeze
Cx_Freeze
a executable conversion utility python files
Misc Page 7
cx_Freeze-4.3.3.win-amd64-py3.4.exe
Note: The source code that follows works with the example code that I have used for this evaluation.
Different projects may encounter problems and the reader may want to access the cx_freeze
documentation directly to solve problems as there is not a lot of information currently on the web.
http://cx-freeze.readthedocs.org/en/latest/
Process:
In the directory where the project resides, create a new python file which will have instructions for the
cx_Freeze utility as it performs the conversion. In this example the name for the file will be setup.py
setup.py contains the following code
import cx_Freeze
import sys
import matplotlib
Optional, cx_freeze can usually solve project dependencies
base = None
Name of main file in python project
if sys.platform == 'win32':
base = "Win32GUI"
This icon statement is optional. The result of this statement attaches
an icon to the exe file in the windows directory
executables = [cx_Freeze.Executable("mainTds.py", base=base, icon="VISA.ICO")]
cx_Freeze.setup(
name='tds',
options = {"build_exe": {"packages":["tkinter","matplotlib"], "include_files":["VISA.ICO"]}},
version = "0.01",
description = "Instruction to create TDS3054 exe",
executables = executables
)
To build the executable, run the following in a command window from the project directory
A successful build will yield a subdirectory in the project folder called build
With the converted executable
Misc Page 8
This icon file reference is used for the icon which is used in the
application. (Also optional if no icon is beign used)
Part 5 - Adding an Icon as a resource file to Qt
The main python windows Icon files can be embedded in the application by using this process.
In Qt click on the windowIcon down error of the MainWindow and Choose
Resource
Then click on the pencil
To bring up the Edit Resource dialog box
Click on the Add Prefix button (5th from left) and name the new prefix ( / )
Misc Page 9
Then click on the Add file icon (4th rom left) and select the file(s) to add. In this example I am adding the
VISA.ICO icon file to the resource file I named icons.qrc
Select the VISA.ICO file and click on OK
The VISA.ICO will be selected for the window icon
Misc Page 10
The last step uses the pyside rrc utility to convert the resource file into a python file and then convert
the GUI (.ui) into a python file as shown below
The application can now be run
Side Note:
Because the icon is embedded, it is no longer necessary to reference it in the setup file
when creating an executable as shown below. (see above
Still, the following modification will attach an Icon into the executable file listing but the icon file must
then be re added to the local directory as the setup build process cannot access the icon in the resource
file.
VISA.ICO references removed
Misc Page 11
VISA.ICO references removed
Misc Page 12