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:

@michael-antonelli
Copy link

@MicroAlign I had this issue as well, you have to generate the signing key with following commands:

cd /lib/modules/$(uname -r)/build/certs

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

sudo openssl req -new -nodes -utf8 -sha512 -days 36500 -batch -x509 -config x509.genkey -outform DER -out signing_key.x509 -keyout signing_key.pem

@ArrestedLightning
Copy link

FYI, The linux-gpib-user directory does not have a Makefile in it when you check the driver out from SVN. In order to run make on it, you first need to run ./bootstrap, followed by ./configure --sysconfdir=/etc. The argument to the config script is important; without it, things like udev rules won't be installed.

@ilockerbie
Copy link

ilockerbie commented Jun 23, 2022

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
}

@ctrimare
Copy link

Have you solved this issue?

@dforero1
Copy link

How do you create this file? like a .py file or?:
Create config file
interface {
minor = 0
board_type = "ni_usb_b"
pad = 0
master = yes
}

@mdpunch
Copy link

mdpunch commented Mar 17, 2023

Hi,

Thanks for putting this up, and the careful documention!

I am trying to get this working with an NI-USB-HS and currently xUbuntu 22.04.1 LTS (5.19.0-35-generic kernel).

After the step
cd ~/linux-gpib-code/linux-gpib-user; sudo make install
I get
make: *** No rule to make target 'install'. Stop.

So, I do
./bootstrap; ./configure
to create the make file.

Then then
sudo make install
works, and I see the device on the usb bus.

$ lsusb | grep GPIB
Bus 002 Device 004: ID 3923:709b National Instruments Corp. GPIB-USB-HS

I follow then next steps, and can see the kernel module okay:

$ lsmod | grep gpib
ni_usb_gpib            40960  0
gpib_common            53248  1 ni_usb_gpib

For the next steps in Python, I had to change the group of /dev/gpib0 from root to dialout (for which I had already added $USER to that group). This also means that I can execute ibtest without doing sudo, by the way.

Correction: I just had to create a group gpib, add the $USER to that group, and then the udev rules ensure persistence between reboots.

Warning: from habit, I was working in a conda/mamba environment, for which the Python step didn't work (ModuleNotFoundError: No module named 'gpib'), so I reverted to the non-conda environment.

Then, in Python, since I didn't know the address of my instrument, I could find it with this adapted version of your code example:

import Gpib
# X is your interface number (usually 0)
# Y is your instrument address (should be configured on the device)
for i in range(30):
    inst = Gpib.Gpib(0,i) 
    try:
        inst.write("*IDN?")
        print("address",i,"is",inst.read(100))
    except:
        pass

which gives
Address 8 is b'Stanford_Research_Systems,SR830,s/n83502,ver1.07 \n'

Hope this can help for other users who might hit the same problems (I started the post as a request for help, but got it working along the way),

//Michael Punch.

@sgodec
Copy link

sgodec commented Jul 10, 2023

Warning: modules_install: missing 'System.map' file. Skipping depmod.
make[1]: Leaving directory '/usr/src/linux-headers-5.19.0-46-generic'
/sbin/depmod -A
I still get this error, tried everything

@Stoyno
Copy link

Stoyno commented Aug 14, 2023

I am getting the following error when trying to communicate with a device :

InternalReceiveSetup: command failed
received string: ''
Number of bytes read: 0
gpib status is:
ibsta = 0x8100 < ERR CMPL >
iberr= 0
EDVR 0: OS error

Any ideas what is causing this behavior ?

@christofferkrook
Copy link

christofferkrook commented Sep 5, 2023

This wasn't trivial to install and get working correctly, however I am quite sure I managed it now.

There are installation instructions in the INSTALL files which supplement these quite nicely, however the step

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

should be
cd ~/linux-gpib-code/linux-gpib-user
./bootstrap
./configure --sysconfdir=/etc
sudo make
sudo make install

Otherwise it wont install and link all the stuff correctly. This procedure is described in the -user INSTALL file.

The device (ESP300 Motion stage controller) has now positively told me its name through ibtest() so that means i've a channel to work with now.

@RavingEnginerd
Copy link

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

@fqbe
Copy link

fqbe commented May 25, 2024

$ cd ~/linux-gpib-code/linux-gpib-kernel
$ make
make -C /lib/modules/uname -r/build V=0 modules
M="/home/integse/linux-gpib-code/linux-gpib-kernel/drivers/gpib"
GPIB_TOP_DIR=/home/integse/linux-gpib-code/linux-gpib-kernel
CONFIG_GPIB_ISA=""
GPIB_CONFIG_PCMCIA="0"
HAVE_DEV_OF_NODE=
GPIB_CONFIG_KERNEL_DEBUG=0
make[1]: Entering directory '/usr/src/linux-headers-6.5.0-35-generic'

ERROR: Kernel configuration is invalid.
include/generated/autoconf.h or include/config/auto.conf are missing.
Run 'make oldconfig && make prepare' on kernel src to fix it.

$ cd /usr/src/linux-headers-$(uname -r)
/usr/src/linux-headers-6.5.0-35-generic$ sudo make oldconfig
LEX scripts/kconfig/lexer.lex.c
YACC scripts/kconfig/parser.tab.[ch]
HOSTCC scripts/kconfig/lexer.lex.o
HOSTCC scripts/kconfig/menu.o
HOSTCC scripts/kconfig/parser.tab.o
HOSTCC scripts/kconfig/preprocess.o
HOSTCC scripts/kconfig/symbol.o
HOSTCC scripts/kconfig/util.o
HOSTLD scripts/kconfig/conf

configuration written to .config

$ sudo make prepare
SYNC include/config/auto.conf.cmd
make[2]: *** No rule to make target 'arch/x86/entry/syscalls/syscall_32.tbl', needed by 'arch/x86/include/generated/uapi/asm/unistd_32.h'. Stop.

@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