Skip to content

Instantly share code, notes, and snippets.

@ochococo
Last active April 26, 2024 12:46
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • 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:

@MicroAlign
Copy link

I'm getting the following errors when trying to install the kernel part.

sudo make install
make -C /lib/modules/`uname -r`/build V=0 modules_install\
	M="/home/antonio/linux-gpib-code/linux-gpib-kernel/drivers/gpib" \
	GPIB_TOP_DIR=/home/antonio/linux-gpib-code/linux-gpib-kernel \
	INSTALL_MOD_DIR=gpib
make[1]: Entering directory '/usr/src/linux-headers-5.4.0-96-generic'
  INSTALL /home/antonio/linux-gpib-code/linux-gpib-kernel/drivers/gpib/agilent_82350b/agilent_82350b.ko
At main.c:160:
- SSL error:02001002:system library:fopen:No such file or directory: ../crypto/bio/bss_file.c:72
- SSL error:2006D080:BIO routines:BIO_new_file:no such file: ../crypto/bio/bss_file.c:79
sign-file: certs/signing_key.pem: No such file or directory
  INSTALL /home/antonio/linux-gpib-code/linux-gpib-kernel/drivers/gpib/agilent_82357a/agilent_82357a.ko
At main.c:160:
- SSL error:02001002:system library:fopen:No such file or directory: ../crypto/bio/bss_file.c:72
- SSL error:2006D080:BIO routines:BIO_new_file:no such file: ../crypto/bio/bss_file.c:79
sign-file: certs/signing_key.pem: No such file or directory
  INSTALL /home/antonio/linux-gpib-code/linux-gpib-kernel/drivers/gpib/cb7210/cb7210.ko
At main.c:160:
- SSL error:02001002:system library:fopen:No such file or directory: ../crypto/bio/bss_file.c:72
- SSL error:2006D080:BIO routines:BIO_new_file:no such file: ../crypto/bio/bss_file.c:79
sign-file: certs/signing_key.pem: No such file or directory
  INSTALL /home/antonio/linux-gpib-code/linux-gpib-kernel/drivers/gpib/cec/cec_gpib.ko
At main.c:160:
- SSL error:02001002:system library:fopen:No such file or directory: ../crypto/bio/bss_file.c:72
- SSL error:2006D080:BIO routines:BIO_new_file:no such file: ../crypto/bio/bss_file.c:79
sign-file: certs/signing_key.pem: No such file or directory
  INSTALL /home/antonio/linux-gpib-code/linux-gpib-kernel/drivers/gpib/gpio/gpib_bitbang.ko
At main.c:160:
- SSL error:02001002:system library:fopen:No such file or directory: ../crypto/bio/bss_file.c:72
- SSL error:2006D080:BIO routines:BIO_new_file:no such file: ../crypto/bio/bss_file.c:79
sign-file: certs/signing_key.pem: No such file or directory
  INSTALL /home/antonio/linux-gpib-code/linux-gpib-kernel/drivers/gpib/hp_82335/hp82335.ko
At main.c:160:
- SSL error:02001002:system library:fopen:No such file or directory: ../crypto/bio/bss_file.c:72
- SSL error:2006D080:BIO routines:BIO_new_file:no such file: ../crypto/bio/bss_file.c:79
sign-file: certs/signing_key.pem: No such file or directory
  INSTALL /home/antonio/linux-gpib-code/linux-gpib-kernel/drivers/gpib/hp_82341/hp_82341.ko
At main.c:160:
- SSL error:02001002:system library:fopen:No such file or directory: ../crypto/bio/bss_file.c:72
- SSL error:2006D080:BIO routines:BIO_new_file:no such file: ../crypto/bio/bss_file.c:79
sign-file: certs/signing_key.pem: No such file or directory
  INSTALL /home/antonio/linux-gpib-code/linux-gpib-kernel/drivers/gpib/ines/ines_gpib.ko
At main.c:160:
- SSL error:02001002:system library:fopen:No such file or directory: ../crypto/bio/bss_file.c:72
- SSL error:2006D080:BIO routines:BIO_new_file:no such file: ../crypto/bio/bss_file.c:79
sign-file: certs/signing_key.pem: No such file or directory
  INSTALL /home/antonio/linux-gpib-code/linux-gpib-kernel/drivers/gpib/lpvo_usb_gpib/lpvo_usb_gpib.ko
At main.c:160:
- SSL error:02001002:system library:fopen:No such file or directory: ../crypto/bio/bss_file.c:72
- SSL error:2006D080:BIO routines:BIO_new_file:no such file: ../crypto/bio/bss_file.c:79
sign-file: certs/signing_key.pem: No such file or directory
  INSTALL /home/antonio/linux-gpib-code/linux-gpib-kernel/drivers/gpib/nec7210/nec7210.ko
At main.c:160:
- SSL error:02001002:system library:fopen:No such file or directory: ../crypto/bio/bss_file.c:72
- SSL error:2006D080:BIO routines:BIO_new_file:no such file: ../crypto/bio/bss_file.c:79
sign-file: certs/signing_key.pem: No such file or directory
  INSTALL /home/antonio/linux-gpib-code/linux-gpib-kernel/drivers/gpib/ni_usb/ni_usb_gpib.ko
At main.c:160:
- SSL error:02001002:system library:fopen:No such file or directory: ../crypto/bio/bss_file.c:72
- SSL error:2006D080:BIO routines:BIO_new_file:no such file: ../crypto/bio/bss_file.c:79
sign-file: certs/signing_key.pem: No such file or directory
  INSTALL /home/antonio/linux-gpib-code/linux-gpib-kernel/drivers/gpib/sys/gpib_common.ko
At main.c:160:
- SSL error:02001002:system library:fopen:No such file or directory: ../crypto/bio/bss_file.c:72
- SSL error:2006D080:BIO routines:BIO_new_file:no such file: ../crypto/bio/bss_file.c:79
sign-file: certs/signing_key.pem: No such file or directory
  INSTALL /home/antonio/linux-gpib-code/linux-gpib-kernel/drivers/gpib/tms9914/tms9914.ko
At main.c:160:
- SSL error:02001002:system library:fopen:No such file or directory: ../crypto/bio/bss_file.c:72
- SSL error:2006D080:BIO routines:BIO_new_file:no such file: ../crypto/bio/bss_file.c:79
sign-file: certs/signing_key.pem: No such file or directory
  INSTALL /home/antonio/linux-gpib-code/linux-gpib-kernel/drivers/gpib/tnt4882/tnt4882.ko
At main.c:160:
- SSL error:02001002:system library:fopen:No such file or directory: ../crypto/bio/bss_file.c:72
- SSL error:2006D080:BIO routines:BIO_new_file:no such file: ../crypto/bio/bss_file.c:79
sign-file: certs/signing_key.pem: No such file or directory
  DEPMOD  5.4.0-96-generic
Warning: modules_install: missing 'System.map' file. Skipping depmod.
make[1]: Leaving directory '/usr/src/linux-headers-5.4.0-96-generic'
/sbin/depmod -A

I'm running Ubuntu 18.04 - kernel version 5.4.0-96-generic

@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

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