Skip to content

Instantly share code, notes, and snippets.

@ochococo
Last active March 17, 2025 12:54
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.

@tnixa
Copy link

tnixa commented Jul 25, 2024

I have two Agilent 34970A data loggers connected to my Ubuntu 22.04 laptop. Each one has a GPIB-USB-HS. I have configured the device address for one of the instruments to 8 and the other to 9. I can see both with the lsusb command...

pwrtest@pwrtest-ThinkPad-P71:~/linux-gpib-code/linux-gpib-kernel$ lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 003: ID 138a:0097 Validity Sensors, Inc. 
Bus 001 Device 002: ID 04ca:7066 Lite-On Technology Corp. Integrated Camera
Bus 001 Device 015: ID 3923:709b National Instruments Corp. GPIB-USB-HS
Bus 001 Device 014: ID 3923:709b National Instruments Corp. GPIB-USB-HS
Bus 001 Device 005: ID 8087:0a2b Intel Corp. Bluetooth wireless interface
Bus 001 Device 004: ID 0765:5010 X-Rite, Inc. X-Rite Pantone Color Sensor
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

But only one device is showing up in pyvisa...

rm.list_resources()
('ASRL/dev/ttyS4::INSTR', 'GPIB0::9::INSTR')

If I unplug 9 and plug in 8 then 8 will show up. So I believe both are working but when both are plugged in only one shows up. Any help with getting this working would be greatly appreciated!

@tnixa
Copy link

tnixa commented Jul 26, 2024

I found this issue below about multiple GPIB controllers. I was able to get both my my devices to show up by linking them together with a GPIB cable and then using a single GPIB-USB-HS
pyvisa/pyvisa-py#57

@lmsb
Copy link

lmsb commented Sep 24, 2024

Hi, I'm getting this error:
sudo modprobe ni_usb_gpib
modprobe: ERROR: could not insert 'ni_usb_gpib': Key was rejected by service

I searched and find its related with the driver not having a signature and related with Secure Boot enabled.
How can I ensure the driver is signed?

I followed the instructions below but it still didn't work. My setup is Ubuntu 22.04.4 LTS.

sudo tee x509.genkey > /dev/null << 'EOF'

[ req ]
default_bits = 4096
distinguished_name = req_distinguished_name
prompt = no
string_mask = utf8only
x509_extensions = myexts
[ req_distinguished_name ]
CN = Modules
[ myexts ]
basicConstraints=critical,CA:FALSE
keyUsage=digitalSignature
subjectKeyIdentifier=hash
authorityKeyIdentifier=keyid
EOF

@montanaviking
Copy link

Thanks!
I was wondering if this could be used to utilize the GPIB-USB-HS+ with C and/or C++? I think one would use the VISA libraries for C?
Thanks!
Phil

@montanaviking
Copy link

Hi,
I cannot get the installation to work if fails at step:
$ sudo gpib_config
[sudo] password for viking:
failed to configure boardtype: ni_pci
failed to configure board
main: Invalid argument
###############
running with strace gives:
#######################################################

$ sudo strace gpib_config
execve("/usr/local/sbin/gpib_config", ["gpib_config"], 0x7ffe1e164e30 /* 16 vars /) = 0
brk(NULL) = 0x625ba0c0f000
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x797080eaf000
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=84899, ...}) = 0
mmap(NULL, 84899, PROT_READ, MAP_PRIVATE, 3, 0) = 0x797080e9a000
close(3) = 0
openat(AT_FDCWD, "/usr/local/lib/libgpib.so.0", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\0\0\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=476688, ...}) = 0
mmap(NULL, 365016, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x797080e40000
mmap(0x797080e43000, 57344, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x3000) = 0x797080e43000
mmap(0x797080e51000, 28672, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x11000) = 0x797080e51000
mmap(0x797080e58000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x17000) = 0x797080e58000
mmap(0x797080e5a000, 258520, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x797080e5a000
close(3) = 0
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\220\243\2\0\0\0\0\0"..., 832) = 832
pread64(3, "\6\0\0\0\4\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0"..., 784, 64) = 784
fstat(3, {st_mode=S_IFREG|0755, st_size=2125328, ...}) = 0
pread64(3, "\6\0\0\0\4\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0"..., 784, 64) = 784
mmap(NULL, 2170256, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x797080c00000
mmap(0x797080c28000, 1605632, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x28000) = 0x797080c28000
mmap(0x797080db0000, 323584, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1b0000) = 0x797080db0000
mmap(0x797080dff000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1fe000) = 0x797080dff000
mmap(0x797080e05000, 52624, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x797080e05000
close(3) = 0
mmap(NULL, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x797080e3d000
arch_prctl(ARCH_SET_FS, 0x797080e3d740) = 0
set_tid_address(0x797080e3da10) = 79543
set_robust_list(0x797080e3da20, 24) = 0
rseq(0x797080e3e060, 0x20, 0, 0x53053053) = 0
mprotect(0x797080dff000, 16384, PROT_READ) = 0
mprotect(0x797080e58000, 4096, PROT_READ) = 0
mprotect(0x625b7ec1c000, 4096, PROT_READ) = 0
mprotect(0x797080ee7000, 8192, PROT_READ) = 0
prlimit64(0, RLIMIT_STACK, NULL, {rlim_cur=8192
1024, rlim_max=RLIM64_INFINITY}) = 0
munmap(0x797080e9a000, 84899) = 0
getrandom("\xb2\xec\xa8\xa7\xc2\x25\xc8\xcc", 8, GRND_NONBLOCK) = 8
brk(NULL) = 0x625ba0c0f000
brk(0x625ba0c30000) = 0x625ba0c30000
openat(AT_FDCWD, "/etc./gpib.conf", O_RDONLY) = 3
ioctl(3, TCGETS, 0x7fffbdbff220) = -1 ENOTTY (Inappropriate ioctl for device)
fstat(3, {st_mode=S_IFREG|0644, st_size=2838, ...}) = 0
read(3, "/*******************************"..., 8192) = 2838
read(3, "", 4096) = 0
read(3, "", 8192) = 0
ioctl(3, TCGETS, 0x7fffbdbfdd20) = -1 ENOTTY (Inappropriate ioctl for device)
close(3) = 0
openat(AT_FDCWD, "/dev/gpib0", O_RDWR) = 3
ioctl(3, _IOC(_IOC_WRITE, 0xa0, 0x27, 0x10), 0x7fffbdbff3c0) = 0
ioctl(3, _IOC(_IOC_WRITE, 0xa0, 0x18, 0x64), 0x7fffbdbff440) = -1 EINVAL (Invalid argument)
write(2, "failed to configure boardtype: n"..., 38failed to configure boardtype: ni_pci
) = 38
write(2, "failed to configure board\n", 26failed to configure board
) = 26
write(2, "main: Invalid argument\n", 23main: Invalid argument
) = 23
exit_group(-1) = ?
+++ exited with 255 +++
#################################################################

OS is Ubuntu 24.04
kernel is: 6.8.0-55-generic #57-Ubuntu SMP PREEMPT_DYNAMIC Wed Feb 12 23:42:21 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux

gpib.conf file is at /etc/gpib.conf AND /usr/local/etc/gpib.conf

cat /etc/gpib.conf
interface {
minor = 0
board_type = "ni_usb_b"
pad = 0
master = yes
}
$ cat /usr/local/etc/gpib.conf

cat /usr/local/etc/gpib.conf

interface {
minor = 0
board_type = "ni_usb_b"
pad = 0
master = yes
}
$ lsusb
c$ lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 002: ID 3923:709b National Instruments Corp. GPIB-USB-HS
Bus 001 Device 003: ID 067b:2303 Prolific Technology, Inc. PL2303 Serial Port / Mobile Phone Data Cable
Bus 001 Device 004: ID 1058:2621 Western Digital Technologies, Inc. Elements 2621
Bus 001 Device 005: ID 3923:709b National Instruments Corp. GPIB-USB-HS
Bus 001 Device 006: ID 046d:0994 Logitech, Inc. QuickCam Orbit/Sphere AF
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 002 Device 005: ID 0547:139b Anchor Chips, Inc. USB3.0 Camera
#######################
$ $ lsmod | grep gpib
ni_usb_gpib 45056 0
gpib_common 57344 1 ni_usb_gpib
################################

Any ideas why the gpib_conf command won't run?
Thanks!
Phil

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