Skip to content

Instantly share code, notes, and snippets.

@ochococo
Last active June 27, 2024 13:34
Show Gist options
  • Save ochococo/8362414fff28fa593bc8f368ba94d46a to your computer and use it in GitHub Desktop.
Save ochococo/8362414fff28fa593bc8f368ba94d46a to your computer and use it in GitHub Desktop.
National Instruments GPIB-USB-HS via PyVISA on UBUNTU.md

National Instruments GPIB-USB-HS + PYVISA on Ubuntu

LINUX-GPIB

Install depedencies:

sudo apt-get install tk-dev build-essential texinfo texi2html libcwidget-dev libncurses5-dev libx11-dev binutils-dev bison flex libusb-1.0-0 libusb-dev libmpfr-dev libexpat1-dev tofrodos subversion autoconf automake libtool mercurial

Checkout the LINUX-GPIB to user home dir:

cd ~
svn checkout svn://svn.code.sf.net/p/linux-gpib/code/trunk linux-gpib-code

Install the kernel part:

cd ~/linux-gpib-code/linux-gpib-kernel
make
sudo make install

Instal the user part:

cd ~/linux-gpib-code/linux-gpib-user
sudo make install

Check if USB dongle is connected:

lsusb | grep GPIB

expected result:

Bus 001 Device 006: ID 3923:709b National Instruments Corp. GPIB-USB-HS

Load kernel module:

sudo modprobe ni_usb_gpib

Update cache for linker

sudo ldconfig

Check if kernel module loaded:

lsmod | grep gpib

expected result:

ni_usb_gpib            36864  0
gpib_common            45056  1 ni_usb_gpib

Create config file

interface {
        minor = 0
        board_type = "ni_usb_b"
        pad = 0
        master = yes
}

File location for Ubuntu:

/usr/local/etc/gpib.conf

Test GPIB configuration:

sudo gpib_config

expected result:

no output, no error

Try to communicate with the device:

sudo ibtest
  • d ENTER,
  • address of your instrument ex. 3
  • w ENTER.
  • type *IDN? ENTER,
  • r ENTER
  • 100 ENTER

Expected result:

Example: KEITHLEY INSTRUMENTS INC.,MODEL 2000,0000,A05 /A02

If that works it means that linux-gpib can talk to the adapter and instrument. Success.

Python3

Install Python3 depedencies

sudo apt-get install python3-dev python3-distutils python3-pip python3-setuptools

Install Python bindings

cd linux-gpib-code/linux-gpib-user/
cd language/python/
sudo python3 setup.py install

Test script

~/testgpib.py

import Gpib
# X is your interface number (usually 0)
# Y is your instrument address (should be configured on the device)
inst = Gpib.Gpib(X,Y) 
inst.write("*IDN?")
print(inst.read(100))
python3 ~/testgpib.py

expected result:

Example: KEITHLEY INSTRUMENTS INC.,MODEL 2000,0000,A05 /A02

PyVISA

Install pyvisa

pip3 install pyvisa

Install pyvisa-py backend

pip3 install pyvisa-py

Install gpib-ctypes

pip3 install gpib-ctypes

Verify configuration

python3 -m visa info

Expected result:

(...)
Backends:
   ni:
      (not important)
   py:
      Version: X.X.X
      GPIB INSTR: Available via Linux GPIB (b'X.X.X r[XXXX]')
      GPIB INTFC: Available via Linux GPIB (b'X.X.X r[XXXXX]')

Test script

~/testvisa.py

import pyvisa
resources = pyvisa.ResourceManager('@py')
# X means your instrument address
k2000 = resources.open_resource('GPIB::X::INSTR')
print(k2000.query('*IDN?'))
python3 ~/testvisa.py

expected result:

Example: KEITHLEY INSTRUMENTS INC.,MODEL 2000,0000,A05 /A02

Sources

While compiling this instruction I used different sources including:

@jhamlin-ufl
Copy link

jhamlin-ufl commented Jun 27, 2024

I solved the issue below. sudo strace gpib_config showed that the config file is at /etc/gpib.conf. Moving the file to this location fixed the issue for me.
I am trying to follow the procedure and have run into an issue. When I execute the command: sudo gpib_config
I get the following error:

failed to configure boardtype: ni_pci
failed to configure board
main: Invalid argument

The device is showing up as expected: Bus 001 Device 003: ID 3923:709b National Instruments Corp. GPIB-USB-HS
I'm running Ubuntu 20.04.4 LTS
I assume this is related to the config file, but I'm not sure what's wrong. Any suggestions?

interface {
        minor = 0
        board_type = "ni_usb_b"
        pad = 0
        master = yes
}

Hi. I'm running into the same issue.

Have you managed to solve it?

Regards

This worked for me on Debian Bookworm. Instead of /usr/local/etc/gpib.conf, I had to move the file to /etc/gpib.conf. Then when I run sudo gpib_config I get back no errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment