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.
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:
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 updateio.py reader script:
elif interface['type'] in ['U6', 'U3', 'U9', 'U12']: utility.log(pilib.dirs.logs.io, 'Interface type is LabJack of type: ' + interface['type'], 3, pilib.loglevels.io) valueentries = processlabjackinterface(interface, previnputs) if valueentries: control_db.queue_queries(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:
|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.
And, when updateio is run, inputs entries with appropriate values are automagically created.