Author Topic: Python at the workbench - SCPI control of lab instruments  (Read 1095 times)

0 Members and 1 Guest are viewing this topic.

Online RoGeorge

  • Super Contributor
  • ***
  • Posts: 4137
  • Country: ro
Python at the workbench - SCPI control of lab instruments
« on: May 13, 2022, 03:50:24 pm »
Code: [Select]
pip install pyvisa-py
python3
>>> import pyvisa as visa
>>> dp832 = visa.ResourceManager('@py').open_resource('TCPIP::192.168.1.4::INSTR')
>>> print (dp832.query("*IDN?"))
RIGOL TECHNOLOGIES,DP832A,DP...,00.01.16

>>> print(dp832.query(':MEASure:ALL? CH3'))
3.1921,0.3004,0.959

>>> # press CTRL+D to exit from python3

Felt chatty today.  These are some sort of personal learning notes.  Trying to make a tale for beginners like me.

\[ \star \ \star \ \star \]
Say you have a power supply, or an oscilloscope, or some other instrument that can be controlled remotely, from a computer.  You see weird acronyms in the instrument's user guide, like LXI, VISA, IVI, VXI, SCPI, GPIB, etc.  Now what?

That means that the instrument can listen to text commands sent from a computer, and can respond to those commands, and send measurements back to the computer when asked.  There are many ways to talk SCPI with an instrument, can be through a USB cable, or RS232, or LAN, or WiFi, or GPIB.

\[ \star \ \star \ \star \]
Let's send some SCPI commands from Python.  You have to have:
- computer with Python3 installed
- a lab instrument that can talk SCPI
- a cable to connect the instrument to the PC

Right now I'm using a Rigol DP832A power supply connected with a LAN cable, but it can be any other SCPI capable instrument, no matter the brand, or if it's a power supply or an oscilloscope.  My OS is Ubuntu, it should work the same from Windows, or Mac, too, but I didn't try.

There is no need to install additional programs, or drivers, or NI-VISA, or IVI, or whatever other software you may have received together with the instrument.  However, there is a module that has to be added to Python, pyvisa.  Open a terminal (a command prompt) and type:

Code: [Select]
pip install pyvisa-pyThat's it, all installed now.

(Skipabble gibberish details:  'pyvisa' and 'pyvisa-py' are not the same thing.  'pyvisa' is the frontend, it can use different backend VISA libraries, either the proprietary NI-VISA or the open source backend 'pyvisa-py'.  The backend is the component knowing VISA, and how to talk over different LAN, USB or COM.  Installing 'pyvisa' alone will only work if a backend is already present.  Installing 'pyvisa-py' with pip will also install 'pyvisa')

\[ \star \ \star \ \star \]
Connect the LAN cable and give a fixed IP to the instrument.  Mine has the IP address 192.168.1.4, so whenever this address will appear in the next examples, replace it with the address of your instrument.  To be sure it's all working, open a terminal and type:
Code: [Select]
ping 192.168.1.4and the instrument should respond.  Just to be sure it's your instrument responding and not some other device, power off the instrument and the ping shouldn't work any more.  If all good, power the instrument on, and now let's try from Python.

\[ \star \ \star \ \star \]
Open a terminal and type
Code: [Select]
python3

# at the python prompt, >>>, type
>>> import pyvisa as visa
>>> dp832 = visa.ResourceManager('@py').open_resource('TCPIP::192.168.1.4::INSTR')

>>> # to send SCPI commands to the DP832 power supply and print the DP832's answer
>>> # for example, the ':*IDN?' command asks the instrument to identify itself

>>> print (dp832.query("*IDN?"))
RIGOL TECHNOLOGIES,DP832A,DP...,00.01.16

>>> # I have manually turned on CH3 with some power LEDs connected
>>> # now let's read the Voltage, Current and Power measured by the DP832 at its channel 3

>>> print(dp832.query(':MEASure:ALL? CH3'))
3.1721,0.3004,0.951

>>>
Those numbers read by Python are the same numbers that were displayed on the DP832's front panel at that moment.

\[ \star \ \star \ \star \]
Once we have those numbers, we can do whatever we want with them in Python.  For example we could save them in a file, or plot them in a chart, or combine them with some other data from some other instruments, or do automated testing, or conduct lab experiments in order to discover the meaning of everything!  ;D

Works similar for other SCPI instruments, or with USB instead of LAN.
« Last Edit: May 13, 2022, 03:59:30 pm by RoGeorge »
 
The following users thanked this post: paf, ruben2022

Offline Wolfgang

  • Super Contributor
  • ***
  • Posts: 1670
  • Country: de
  • Its great if it finally works !
    • Electronic Projects for Fun
Re: Python at the workbench - SCPI control of lab instruments
« Reply #1 on: May 14, 2022, 07:15:08 pm »
Hi George,

I agree that SCPI is the way to go to control a multi-instrument, multi-vendor lab environment, and using python and VISA makes it simple enough. I prefer this over any LabView, BenchVue or other vendor integration layer. Its stable, its open, its flexible, there are no single vendor restrictions, and the math and engineering libraries of Python are a *lot* better than other stuff.

The best thing is that you can create your own SCPI-based instruments and you can control them via Python as well.

https://wordpress.com/stats/post/6495/electronicprojectsforfun.wordpress.com

Regards
  Wolfgang
 

Online RoGeorge

  • Super Contributor
  • ***
  • Posts: 4137
  • Country: ro
Re: Python at the workbench - SCPI control of lab instruments
« Reply #2 on: May 17, 2022, 11:59:33 am »
The link doesn't work for me (try your link from a private browser window), you mean this one right?
https://electronicprojectsforfun.wordpress.com/
That's a very nice blog!   :-+  I think I've met it already when googled for some RF projects.

Same here about NI-VISA, I prefer small and open source components, the NI-VISA thing is a huge install, and I remember it as Windows only.  The default backend for pyvisa might be the NI-VISA binaries, if installed, but I didn't check, so just to be sure it's using the pyvisa-py backend, I specify "py" when creating a new ResourceManager.

That reminds me of some setup steps required for USB instruments in Linux:



An example for controlling a Rigol DS1054z by USB from Python, using Ubuntu and pyvisa.
(Make sure the oscilloscope is not in pict bridge mode, set it to "computer", on the oscilloscope buttons press "Utility" -> "IO Setting" -> "USB Device" to mode "Computer").

Install the following
Code: [Select]
pip install pyvisa-py
pip install rlcompleter    #optional, it helps with TAB autocompletion in interactive mode
sudo apt install python3-usb

Configure the following
Code: [Select]
sudo groupadd usbtmcusers
sudo usermod -a -G usbtmcusers $USER

sudo su
echo 'SUBSYSTEM=="usb", MODE="0666", GROUP="usbtmcusers"' >> /etc/udev/rules.d/90-grant-USB-access-for-users-in-group-usbtmcusers.rules
exit
# reboot the computer

After rebooting, plug the oscilloscope by its US cable, and check if all OK
Code: [Select]
lsusb
# Bus 003 Device 013: ID 1ab1:04ce Rigol Technologies DS1000Z Series

getent group usbtmcusers
# usbtmcusers:x:1001:your_username

ls -la /dev/usbtmc0
# crw------- 1 root root 180, 0 May 17 14:22 /dev/usbtmc0

Test it from Python, in interactive mode:
Code: [Select]
        >>> import pyvisa as visa
        >>> import rlcompleter
        # double press TAB to reveal available functions from autocomplete
        # e.g. type "visa." at the ">>>" prompt then press TAB TAB
       
        >>> rm = visa.ResourceManager('@py')
        >>> print( rm.list_resources_info() )
           
        >>> print( rm.list_resources() )
            ('USB0::6833::1230::DS1Zyour_serial_number::0::INSTR',)
           
        >>> ds1054z = rm.open_resource('USB0::6833::1230::DS1Zyour_serial_number::0::INSTR')
        >>> print( ds1054z.query('*IDN?') )
            /home/muuu/.local/lib/python3.8/site-packages/pyvisa_py/protocols/usbtmc.py:115: UserWarning: Unexpected MsgID format. Consider updating the device's firmware. See [url]https://github.com/pyvisa/pyvisa-py/issues/20[/url]
            warnings.warn(
            RIGOL TECHNOLOGIES,DS1104Z,DS1Zyour_serial_number,00.04.05.SP2
        # first time might get this warn, it's about the string terminator
        #   warns only once, it doesn't complain at the following commands
        >>> print( ds1054z.query('*IDN?') )
            RIGOL TECHNOLOGIES,DS1104Z,DS1Zyour_serial_number,00.04.05.SP2
           
        # to find the oscilloscope's SCPI commands, see the rigol ds1000z programming guide site rigolna.com
        # [url]https://www.rigolna.com/products/digital-oscilloscopes/1000z/[/url]
        # [url]https://beyondmeasure.rigoltech.com/acton/attachment/1579/f-0386/1/-/-/-/-/DS1000Z_Programming%20Guide_EN.pdf[/url]
       
        # turn channel 2 on, set the hardware frequency counter to channel 2, then read its value
        # SCPI to send
        #   :CHANnel2:DISPlay ON
        #   :MEASure:COUNter:SOURce CHAN2
        #   :MEASure:COUNter:VALue?
        #
        >>> ds1054z.write(':CHANnel2:DISPlay ON')
        >>> ds1054z.write(':MEASure:COUNter:SOURce CHAN2')
        >>> ds1054z.query(':MEASure:COUNter:VALue?')
            '7.0725586e+02\n'
           
        >>> # CTRL+D to exit

I'm not sure what should I mention how to do it, and what not.  The intent is to explain everything as for a newcomer, those not very comfortable with programming, but this makes it all look like a complicated TL;DR.

The brief alternative for all the above would be:

\[ \star \ \star \ \star \]
Connect with a Rigol DS1054z by USB, Python, pyvisa in Ubuntu.
(first, from the oscilloscope, press "Utility" -> "IO Setting" -> "USB Device" to mode "Computer")

Required computer installs/configure and reboot:
Code: [Select]
pip install pyvisa-py
sudo apt install python3-usb

sudo groupadd usbtmcusers
sudo usermod -a -G usbtmcusers $USER

sudo su
echo 'SUBSYSTEM=="usb", MODE="0666", GROUP="usbtmcusers"' >> /etc/udev/rules.d/90-grant-USB-access-for-users-in-group-usbtmcusers.rules
reboot

Test from Python:
Code: [Select]
import pyvisa as visa

rm = visa.ResourceManager('@py')
print( rm.list_resources() )
#            ('USB0::6833::1230::DS1Zyour_serial_number::0::INSTR',)
ds1054z = rm.open_resource('USB0::6833::1230::DS1Zyour_serial_number::0::INSTR')
print( ds1054z.query('*IDN?') )

# set the HW counter to CH2 and read the measured frequency
ds1054z.write(':CHANnel2:DISPlay ON')
ds1054z.write(':MEASure:COUNter:SOURce CHAN2')
ds1054z.query(':MEASure:COUNter:VALue?')
#            '7.0725586e+02\n'


Would it be better to keep it short like that?
« Last Edit: May 17, 2022, 12:15:02 pm by RoGeorge »
 

Online RoGeorge

  • Super Contributor
  • ***
  • Posts: 4137
  • Country: ro
Re: Python at the workbench - SCPI control of lab instruments
« Reply #3 on: May 19, 2022, 02:54:31 pm »
Wanna GUI-Click!-Click with SCPI from Python?  :D

Download the DVD .iso of Kubuntu 20.04 LTS from https://kubuntu.org/getkubuntu/, and run it in live mode, without installing anything on the current computer.  Preferably would be to install VirtualBox on the current computer, and boot the DVD from inside the VirtualBox program, but this is not mandatory.

Should work the same for Linux, or Windows, or Mac, when booting the live DVD.  This will not mess with the existing computer, and it won't install or change anything permanently on the existing computer.

\[ \star \ \star \ \star \]
Once the Kubuntu live is running, open a terminal in Kubuntu live (with CTRL+ALT+T) and type (inside the running Kubuntu live in a Terminal - 100% tested and working):
Code: [Select]
sudo apt update
sudo apt install python3-pip idle

pip install pysimplegui pyvisa-py

Inside Kubuntu live, open IDLE, press CTRL+N to create a new Python3, write the following Python code to test PySimpleGUI, then press F5 to run it (give the file a name when asked to save):
Code: [Select]
import PySimpleGUI as sg

layout = [
    [sg.Button('OK'), sg.Button('Cancel')]
]

window = sg.Window('GUI App', layout)

while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED:
        break

window.close()
This should open a window with two buttons that can be clicked that does nothing (for now):



New skill unlocked, create a window from Python!  ;D

\[ \star \ \star \ \star \]
For more info about PySimpleGUI, visit the PySimpleGUI dot org http://pysimplegui.org pages, or for a step by step video, watch Creating 10 apps in Python with PySimpleGUI by Clear Code.

Close the OK/Cancel window, and to add some SCPI functionality, make the following chahttps://www.eevblog.com/forum/programming/exit/?action=dlattach;attach=1490818;imagenges to the python file (inside IDLE):
- add a textbox where one can type an SCPI command to be sent to the instrument
- add a read-only textbox to display the instrument's reply
- rename the previous buttons and change the window layout so they will become buttons for "Send" and "Exit".
- for now, the instrument connection will be hardcoded, I've used a Rigol DS1054Z oscilloscope connected by a network cable (mine has the IP address 192.168.1.4, you should use the IP address of your instrument).
Code: (Python) [Select]
import PySimpleGUI as sg
import pyvisa as visa

IP_address = '192.168.1.3'

rm = visa.ResourceManager()
ds1054z = rm.open_resource('TCPIP::' + IP_address + '::INSTR')

layout = [
    [sg.Text('SCPI command:'), sg.Input('*IDN?', key='-SCPI-'), sg.Button('Send')],
    [sg.Text('Response:'), sg.Input('SCPI answer', key='-ANSWER-', expand_x=True, readonly=True)],
    [sg.Button('Exit')]
]

window = sg.Window('Simple SCPI Sender', layout, finalize=True)
window['-SCPI-'].bind('<Return>', '_Enter')
window['-SCPI-'].bind('<KP_Return>', '_Enter')

while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED:
        break

    if event == 'Exit':
        break
    if event == 'Send' or event == '-SCPI-' + '_Enter':
        if values['-SCPI-'][-1] == '?':
            answer = ds1054z.query(values['-SCPI-'])
        else:
            answer = ds1054z.write(values['-SCPI-'])
        window['-ANSWER-'].update(answer)

ds1054z.close()
rm.close()
window.close()

Run it with F5, and a window should pop:


If you click 'Send' or press 'Enter' the command written in the textbox will be sent to the instrument (here the DS1054z oscilloscope) and the reply from the instrument will be displayed on the next row:


\[ \star \ \star \ \star \]
Type and send some other SCPI commands.  For example, to interrogate the oscilloscope what is its current timebase setting, send the SCPI command:
Code: [Select]
TIM:SCAL?


To set the horizontal timebase to 2ms/div, send the command:
Code: [Select]
TIMebase:MAIN:SCALe 0.002


and so on.   The response "28" in this case indicates the total length of the sent SCPI command (26 letters plus 2 the <CR><LF> line termination characters).

For this Rigol DS1000Z series oscilloscope, the command for the timebase has this format:
Quote
Syntax
:TIMebase[:MAIN]:SCALe <scale>
:TIMebase[:MAIN]:SCALe?
The parts written between square brackets are optional, same the small letters, so either the full length or the short format "TIM:SCAL" will be understood by the oscilloscope.
 
(lookup the programming user guide of your instrument to find supported commands, for the Rigol DS1054Z oscilloscope, the programming guide can be found at https://beyondmeasure.rigoltech.com/acton/attachment/1579/f-0386/1/-/-/-/-/DS1000Z_Programming%20Guide_EN.pdf )
« Last Edit: May 19, 2022, 03:27:33 pm by RoGeorge »
 

Offline dagema

  • Contributor
  • Posts: 39
  • Country: de
Re: Python at the workbench - SCPI control of lab instruments
« Reply #4 on: May 19, 2022, 09:44:39 pm »
Hi, has anyone been able to get it to work on Windows10? When I try it, I get the following errors:

C:\Users\donnie>pip install pyvisa-py
Collecting pyvisa-py
  Downloading PyVISA_py-0.5.3-py3-none-any.whl (59 kB)
     ---------------------------------------- 59.0/59.0 kB 774.3 kB/s eta 0:00:00
Collecting pyvisa>=1.12.0
  Using cached PyVISA-1.12.0-py3-none-any.whl (175 kB)
Collecting typing-extensions
  Using cached typing_extensions-4.2.0-py3-none-any.whl (24 kB)
Installing collected packages: typing-extensions, pyvisa, pyvisa-py
  WARNING: The scripts pyvisa-info.exe and pyvisa-shell.exe are installed in 'C:\Users\donnie\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\Scripts' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed pyvisa-1.12.0 pyvisa-py-0.5.3 typing-extensions-4.2.0

C:\Users\donnie>python3
Python 3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyvisa as visa
>>> EEZ = visa.ResourceManager('@py').open_resource('TCPIP::10.2.2.50::INSTR')
Traceback (most recent call last):
  File "C:\Users\donnie\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pyvisa_py\tcpip.py", line 74, in after_parsing
    self.interface = vxi11.CoreClient(
  File "C:\Users\donnie\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pyvisa_py\protocols\vxi11.py", line 202, in __init__
    super(CoreClient, self).__init__(
  File "C:\Users\donnie\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pyvisa_py\protocols\rpc.py", line 768, in __init__
    pmap = TCPPortMapperClient(host, open_timeout)
  File "C:\Users\donnie\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pyvisa_py\protocols\rpc.py", line 748, in __init__
    RawTCPClient.__init__(self, host, PMAP_PROG, PMAP_VERS, PMAP_PORT, open_timeout)
  File "C:\Users\donnie\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pyvisa_py\protocols\rpc.py", line 457, in __init__
    self.connect((open_timeout / 1000.0) + 1.0)
  File "C:\Users\donnie\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pyvisa_py\protocols\rpc.py", line 491, in connect
    raise RPCError("can't connect to server")
pyvisa_py.protocols.rpc.RPCError: can't connect to server

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\donnie\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pyvisa\highlevel.py", line 3284, in open_resource
    res.open(access_mode, open_timeout)
  File "C:\Users\donnie\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pyvisa\resources\resource.py", line 278, in open
    self.session, status = self._resource_manager.open_bare_resource(
  File "C:\Users\donnie\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pyvisa\highlevel.py", line 3209, in open_bare_resource
    return self.visalib.open(self.session, resource_name, access_mode, open_timeout)
  File "C:\Users\donnie\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pyvisa_py\highlevel.py", line 167, in open
    sess = cls(session, resource_name, parsed, open_timeout)
  File "C:\Users\donnie\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pyvisa_py\sessions.py", line 325, in __init__
    self.after_parsing()
  File "C:\Users\donnie\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pyvisa_py\tcpip.py", line 78, in after_parsing
    raise errors.VisaIOError(constants.VI_ERROR_RSRC_NFOUND)
pyvisa.errors.VisaIOError: VI_ERROR_RSRC_NFOUND (-1073807343): Insufficient location information or the requested device or resource is not present in the system.
>>>

A ping to the EEZ power supply at 10.2.2.50 responds, and the EEZ Studio can send and recieve SCPI commands to it without problems. 

I have never used Python before, so I have no idea what is causing the errors.

Any suggestions?

Thanks
 

Online RoGeorge

  • Super Contributor
  • ***
  • Posts: 4137
  • Country: ro
Re: Python at the workbench - SCPI control of lab instruments
« Reply #5 on: May 20, 2022, 01:18:22 am »
What instrument is the one you try to connect?
Look in its programing guide documentation and find if it is VISA or LXI compliant.

I don't have Windows.  On a clean Windows install it should work.  Might be some installation conflict, or the python paths were added at the end instead of being added first, or some other Windows settings are cutting the access, I don't know.

Type at a command prompt 'pyvisa-info', maybe you get some clue.  One of the lines should read "TCPIP INSTR: Available" and another line "TCPIP SOCKET: Available".

Could also be that the instrument responds to other resource string format.
https://pyvisa.readthedocs.io/en/1.8/names.html

The easiest way would be to install VirtualBox, download Kubuntu 20.04 LTS and create a virtual machine with Kubuntu (inside VirtualBox), and try from there (because it was tested and known as working).  Ubuntu is as easy to use as Windows, and you can have both Windows 10 and Kubuntu 20.04 running at the same time.  Kubuntu will be just like any other program running inside a window, it won't overwrite the existing Windows10 install.

Offline dagema

  • Contributor
  • Posts: 39
  • Country: de
Re: Python at the workbench - SCPI control of lab instruments
« Reply #6 on: May 23, 2022, 05:53:40 pm »
Thank you for your reply.  I am trying to connect to an EEZ H24005 dual channel power supply. I have attached a screenshot of the device in use with it's SCPI interface application EEZ Studio. I installed  Python3 on a fresh Ubuntu 20.04 installation, then PIP, then pyvisa:

donnie@UD5:~$ sudo apt install python3-pip
[sudo] Passwort für donnie:
Paketlisten werden gelesen... Fertig
Abhängigkeitsbaum wird aufgebaut.       
Statusinformationen werden eingelesen.... Fertig
Die folgenden zusätzlichen Pakete werden installiert:
  libpython3-dev libpython3.8-dev python-pip-whl python3-dev
  python3-setuptools python3-wheel python3.8-dev
Vorgeschlagene Pakete:
  python-setuptools-doc
Die folgenden NEUEN Pakete werden installiert:
  libpython3-dev libpython3.8-dev python-pip-whl python3-dev python3-pip
  python3-setuptools python3-wheel python3.8-dev
0 aktualisiert, 8 neu installiert, 0 zu entfernen und 4 nicht aktualisiert.
Es müssen 6.864 kB an Archiven heruntergeladen werden.
Nach dieser Operation werden 25,5 MB Plattenplatz zusätzlich benutzt.
Möchten Sie fortfahren? [J/n]
Holen:1 http://de.archive.ubuntu.com/ubuntu focal-updates/main amd64 libpython3.8-dev amd64 3.8.10-0ubuntu1~20.04.4 [3.952 kB]
Holen:2 http://de.archive.ubuntu.com/ubuntu focal/main amd64 libpython3-dev amd64 3.8.2-0ubuntu2 [7.236 B]
Holen:3 http://de.archive.ubuntu.com/ubuntu focal-updates/universe amd64 python-pip-whl all 20.0.2-5ubuntu1.6 [1.805 kB]
Holen:4 http://de.archive.ubuntu.com/ubuntu focal-updates/main amd64 python3.8-dev amd64 3.8.10-0ubuntu1~20.04.4 [514 kB]
Holen:5 http://de.archive.ubuntu.com/ubuntu focal/main amd64 python3-dev amd64 3.8.2-0ubuntu2 [1.212 B]
Holen:6 http://de.archive.ubuntu.com/ubuntu focal/main amd64 python3-setuptools all 45.2.0-1 [330 kB]
Holen:7 http://de.archive.ubuntu.com/ubuntu focal/universe amd64 python3-wheel all 0.34.2-1 [23,8 kB]
Holen:8 http://de.archive.ubuntu.com/ubuntu focal-updates/universe amd64 python3-pip all 20.0.2-5ubuntu1.6 [231 kB]
Es wurden 6.864 kB in 15 s geholt (473 kB/s).                                 
Vormals nicht ausgewähltes Paket libpython3.8-dev:amd64 wird gewählt.
(Lese Datenbank ... 230393 Dateien und Verzeichnisse sind derzeit installiert.)
Vorbereitung zum Entpacken von .../0-libpython3.8-dev_3.8.10-0ubuntu1~20.04.4_am
d64.deb ...
Entpacken von libpython3.8-dev:amd64 (3.8.10-0ubuntu1~20.04.4) ...
Vormals nicht ausgewähltes Paket libpython3-dev:amd64 wird gewählt.
Vorbereitung zum Entpacken von .../1-libpython3-dev_3.8.2-0ubuntu2_amd64.deb ...
Entpacken von libpython3-dev:amd64 (3.8.2-0ubuntu2) ...
Vormals nicht ausgewähltes Paket python-pip-whl wird gewählt.
Vorbereitung zum Entpacken von .../2-python-pip-whl_20.0.2-5ubuntu1.6_all.deb ..
.
Entpacken von python-pip-whl (20.0.2-5ubuntu1.6) ...
Vormals nicht ausgewähltes Paket python3.8-dev wird gewählt.
Vorbereitung zum Entpacken von .../3-python3.8-dev_3.8.10-0ubuntu1~20.04.4_amd64
.deb ...
Entpacken von python3.8-dev (3.8.10-0ubuntu1~20.04.4) ...
Vormals nicht ausgewähltes Paket python3-dev wird gewählt.
Vorbereitung zum Entpacken von .../4-python3-dev_3.8.2-0ubuntu2_amd64.deb ...
Entpacken von python3-dev (3.8.2-0ubuntu2) ...
Vormals nicht ausgewähltes Paket python3-setuptools wird gewählt.
Vorbereitung zum Entpacken von .../5-python3-setuptools_45.2.0-1_all.deb ...
Entpacken von python3-setuptools (45.2.0-1) ...
Vormals nicht ausgewähltes Paket python3-wheel wird gewählt.
Vorbereitung zum Entpacken von .../6-python3-wheel_0.34.2-1_all.deb ...
Entpacken von python3-wheel (0.34.2-1) ...
Vormals nicht ausgewähltes Paket python3-pip wird gewählt.
Vorbereitung zum Entpacken von .../7-python3-pip_20.0.2-5ubuntu1.6_all.deb ...
Entpacken von python3-pip (20.0.2-5ubuntu1.6) ...
python3-setuptools (45.2.0-1) wird eingerichtet ...
python3-wheel (0.34.2-1) wird eingerichtet ...
libpython3.8-dev:amd64 (3.8.10-0ubuntu1~20.04.4) wird eingerichtet ...
python3.8-dev (3.8.10-0ubuntu1~20.04.4) wird eingerichtet ...
python-pip-whl (20.0.2-5ubuntu1.6) wird eingerichtet ...
libpython3-dev:amd64 (3.8.2-0ubuntu2) wird eingerichtet ...
python3-dev (3.8.2-0ubuntu2) wird eingerichtet ...
python3-pip (20.0.2-5ubuntu1.6) wird eingerichtet ...
Trigger für man-db (2.9.1-1) werden verarbeitet ...
donnie@UD5:~$ pip install pyvisa-py
Collecting pyvisa-py
  Downloading PyVISA_py-0.5.3-py3-none-any.whl (59 kB)
     |████████████████████████████████| 59 kB 474 kB/s
Collecting typing-extensions
  Downloading typing_extensions-4.2.0-py3-none-any.whl (24 kB)
Collecting pyvisa>=1.12.0
  Downloading PyVISA-1.12.0-py3-none-any.whl (175 kB)
     |████████████████████████████████| 175 kB 759 kB/s
Installing collected packages: typing-extensions, pyvisa, pyvisa-py
  WARNING: The scripts pyvisa-info and pyvisa-shell are installed in '/home/donnie/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed pyvisa-1.12.0 pyvisa-py-0.5.3 typing-extensions-4.2.0
donnie@UD5:~$ python3
Python 3.8.10 (default, Mar 15 2022, 12:22:08)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyvisa as visa
>>> EEZ = visa.ResourceManager('@py').open_resource('TCPIP::10.2.2.50::5025::SOCKET')
>>> print (EEZ.query("*IDN?"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/donnie/.local/lib/python3.8/site-packages/pyvisa/resources/messagebased.py", line 648, in query
    return self.read()
  File "/home/donnie/.local/lib/python3.8/site-packages/pyvisa/resources/messagebased.py", line 486, in read
    message = self._read_raw().decode(enco)
  File "/home/donnie/.local/lib/python3.8/site-packages/pyvisa/resources/messagebased.py", line 442, in _read_raw
    chunk, status = self.visalib.read(self.session, size)
  File "/home/donnie/.local/lib/python3.8/site-packages/pyvisa_py/highlevel.py", line 519, in read
    return data, self.handle_return_value(session, status_code)
  File "/home/donnie/.local/lib/python3.8/site-packages/pyvisa/highlevel.py", line 251, in handle_return_value
    raise errors.VisaIOError(rv)
pyvisa.errors.VisaIOError: VI_ERROR_TMO (-1073807339): Timeout expired before operation completed.
>>> print (EEZ.query("*IDN?"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/donnie/.local/lib/python3.8/site-packages/pyvisa/resources/messagebased.py", line 648, in query
    return self.read()
  File "/home/donnie/.local/lib/python3.8/site-packages/pyvisa/resources/messagebased.py", line 486, in read
    message = self._read_raw().decode(enco)
  File "/home/donnie/.local/lib/python3.8/site-packages/pyvisa/resources/messagebased.py", line 442, in _read_raw
    chunk, status = self.visalib.read(self.session, size)
  File "/home/donnie/.local/lib/python3.8/site-packages/pyvisa_py/highlevel.py", line 519, in read
    return data, self.handle_return_value(session, status_code)
  File "/home/donnie/.local/lib/python3.8/site-packages/pyvisa/highlevel.py", line 251, in handle_return_value
    raise errors.VisaIOError(rv)
pyvisa.errors.VisaIOError: VI_ERROR_TMO (-1073807339): Timeout expired before operation completed.
>>>


From the EEZ Studio setup I surmised that I need add the socket port 5025 to the TCP/IP call to address the unit. Doing that, I seem to reach the unit with pyvisa, but the *IDN? command does not seem to get any response.

Your intro led me to believe this was a seemingly simple and generic method to talk SCPI, but it no longer appears to me to simple nor generic as I have not been able to get it to work on either Windows or Linux. I guess my expectations were too high.

Kind regards,
Donnie




 

Online RoGeorge

  • Super Contributor
  • ***
  • Posts: 4137
  • Country: ro
Re: Python at the workbench - SCPI control of lab instruments
« Reply #7 on: May 23, 2022, 06:28:09 pm »
It looks like the instrument can only use SOCKET to connect, and not INSTR.

That instrument might not be fully compatible with pyvisa.  (I assume you can ping the instrument from Ubuntu, right?)  Might be because the instrument is not ready yet for VISA (saying this because I've noticed it only accepts a single connection - my Rigol instruments can have multiple socket connections open at the same time  :-//), or it might be some other simple setting that has to be changed, as for example using a different string terminator, or increasing the timeout to give the instrument more time to reply, I don't know.

I don't have an EEZ instrument to debug the communication, but the EEZ power supplies happens to have a thread on this forum, open by one of the designers.  User prasimix might be able to tell what settings are needed for your EEZ instrument, so to make it talk with pyvisa.  You may want to ask him for help connecting to the instrument from python/pyvisa here:
https://www.eevblog.com/forum/testgear/eez-studio-for-accessing-your-(scpi)-instruments/

PS:
For the future, please copy/paste console text/debugging messages between the "code" tags, so the text will be contained.
« Last Edit: May 23, 2022, 06:55:34 pm by RoGeorge »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf