CuPID + LabJack USB with Python API


Previously, we showed how to read a LabJack over Modbus TCP/IP, and showed a couple examples. This is super convenient for networked devices. Having a LAN handy (or the U9, the only LabJack with an ethernet port) isn’t always the situation, however, and in these cases, communicating over USB is the standard method with a LabJack. Further, the flexibility of the API allows us to abstract the functions of the LabJack much more easily with built-in functions. This is of course possible by wrapping up functions with a modbus map available, but here we don’t have to reinvent the wheel, as the functions are already wrapped for us. There are some clear drawbacks and shortcomings with the API (as will be discussed), but there are ways to deal with them.

The Setup

Here, we’ll be using a typical Raspberry Pi-based CuPID, with a LabJack U6 connected on USB. We are using a recently release of Raspbian as a part of our standard install. This particular system involves two devices connected to the LabJack, where the LabJack is put near the devices and some distance away from the CuPID. For this reason, we use USB over Cat5 adapters, which allow extension of the USB far beyond what is the typical spec of 5m. Here is what they look like:

RJ45 USB extenders
RJ45 USB extenders

For this particular device, we need to read an analog input with a max output of 10mV, and a counter.

The Python API

The LabJack python API is available here. The main disadvantage … and a huge disadvantage it is … is that you MUST maintain and close your sessions to the device religiously. There is no way to reattach, and no method to close_all(), as ¬†you would expect (AHEM!). So, don’t abuse your connection (read, handle errors in all called functions religiously), and close it. Otherwise you will be rebooting. This can be enormously painful if you are trying to do it all remotely.

Our little wrapper

To do what we need to do, we wrap up some primitive functions inside a couple small wrapper functions.

Integration in CuPID IO reader

To use this in a modular fashion, we include it as an interface type in our reader script:

elif interface['type'] in ['U6', 'U3', 'U9', 'U12']:
    utility.log(, 'Interface type is LabJack of type: ' + interface['type'], 3,
    valueentries = processlabjackinterface(interface, previnputs)

    if valueentries:

We call our processlabjackinterface function to process the interface entry. Any number of options can be entered into the interface options field and be processed, e.g. resolution, gain, etc.


For our case of U6 entries, we create the following interface entry:

interface type address id name
USB U6 first USB_U6_first U6 on USB

We then create entries in our LabJack table. Here, we are reading an analog input (with suitable options), and a counter. The addresses correspond to input addresses. See the processlabjackinterface function for more detail.

interfaceid address mode options
USB_U6_first 2 AIN differential:false,resolution:12,gain:3
USB_U6_first 0 CNT

And, when updateio is run, inputs entries with appropriate values are automagically created.

id interface type address name value unit polltime pollfreq
USB_U6_first_2_AIN USB U6 2 USB_U6_first_2_AIN 0.00601221327607 2017-01-24 12:37:03 60.0
USB_U6_first_0_CNT USB U6 0 USB_U6_first_0_CNT 8302454.0 2017-01-24 12:37:03 60.0


Leave a Reply

Your email address will not be published. Required fields are marked *