Using Python for Instrument Control

Instrument control via any programming language requires commands to be sent out to the instrument from the VISA layer.

pyVISA is one such software package that enables Python communication with VISA layer and hence with the instrument, making the job of the programmer relatively more easy.

Here are the installation steps required before we talk about programming:

  1. VISA libraries (Chances are that you might already have these installed)

There are many variants of VISA available from different vendors. One criteria about which one to install could be the make of your instrument.

  1. Python (https://www.python.org/download/)

Make sure you download one of the versions supported with pyVISA. At the time of writing, version 2.6 and 2.7 were tested. Though personally I tried out version 3.2.3 and was able to make it work. It looks like there are some syntactical differences between 2.x and 3.x versions so as long as you know what you’re doing, you should be okay.

python-install

  1. pyVISA (https://github.com/hgrecco/pyvisa)

Download and install the pyVISA package

PyVISA

 

Now open Python GUI and type the required commands. Alternatively, you can use any Text Editor like Notepad++ and type your script into it. Make sure you save your file with the *.py extension

Here is a very simple example program/script to establish communication with the instrument (think of it as a “Hello World” for instrument control, where we ask the instrument “Tell me about you” and the instrument responds with details such as its manufacturer, model number, firmware revision etc.)

For pyVISA versions <1.5

# import visa
# instr = visa.instrument("TCPIP0::192.168.1.20::inst0::INSTR")
# print(instr.ask("*IDN?"))

For newer pyVISA versions

# import visa
# rm = visa.ResourceManager()
# my_instrument = rm.open_resource(TCPIP0::192.168.1.5::inst0::INSTR')
# print(my_instrument.query('*IDN?'))

CMDLine2

If you’re able to see a response to your *IDN? query, you have your system set up for Python scripting

To run a set of Python commands as a script, create a text file with .py extension and add your commands to it. Then you can run the script from command line by calling Python.exe and passing the .py file path as an argument

NPPCode

CMDline_py

Note: There is a considerable difference in syntax and commands between pyVISA versions as well as Python versions. Please go through the official pyVISA documentation to know more about this:

http://pyvisa.readthedocs.org/en/master/

http://pyvisa.readthedocs.org/en/master/migrating.html

5 comments:

  1. I like the valuable information you provide in your articles. I’ll bookmark your weblog and check again here regularly. I’m quite certain I will learn lots of new stuff right here! Best of luck for the next!

  2. Excellent weblog here! Additionally your web
    site a lot up very fast! What web host are you the usage of?
    Can I get your associate link on your host? I desire my site loaded up as quickly as
    yours lol

  3. Woah! I’m really digging the template/theme of this
    site. It’s simple, yet effective. A lot of times it’s tough to
    get that “perfect balance” between user friendliness and
    appearance. I must say that you’ve done a great job with this.

    Additionally, the blog loads super quick for me on Opera.
    Exceptional Blog!

Leave a Reply

Your email address will not be published. Required fields are marked *