email me.mailto:robertrau12@yahoo.com?subject=PyVISA

Visit some of my other pages:

Electronic Design ToolsElectronics.html
Rocketry ApplicationsRocketry.html
Macintosh Compatible Test EquipmentMac-Instruments.html
SchematicsSchematics.html
Home > Robert_Rau.html
Links >Links.html
Computer based Instruments >Mac-Instruments.html
Developing Mac ApplicationsDesktopProgLinks.html

PyVISA

The lowest level piece that must be installed is the free National Instruments VISA runtime library. There are several versions, select the one that is the latest for the version of Mac OS X you are running. The rest of this page was written with version 5.12 running on Mac OS X version 10.7.4


Version 5.1.2 for OS X 10.6 and 10.7 can be down loaded from:

http://joule.ni.com/nidu/cds/view/p/id/2919/lang/en

Version 5.0 for OS X 10.5 and 10.6 can be downloaded from:

http://joule.ni.com/nidu/cds/view/p/id/2043/lang/en

Version 4.5 for OS X 10.4.8 and 10.5 can be downloaded from:

http://joule.ni.com/nidu/cds/view/p/id/1268/lang/en


This installation is done in the Finder and does not need the Terminal program.

National Instruments VISA Runtime Library

Home > Robert_Rau.html
Links >Links.html
Mac Programming >DesktopProgLinks.html

OR

PyVISA

PyVISA

This is a compilation of information from several sites on the internet and a lot of trial and error on my part. I hope this page is useful to those automating lab and production instruments, and if not, I hope I get feedback to achieve that goal. An incredible amount of work has been done on the software components and the documentation described here; I am just filling in some gaps in information for implementing this on OS X. All this work was done on my MacBook Pro, 2.66GHz running Mac OS X 10.7.4 (Lion).

PyVISA is the link between the National instruments library and Python. You can download the latest version from:

http://sourceforge.net/projects/pyvisa/

This installation is done in Terminal. I have a page here on some tips and issues installing programs using Terminal. In general you shouldn’t need to worry about most of it, but it is a good place to start if your install fails. I installed PyVISA using the bash shell.

After un-zipping, move the folder into a place easy to get to with Terminal. I simply dragged the PyVISA-1.4 folder to my Documents folder. The file README in the PyVISA-1.4 folder has the install instructions. You need to be connected to the internet for the install to work. Be prepared for a long stream of text during the install/build process. You will also need root privileges for the installation, so for the command line you should use:

sudo python setup.py install

You will be asked for your login password because sudo is asking for root privileges.


The README file mentions ctypes as a prerequisite. Since Python version 2.5, ctypes has been included in Python.

PyVISA

OS X includes several versions of Python. At the time of this writing the PyVISA library builds as a 32 bit library. Your active version of Python must match; the main page shows how to select your default Python. Also, as of this writing, PyVISA is not compatible with Python 3.x. I use version 2.7. To write scripts I use Text Wrangler from Bare Bones Software. It is free and it has a syntax highlighter that understands Python. You can write a little Python script to print the version of your Python.


import sys

print sys.version[:3]


Python documentation:

http://docs.python.org/


From the main page for Python you will find the command to make the default Python 32 bit:

defaults write com.apple.versioner.python Prefer-32-Bit -bool yes


The main page can also be found here on the web:

https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/python.1.html


There is also Intel’s accelerated Python:

https://software.intel.com/en-us/intel-distribution-for-python?utm_campaign=cmd_12116-4&utm_source=direct&utm_medium=email&utm_content=python_bodycopy_link5

Setting up Python

If you were able to run the Python version script above, then Python itself should be OK.


from pyvisa.vpp43 import visa_library

visa_library.load_library("/Library/Frameworks/Visa.framework/VISA")

import visa

print visa.get_instruments_list()


With no instruments plugged in the USB connectors, you should see something like:


['ASRL1', 'ASRL2', 'ASRL3', 'ASRL4']


If you got an error like this:


Traceback (most recent call last):

  File "test.py", line 6, in <module>

    visa_library.load_library("/Library/Frameworks/Visa.framework/VISA")

  File "/Library/Python/2.7/site-packages/PyVISA-1.4-py2.7.egg/pyvisa/vpp43.py", line 146, in load_library

    self.__lib = self.__cdecl_lib = cdll.LoadLibrary(path)

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 431, in LoadLibrary

    return self._dlltype(name)

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 353, in __init__

    self._handle = _dlopen(self._name, mode)

OSError: dlopen(/Library/Frameworks/Visa.framework/VISA, 6): no suitable image found.  Did find:

/Library/Frameworks/Visa.framework/VISA: no matching architecture in universal wrapper


...this is a mismatch of Python and PyVISA build types (32 or 64 bit issues). Try switching to 32 bit as described in the Setting Up Python section above, or changing it to 64 bit if you were able to build your PyVISA as 64 bit.

Testing the installation

In this example we will use the Rigol DG1022 function/arbitrary waveform generator. Rigol has many instruments that follow the National Instruments VISA interface. Here is the link to Rigol’s VISA Runtime forApple OS X PDF:

http://beyondmeasure.rigoltech.com/acton/attachment/1579/f-00cd/0/-/-/-/-/file.pdf

The DG1022 uses a USB interface. To find the USB ID of the instrument you hit the following keys; Utility, (right arrow soft key), (I/O soft key) and you are presented with this screen:

Instruments

If you use this USB ID you are faced with this Python error:


Traceback (most recent call last):

  File "test.py", line 9, in <module>

    MyInstrument = visa.instrument("USB0::0x1AB1::0x0400::DG1D124104464::INSTR")

  File "/Library/Python/2.7/site-packages/PyVISA-1.4-py2.7.egg/pyvisa/visa.py", line 294, in instrument

    return Instrument(resource_name, **keyw)

  File "/Library/Python/2.7/site-packages/PyVISA-1.4-py2.7.egg/pyvisa/visa.py", line 358, in __init__

    "lock")))

  File "/Library/Python/2.7/site-packages/PyVISA-1.4-py2.7.egg/pyvisa/visa.py", line 132, in __init__

    keyw.get("lock", VI_NO_LOCK))

  File "/Library/Python/2.7/site-packages/PyVISA-1.4-py2.7.egg/pyvisa/vpp43.py", line 753, in open

    byref(vi))

  File "/Library/Python/2.7/site-packages/PyVISA-1.4-py2.7.egg/pyvisa/vpp43.py", line 398, in check_status

    raise visa_exceptions.VisaIOError, status

pyvisa.visa_exceptions.VisaIOError: VI_ERROR_RSRC_NFOUND: Insufficient location information or the requested device or resource is not present in the system.

Most Python errors are these long multi-line things. Basically it is saying it can’t find the USB ID you specified.

Unfortunately, with the 00.02.00.06.00.02.07 version of firmware in the DG1022, this USB ID incorrect. With the following little Python program you can find the real USB ID:


from pyvisa.vpp43 import visa_library

visa_library.load_library("/Library/Frameworks/Visa.framework/VISA")

import visa

print visa.get_instruments_list()


With the DG1022 I used, its real USB ID is:


USB0::0x1AB1::0x0588::DG1D124104464::INSTR


This trick works with USB instruments, but not TCPIP or serial-based instruments (it doesn’t go out and scan your entire Ethernet network, no surprise).


Remember after sending the DG1022 commands, it is in remote mode (it is not crashed). To return to front panel control, hit the VIEW button under the USB connector.


To write a script you will need to have the programming manual for your instrument. Every one has its unique blend of commands. The Rigol DG1022’s manual can be found here:

http://cdn1.actonsoftware.com/acton/cdna/1579/f-0038/0/0


The basic first program for any instrument is asking it to identify itself.


from pyvisa.vpp43 import visa_library

visa_library.load_library("/Library/Frameworks/Visa.framework/VISA")

import visa

MyInstrument = visa.instrument("USB0::0x1AB1::0x0588::DG1D124104464::INSTR")

MyCommand = "*IDN?"

print MyInstrument.ask("*IDN?")


...and you get:


RIGOL TECHNOLOGIES,DG1022 ,DG1D124104464,,00.02.00.06.00.02.07


All seems well until you start adding more command exchanges.  If you make this script:


from pyvisa.vpp43 import visa_library

visa_library.load_library("/Library/Frameworks/Visa.framework/VISA")

import visa

MyInstrument = visa.instrument("USB0::0x1AB1::0x0588::DG1D124104464::INSTR")

MyCommand = "*IDN?"

MyInstrument.write(MyCommand)

MyResult = MyInstrument.read()

MyCommand = "VOLT:UNIT VPP"

MyInstrument.write(MyCommand)

MyCommand = "APPL:SIN 20000,2.5,0.5"

MyInstrument.write(MyCommand)

MyCommand = "OUTP ON"

MyInstrument.write(MyCommand)


...you may get this error:


Traceback (most recent call last):

  File "test44.py", line 22, in <module>

    MyResult = MyInstrument.read()

  File "/Library/Python/2.7/site-packages/PyVISA-1.4-py2.7.egg/pyvisa/visa.py", line 433, in read

    return self._strip_term_chars(self.read_raw())

  File "/Library/Python/2.7/site-packages/PyVISA-1.4-py2.7.egg/pyvisa/visa.py", line 407, in read_raw

    chunk = vpp43.read(self.vi, self.chunk_size)

  File "/Library/Python/2.7/site-packages/PyVISA-1.4-py2.7.egg/pyvisa/vpp43.py", line 840, in read

    visa_library().viRead(vi, buffer, count, byref(return_count))

  File "/Library/Python/2.7/site-packages/PyVISA-1.4-py2.7.egg/pyvisa/vpp43.py", line 398, in check_status

    raise visa_exceptions.VisaIOError, status

pyvisa.visa_exceptions.VisaIOError: VI_ERROR_TMO: Timeout expired before operation completed.


It seems the Rigol DG1022 isn’t able to keep up with the commands. To get around this I added delays. You must import the time library. After a little cleanup I ended up with this script:


from pyvisa.vpp43 import visa_library

visa_library.load_library("/Library/Frameworks/Visa.framework/VISA")

import visa

import time

print visa.get_instruments_list()

RigolDG1022 = "USB0::0x1AB1::0x0588::DG1D124104464"

if RigolDG1022 in visa.get_instruments_list():

MyInstrument = visa.instrument("USB0::0x1AB1::0x0588::DG1D124104464::INSTR")

MyCommand = "*IDN?"

MyInstrument.write(MyCommand)

time.sleep(0.1)

MyResult = MyInstrument.read()

time.sleep(0.1)

MyCommand = "VOLT:UNIT VPP"

MyInstrument.write(MyCommand)

time.sleep(0.1)

MyCommand = "APPL:SIN 20000,2.5,0.5"

MyInstrument.write(MyCommand)

time.sleep(0.1)

MyCommand = "OUTP ON"

MyInstrument.write(MyCommand)

else:

print "Didn't find RIGOL TECHNOLOGIES,DG1022"

This next example we will use the Rigol DS1102E oscilloscope. As above, Rigol has many instruments that follow the National Instruments VISA interface. Here is the link to Rigol’s VISA Runtime forApple OS X PDF:

http://beyondmeasure.rigoltech.com/acton/attachment/1579/f-00cd/0/-/-/-/-/file.pdf

The DS1102E uses a USB interface. To find the USB ID of this instrument need to run this program:


from pyvisa.vpp43 import visa_library

visa_library.load_library("/Library/Frameworks/Visa.framework/VISA")

import visa

print visa.get_instruments_list()


With the DS1102E I used, its USB ID is:

USB0::0x1AB1::0x0588::DS1EB132503279

Remember after sending the DS1102E commands, it is in remote mode (it is not crashed). To return to front panel control, hit the FORCE button in the trigger section.


Here is a short little script to suck the last displayed trace from the DS1102E:


from pyvisa.vpp43 import visa_library

visa_library.load_library("/Library/Frameworks/Visa.framework/VISA")

import visa

import time

print visa.get_instruments_list()

RigolDS1102E = "USB0::0x1AB1::0x0588::DS1EB132503279"

if RigolDS1102E in visa.get_instruments_list():

MyInstrument = visa.instrument("USB0::0x1AB1::0x0588::DS1EB132503279::INSTR", timeout = 12)

MyCommand = "*IDN?"

MyInstrument.write(MyCommand)

time.sleep(0.1)

print MyInstrument.read()

time.sleep(0.1)

MyCommand = ":WAV:DATA? CHAN1"

MyInstrument.write(MyCommand)

time.sleep(1.5)

WaveformData = MyInstrument.read()

print WaveformData.encode("hex")

else:

print "Didn't find RIGOL TECHNOLOGIES,DS1102E"


I leave it to you to interpret the binary data returned form this script.


There is a page that might help you with that

Grabbing data from a Rigol ‘scope with Python

http://hackaday.com/2012/03/30/grabbing-data-from-a-rigol-scope-with-python/

Additional Information

PyVISA on OSX

http://bardagjy.com/?p=1245

https://www.python.org/downloads/mac-osx/

https://pyvisa.readthedocs.io/en/stable/


Learning Python:

https://trinket.io/

http://blog.tim-smith.us/2015/09/python-extension-modules-os-x/


Using the neoVIM editor with Python

http://ricostacruz.com/til/neovim-with-python-on-osx


Controlling a Rigol DP800 power supply

https://github.com/paperwork/pySourceremotecontrol/blob/master/sources_dp800.py


Controlling a Rigol oscilloscope using Linux and Python:

http://www.cibomahto.com/2010/04/controlling-a-rigol-oscilloscope-using-linux-and-python/


IDLE and tkinter with Tcl/Tk on macOS

https://www.python.org/download/mac/tcltk/


There are some Python apps for the iPhone, see:

Pythonista

Python 2.7 for iOS

Pythoni3.3

Python Tutorial

National Instruments low cost USB data acquisition  products also have a VISA interface with OSX drivers. The software drivers support many of their products. The link is:

http://joule.ni.com/nidu/cds/view/p/id/428/lang/en