PH415 Computer Interfacing 2013
OSU logo
Python and USB Instruments

Table of Contents


  • Confirm that the most recent versions of the following Python software packages are available on your computer. If not, then log in and download the newest versions by clicking on View Files and following the Python path to the zip files:
    • Marina
    • Measure the transmission function A(ω) of a circuit using
    • Simulate the response A(ω) of a circuit using
    • Plot the simulations, data and both together using


  • This task will introduce programming with Python, communicating with USB-based instruments and simple signal processing.
  • The experiments are the following: I(V) response of a purely resistive object; complex impedance spectrum of an object.

Basic Python Programming

  • Open a DOS session and run the command "python". Follow the programming instructions.
  • From start menu -> python, run Idle, a simple IDE, and follow the programming instructions.
  • From the start menu, run geany, a real IDE for several programming languages and even LaTeX. Compose and execute simple programs outlined by the instructor.
  • On the Python Wiki site, go to the "Basics" page and experiment with the programs.
  • Copy and execute the simple plotting program provided by the instructor to generate a data set using the numpy module and plot it using the pylab module. The pylab module is part of the matplotlib package which provides Matlab style graphing functionality. Read the Python page for more information.

Instrumentation Communication Resources

  • On the PH415 Spring 2012 site, the instrumention page has a wealth of information.
  • The VISA protocol is important and will be the basis for controlling high-level instrumentation. There is, of course, a Python module to facilitate this. Visit the PyVisa site and read the documentation.

Viewing USB Device Parameters

  • Use control panel -> system -> hardware -> device manager to get some information about USB controllers and connected devices. Pay particular attention to device information, such as vid and pid, and the service or driver used, such as usbstor for flash drives, usbhib for a mouse, and niusbtmc (niusbtmc.sys in c:\windows\system32\drivers) for an oscilloscope.
  • Unzip and execute usbview, a utility from M$ to see some detailed information about the USB configuration for the scope.
  • Unzip and execute SniffUSB to see the transactions with USB devices on your machine. The documentation is helpful. Hit the view log button to see some USB transactions. Be thankful than you do not have to deal with communications at this level.

Recognizing VISA Compatible Instruments

  • All programs have been updated to include the ability to use a visa module simulator and to create log files.
  • To simulate the PyVisa module outside of the laboratory, copy to your working directory. This simulates the functions of the real visa module. The "import visa" in your programs will look for in your working directory first and then seach the PYTHON_PATH. To use as the visa module, comment out the "import visa as v" line and add "import visa_fake as v".
  • The program find visa devices uses PyVisa to find compatible instruments on the USB, the GPIB bus, com ports and lpt ports.
  • Modify the program to create a dictionary of VISA-compatible (VC) instruments on the USB. Compare your program to the instructor's program find visa usb devices.
  • These first two programs have been condensed into visa devices, which is a generally useful module to be imported into higher level programs. Notice the structure of the dictionary of detected instruments.
  • Write another module to create a dictionary of VISA-USB devices that have been detected and which will be supported syntactically by the user. Begin this module with the "import visa_devices as vd" statement. Compare your program to the instructor's This parses a text file describing the user-supported devices and then constructs a dictionary of connected and recognized instruments. Included in the dictionary entry for each instrument is the "handle", the operating system link to the communication pipeline for that instrument. These files must be in the working directory along with supported_instruments.txt and
  • This last program can now be imported as a module in a grander program that will communicate with the instruments.

Syntax For Instrumental Communication

  • The Tektronix and Rigol instruments used in PH415 have communication syntaxes which conform to the Standard Commands for Programmable Instruments (SCPI). Peruse this document before digesting the programming manuals for the instruments you will use.
  • In general, a programmable instrument might conform to the Interchangeable Virtual Instruments - Scope Class specifications which are controlled by the IVI Foundation, a Microsoft/National Instuments (LabView)/MathWorks (Matlab) syndicate. SCPI has been consumed by this organization and remains unchanged, if not orphaned, since 1999.
  • Establish basic communications with the instruments by reading the PyVisa documentation. Even just the first example in this document is sufficient. Open an IDLE session and converse with an instrument. Using Geany, write a very short Python program to both set and read one parameter for each instrument.
  • Familiarize yourself with the printed operation manuals for the TDS1012B oscilloscope and the AFG3021B function generator. On the oscilloscope page, read about the programming syntax. On the waveform generator page, read about the syntax.

Creating Instrument Objects in Python

  • Go to the Marina site and follow the path "Site Map" --> "Introduction" --> "API". View all of the classes used in Marina and examine the classes for the oscilloscopes and arbitrary waveform generators (awgs). These are too complicated to work with in the limited time at hand, but the concept of building a system of modules and an explanatory API is central to constructing experimental control, data acquisition and data analysis programs.
  • Write a module called containing a Python class for each instrument with simple read and write functions. Then write a test routine which will query each instrument for some parameter settings and set some parameters. You will need to import and include a routine to create instances of the instrument classes as instruments are recognized. Compare your program to the instructor's version Use this program as a module to be imported into a program at the next level.

Documenting Programs and Creating an API

  • Documenting a program must be an integral part of the development process. The Doxygen program documentation system package has been installed on the computers. Read the doxygen manual- and incorporate the correct type of comment lines into so that Doxygen can interpret them properly. Doxygen will generate a set of htmls that can be opened with a browser.
  • In order to use Doxygen or any similar documentation system, organize your Python modules in a logical directory structure. Suggested structure: directory tree.
  • Run the Doxygen GUI and set the working directory to c:\documents and settings\ph411\ph415_s13\doxygen. Then, choose the "wizard" and set the project name to "Instrument Classes". Sequentially select the topics on the "Topics" panel and set several parameter as given below:
    • Projects: Choose as the source code directory c:\documents and settings\ph411\ph415_s13\code\instrument_classes; Choose as the destination directory c:\documents and settings\ph411\ph415_s13\documentation\instrument_classes.
    • Mode: Check the "All entities","Include cross-referenced source code in the output" and "Optimize for Java ...".
    • Output: html, with navigation panel, with search function, LaTeX, as intermediate format for pdf.
    • Diagrams: Use built-in class diagram generator.

Configuring Instruments for Experimental Measurements

  • To send multiple commands to and read multiple parameter values from an instrument in a way that can be easily changed, it is logical to create a text file of commands, such as initialize_fg_scope_commands.txt. This file can then be parsed and used with a program such as This older program is not compatible with your module of instrument classes, so write a new program called which will parse a new file configure_instruments.txt, recognize instruments, create instrument objects and execute the commands in configure_instruments.txt.
  • Instructor's version (place these files together in a "configure_instruments" directory) :, configure_instruments.txt,,, supported_instruments.txt,, To run without PyVisa or a real scope and wg, rename as The program simulates a scope and a wg to a limited extent.

Conduct an Experiment Using a Waveform Generator and an Oscilloscope

  • The next goal is write a program to set some important parameters on the awg and scope automatically, perhaps set other parameters by twiddling the knobs and pushing buttons, acquire waveforms from both channels of the scope, plot both waveforms and save both waveforms in a csv file.
  • Begin by copying the Instructor's package of modules into a directory ...\code\scope_expt.
    This directory must be on the machine's C: drive, not O:, T: or Z:.
    The package:, configure_instruments.txt, configure_instruments_for_scope_expt.txt,,,, supported_instruments.txt,,
  • Change the wg and scope information in configure_instruments_for_scope_expt.txt for your particular instruments. The program is basically an empty shell, with only two simple functions defined: TestWithInitialization() and TestSansInitialization(). Execute with TestWithInitialization() uncommented just to make sure all is well.
  • Open with geany and notice that the TekScope class has been embellished. If you execute it, data will actually be acquired from the scope, saved as a csv file and plotted. Two avoid an error condition, make sure that both scope channels are displayed. The Test() function prints much diagnostic information because it was written just for testing purposes. Change the data encoding directive fron 'ascii' to 'binary' in the statement: error, x, y = scope.GetWaveform([1], 'ascii'). Binary is the preferred, faster mode of waveform acquisition.
  • Your task is to write a main program in called Main() which will use the capability of as demonstrated in Test(). When finished with your program, use Doxygen to document all the python modules in the directory.