Using the National Instruments GPIB-USB-B on linux

This post is intended to help those that need to connect to a GPIB device, such as an oscilloscope, using theirs standard PC with an USB connection and an USB-to-GPIB device (such as the National Instruments GPIB-USB-B or GPIB-USB-HS). I assume you are running a recent linux distribution, such as Ubuntu 10.04 (although should work for previous versions as well).

The first thing to do is get the latest linux-gpib driver from here.

Then install the linux-gpib (see the INSTALL file) which is a simple "./configure" then "make" then "make install". Now the tricky part is that you actually need to do a few modifications to what is said there, since in 2008 some Linux distros used the hotplug system which is now obsolete. First of all, modify the /etc/gpib.conf file to match the gpib-usb-b device, having a line like this:

board_type = "ni_usb_b" /* type of interface board being used */

Alternatively, as I did, you can get a working gpib.conf file from here or here.

Now from either "your_linux_gpib_dir/usb/ni_usb_gpib/" or "/etc/hotplug/usb/" (where these files get installed by default), copy "ni_usb_gpib" to "/lib/udev" ("ni_usb_gpib.usermap" is not needed, since we'll be adding the necessary rules in the "/etc/udev/rules.d/" folder).

The next step is to download the ni_gpib_usb_b firmwire update from here and unzip the necessary contents (mainly ni_gpib_usb_b/) to "/lib/firmware/" (and NOT in "/usr/share/usb" as the README/INSTALL says - that's obsolete). Then rename the folder ni_gpib_usb_b to ni_usb_gpib.

Now, since you have changed the location of the firmwire you should change the "/lib/udev/ni_usb_gpib" script. Update the "DATADIR" variable as follows:


and then change all the occurrences of "$DATADIR/usb/" to "$DATADIR/".

Then modify also the last line of the ni_usb_gpib script (the one starting with $FXLOAD) with the following:


The diff of the whole script is available here and the new file here.

You will need to have fxload installed. In ubuntu/debian you can just do: "sudo apt-get install fxload".

Next, create a file in /etc/udev/rules.d/ like "99-linux_gpib_ni_usb.rules" with this content:

   SUBSYSTEM=="usb", ACTION=="add", ENV{DEVTYPE}=="usb_device", ATTR{idVendor}=="3923", ATTR{idProduct}=="702[ab]", MODE="660", GROUP="plugdev", SYMLINK+="usb_gpib"
   SUBSYSTEM=="usb", ACTION=="add", ENV{DEVTYPE}=="usb_device", ATTR{idVendor}=="3923", ATTR{idProduct}=="702[ab]", RUN+="/lib/udev/ni_usb_gpib"
   KERNEL=="gpib[0-9]*", ACTION=="add", MODE="660", GROUP="plugdev"

I've noticed that on Ubuntu 12.04 the ATTR{} doesn't work that well with certain devices. If you experience problems try to use ENV{ID_VENDOR..} for the above rule match. Check the output from "udevadm monitor --environment" to find which ID_... to match on.

where "plugdev" is simply a group to which your user (the one that will be using the device) is a member (so that you don't need root access). The idVendor and idProduct are selected based on my "lsusb" output for the GPIB-USB-B device. You should put there the IDs matching your device, taking in consideration that idProduct will change from 702b to 702a when the firmwire is updated.

For additional reading on the topic of udev and its rules I recommend this, and this. For debugging information I found this post useful (in particular the command "udevadm monitor --environment").

In order to use the linux-gpib driver you can either use the C API or the Python bindings. The Python bindings can be installed from the ubuntu/debian package "python-gpib". However on Ubuntu 12.04 this package is not available at the moment so the easiest thing to do is to get the linux-gpib source code (here) and install the Python bindings which are available under the languages/ folder. You must install the python-dev package first.

If your GPIB-USB-B device is getting stucked, which can happen when sending incorrect commands or queries, then running the following sequence of commands a couple times and waiting a few seconds in between might save you from unplugging/plugging your USB device:

   service udev reload
   udevadm trigger --type=devices
   udevadm settle --timeout=60

Another debugging hint: sometimes the gpib[0-15] entries in /dev/ get deleted (not really sure why, but I've noticed this from time to time, generally after some updates). This will cause the ni_gpib driver to fail. There are 2 solutions:

Take it from there to suit your needs.

back to other tutorials