LabJackPython for UD, Exodriver, U12 - Windows, Mac, Linux | LabJack
 
« Close

Datasheets and User Guides

App Notes

Software & Driver

 

LabJackPython for UD, Exodriver, U12 - Windows, Mac, Linux

LabJack Python Overview

LabJackPython is our cross-platform Python module for communicating with the LabJack U3, U6, UE9, and U12. It works on Windows with the UD Driver and U12 Driver, and on Linux and macOS with the Exodriver. For the LJM library (T7, T4, and Digit), use Python_LJM.

Examples

For LabJackPython examples, please see the Examples directory in the LabJackPython download, or see on GitHub: https://github.com/labjack/LabJackPython/tree/master/Examples

Download

The latest release of LabJackPython is 2.0.4 from September 9, 2020: Download the 2.0.4 release of LabJackPython.

For the latest development version, archived versions and code repository, visit LabJackPython on GitHub.

Requirements

  • Python 2.6, 2.7 and 3.x for the latest LabJackPython release.
  • Install the appropriate driver:
  • If using Modbus, make sure your LabJack meets the minimum firmware requirement mentioned in the Modbus Support table on the UD Modbus page.

Install

Unzip the LabJackPython.zip file and open a terminal/command prompt. In the terminal, use the command line to go to the unzipped LabJackPython directory (e.g., “cd Desktop/LabJackPython”). Then run one of the following commands to install the LabJackPython modules:

Windows

python setup.py install

Windows Troubleshooting

If running the above command causes a "'python' is not recognized as an internal or external command, operable program or batch file." or similar error, that means either Python is not installed on your computer or the Python executable's path was not added to the Windows user or system path. To help resolve this issue, please run the Python Windows installer and in the "Customize Python" window, click the "Add python.exe to Path" option towards the bottom and select "Will be installed on local hard drive".



Then finish the Python installation. Open a new terminal/console window and then go through our LabJackPython installation instructions again.

Alternatively, after Python is installed you can run the Python executable from its path. For example:

C:\Python27\python.exe setup.py install

Note that the above command uses the Python 2.7 path, and you should use the correct path on your computer which may differ.

 

Another option is to manually add the Python executable's path to the Windows path as described in the Python documentation.

Linux and macOS

$ sudo python setup.py install

Note the LabJackPython zip file and unzipped directory name will vary on the download.

Known Issues

macOS: As of macOS 10.15, opening a U6 the first time after plugging it in sometimes fails. Subsequent attempts to open succeed.

Documentation

For documentation, please refer to the docstrings in the source code or use the help function on the module, class, or method.

For the U3, refer to its u3.py source, or use the “help(u3)” call in Python (“import u3” beforehand).

For the U6, refer to its u6.py source, or use the “help(u6)” call in Python (“import u6” beforehand).

For the UE9, refer to its ue9.py source, or use the “help(ue9)” call in Python (“import ue9” beforehand).

For the U12, refer to its u12.py source, or use the “help(u12)” call in Python (“import u12” beforehand).

Quickstart

LabJackPython provides both a Modbus and low-level command interface for performing operations on a LabJack device. Quickstarts can be found for both in the Modbus Quickstart and Low-level Commands Quickstart sections.

 

File Attachment: 

91 comments

I noticed that u3.py contains the following comment:

Section Number Mapping:

1 = Object Functions

2 = User's Guide Functions

3 = Convenience Functions

4 = Private Helper Functions

A corresponding "section" variable is assigned after each U3 method.

What are these for?

 

Beyond organization reasons, I am not sure what the original intention of the "section" method attribute was as no other LabJackPython class does this or uses this attribute that I could find.

I'm trying to use Epydoc to generate html documentation for LabJackPython.

While this works in general, the output has some 'features' which I can't explain. They may be due to Epydoc bugs, but I thought I would ask here.

For example, the documentation for the u3.U3 class lists 39 methods. For some reason, they all have a link to a "Method Details" section except for 2: binaryToCalibratedAnalogTemperature & loadConfig.

What makes these 2 methods different from the others?

Codewise both are public methods like the others. binaryToCalibratedAnalogTemperature doesn't have documentation or an assigned section (would be section 3), but loadConfig does have documentation and a section.

Using the built in Python help both methods show up with the documentation (if available). My guess for binaryToCalibratedAnalogTemperature not showing up in Epydoc details is that it is not documented. I don't know why loadConfig is missing though.

What is the clean way to remove power from the labjacks, then apply power again and keep the python code running? Here is some psuedocode for example:

>>>import u6
>>>u6_1 = u6.U6(serial = 360008508)
>>>u6_2 = u6.U6(serial = 360009180)
>>>v1 = u6_1.getFeedback(u6.AIN24(PositiveChannel=12, ResolutionIndex=8, GainIndex = 0, Differential = False))    #get a voltage reading
>>>v2 = u6_2.getFeedback(u6.AIN24(PositiveChannel=12, ResolutionIndex=8, GainIndex = 0, Differential = False))
>>>
>>>GPIO.output("P9_12", GPIO.LOW)    #power to the hub cuts out here
>>>
>>>time.sleep(20)    #sleep for 20 secs
>>>
>>>GPIO.output("P9_12", GPIO.HIGH)    #power restored to hub, labjack led's light up
>>>
>>>time.sleep(5)    #buffer time to wait for hub and labjacks to be restored
>>>
>>>v1 = u6_1.getFeedback(...)    #get some more voltage readings
>>>v2 = u6_2.getFeedback(...)

I've tried u6_1.close() and u6_1.open() on each side of the sleep time, respectively, but I always get and error that says,

"LabJackPython.LabJackException: Could only write 0 of 12 bytes.
LIBUSB_ERROR_NO_DEVICE"

What am I doing wrong?

I appreciate your help.

Thank you.

If your U6 disconnects or loses power the current USB device handle becomes invalid. You should both close the U6 after losing power and then open your U6 again when it is enumerated. This cleans up the old, invalid handle in libusb and gets a new, valid handle. So after your "time.sleep(5)" when the U6s are enumerated (LED is on) and before your Feedback calls try:

u6_1.close()
u6_2.close()
u6_1 = u6.U6(serial = 360008508)
u6_2 = u6.U6(serial = 360009180)

If your U6 cannot be connected to, your u6.U6(...) call will return an exception.

That works perfectly. You guys are the best. Thanks.

Error in commented-out code in example:

 

In the PWM-looping.py example in LabJackPython-10-22-2012, there are commented-out chunks of

code that are supposed to work for U3 and U6.  One of them says

#d.configIO( NumberTimersEnabled = 1 )

 

I think that should be #d.configIO( NumberOfTimersEnabled = 1 ).

 

"d.configIO( NumberTimersEnabled = 1 )" is the correct call for the U6, and "d.configIO( NumberOfTimersEnabled = 1 )" should be the call for the U3. Looks like the low-level commands portion of the example were mainly for the U6. I updated the example on github with specific U3 and U6 low-level commands.

Are there any Python 3.x support plans for the U3?  If so, what is the time-frame for doing this?

Currently there are no plans for Python 3 support for LabJackPython. There are however LabJackPython forks on github for Python 3 (the LabJackPython github link is on this page), and you can try the 2to3 converter script (http://docs.python.org/2.7/library/2to3.html).

We do have Python 3 support for our Python interface to the new LJM library, but that is separate from LabJackPython and only supports the LabJack T7 at the moment.

Hi,

I am attempting to integrate a LJ U3 and LJ Tick-InAmp together to log the data from a thermocouple using a Python program. The U3 is also responsible for driving a relay that controls an actuator. as the relay and actuator are concerned, I have this functionality working, but am attempting to modify the extensive Python program I have in order to add the thermocouple functionality.

The relay is in FIO0 and FIO1, the LJTIA is connected to FIO2 and FIO3

First, I am curious if there is a way to call the function "TCVoltsToTemp.." in Python. I know this function can be used in LJLogUD so I hope it is accessible. 

Next, the data from the thermocouple must be extracted, so...

LJu3 = u3.U3()

v1 = LJu3.getfeedback(u3.AIN2()) #gets voltage from LJTIA OUTA

Temp = u3.TCVoltsToTemp[J:(v1 - 1.24)/201: 22) #converts v1 reading retrieved from AIN2 to Temp, Voffset = ~1.24 and G = 201x

Not sure if that's 100% so if someone could let me know if there's anything wrong there...

The TIA GND is connected via 10k resistor to InA+

 

When I try to install for python, I'm told:

byte-compiling C:\Python34\Lib\site-packages\u12.py to u12.cpython-34.pyc

  File "C:\Python34\Lib\site-packages\u12.py", line 369

    except OSError, e:

                  ^

Does this only work in Python 2?

Thanks.

LabJackPython currently only supports Python 2.5, 2.6 and 2.7 as mentioned in the Requirements section on this page.

Regarding Python 3, refer to this comment on this page:

http://labjack.com/support/labjackpython#comment-2378

In Python on Cygwin, I am able to call "import u6", but when I call "d = u6.U6()", I am getting a seg fault.  I ran a gdb backtrace, and it looked like there was an issue in the udlabjack dll file.  I'd appreciate any help on this.  We're excited to use this product.

I'm on Cygwin 1.7.34, 64 bit. I've tried on Windows 7 and Windows 8.1 64 bit. I'm using the latest version of the driver and I cloned the git repository last night.

If you install Python and LabJackPython on your Windows host (outside Cygwin) and run code using the Windows command prompt, does the seg. fault still occur?

Not on the windows 8 machine. We've been using it to collect data. I wanted to switch to Cygwin so I can use some Linux processing scripts and gnuplot for plotting data.

I will check on the windows 7 machine tomorrow.

I installed 32 bit cygwin on my 64 bit windows 8 machine and didn't get a seg fault.  I believe the issue is related to 64 bit cygwin.

I installed a 64-bit version of Cygwin and see the seg. fault now. We'll investigate the issue further and reply back to these comments when we have an update. In the meantime, it looks like to work around the issue use a 32-bit version of Cygwin.

I updated LabJackPython on github. Potentially the seg. faults could happen in 64-bit Python when using the UD driver (Windows only) in general, but this update/commit will resolve the issue. Let me know if you still encounter the issue after updating your LabJackPython in 64-bit Cygwin.

Hi,

Just got a U6 and have installed LabJack-02-02-2015.exe

I'm working on an up-to-date HP PC running Windows 7.

LJControlPanel works fine:

* Finds the USB U6, Test with jumper from DAC0 - AIN0 responds to changes on DAC0

LJLogUD works:

* I can log a signal from a function generator on AIN0

However, Kipling (3.0.6, LJM 1.0707, LJM Wrapper 0.0.13) fails to find any devices. After clicking Refresh Devices, I get "Sorry, no devices found..."

I've uninstalled everything, rebooted, and still no joy. Device Manager shows the U6 "working properly".

As Python control is my ultimate goal, I have installed the LabJackPython-4-24-2014.zip files, and successfully installed the python LJM library to my python (Anaconda 2.1.0) distro. Python can import the LJM library. However my copy of your test code snippet:

......testcomm.py: .................................

from labjack import ljm

# Open first found LabJack
handle = ljm.openS("ANY", "ANY", "ANY")

# Call eReadName to read the serial number from the LabJack.
name = "SERIAL_NUMBER"
result = ljm.eReadName(handle, name)

print("\neReadName result: ")
print("    %s = %f" % (name, result))

.......................................................

produces the following error in iPython:

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
In [1]: %run testcomm
---------------------------------------------------------------------------
LJMError                                  Traceback (most recent call last)
C:\Users\jhetrick\Documents\Python Scripts\testcomm.py in <module>()
      2
      3 # Open first found LabJack
----> 4 handle = ljm.openS("ANY", "ANY", "ANY")
      5
      6 # Call eReadName to read the serial number from the LabJack.

C:\Users\jhetrick\AppData\Local\Continuum\Anaconda\lib\site-packages\labjack\ljm
\ljm.pyc in openS(deviceType, connectionType, identifier)
    301     error = _staticLib.LJM_OpenS(deviceType.encode("ascii"), connectionT
ype.encode("ascii"), identifier.encode("ascii"), ctypes.byref(cHandle))
    302     if error != errorcodes.NOERROR:
--> 303        raise LJMError(error)
    304
    305     return cHandle.value

LJMError: LJM library error code 1298 LJME_ATTR_LOAD_COMM_FAILURE

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Any advice welcome!  Thanks.

 

 

Kipling is for the LJM library, and the LJM library is just for the T7 at this time.  LJControlPanel is for UD devices (U3/U6/UE9) on Windows, and should work with your U6 as you confirmed.  For Python you don't want Python_LJM, but rather want LabJackPython from this page, so follow the steps above on down to "Quickstart".

Works great! Thanks.

SteveG's picture

A few questions:
1. I'd like to use a Raspberry Pi and a U6 to collect about 8 analogue inputs at a rate of about 500Hz each, streaming continuously to disc (ie SD card) for several hours at a time. Is this at all feasible?

2. I've looked through examples such as  streamTest-threading.py in LabJackPython-5-26-2015 but these example don't write anything to disc

3. I could not find any Linux applications that do this, eg

Look forward to your comments.

LabJack Support's picture

1. Sounds feasible. The U6 can stream at a maximum sample rate of 50,000 samples per second. You want to stream at 4,000 samples per second (8 channels * 500 Hz).

https://labjack.com/support/datasheets/u6/operation/stream-mode

2 and 3. None of our examples demonstrate writing to a file, and we don't have any Linux applications for logging. Refer to the streaming examples for the LabJack side of operations. For writing to a file take a look at Python documentation:

https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-...

Basically you will be taking the samples from your U6 stream and writing them to a file.

heroux.martin's picture

Hi, I and using a U12 and running Python on a 64-bit Linux Mint system. I have installed the exodriver as well as ljacklm.zip. I can successfully run the examples provided with the exodriver as well as ljacklm. I am also able to connect to the device and use some of the 'easy' function in Python. However, I get errors when trying to use the burst or stream functions.
###########################################
# Session connecting to U12 and using 'easy' functions
###########################################
>>> import u12
>>> d = u12.U12(serialNumber=100076677)
>>> print d.id
-1
>>> print d.deviceName
U12>>> print d.eAnalogIn(0)
{'overVoltage': 0, 'idnum': -1, 'voltage': -0.1806640625}
>>> print d.eDigitalIn(0)
{'state': 0, 'idnum': -1}

###############################
# Here is what I get if I try to use aiBurst:
###############################
>>>  d.aiBurst(1,[0],400,10)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/u12.py", line 2185, in aiBurst
    ecode = staticLib.AIBurst(ctypes.byref(idNum), demo, stateIOin, updateIO, ledOn, numChannels, ctypes.byref(channelsArray), ctypes.byref(gainsArray), ctypes.byref(scanRate), disableCal, triggerIO, triggerState, numScans, timeout, ctypes.byref(voltages), ctypes.byref(stateIOout), ctypes.byref(overVoltage), transferMode)
  File "/usr/lib/python2.7/ctypes/__init__.py", line 375, in __getattr__
    func = self.__getitem__(name)
  File "/usr/lib/python2.7/ctypes/__init__.py", line 380, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self))
AttributeError: /usr/local/lib/liblabjackusb.so: undefined symbol: AIBurst

#############################
# Other attributes don't work
#############################
I tried running some of the 'get' commands, and these also return an AttribteErrorexample: >>> d.getFirmwareVersion(-1)


Any advice on what I might be doing wrong would be greatly appreciated.

Marty

LabJack Support's picture

AIBurst is a wrapper method for the Windows AIBurst call and is not implemented for Linux/Mac in LabJackPython. The "undefined symbol" indicates that a library call is unavailable and that the Python u12 method is not Linux/Mac compatible. Use the rawAIBurst function instead.

We mention the usage of the raw functions in the u12 module's doc, which is the comment at the top of u12.py:

https://github.com/labjack/LabJackPython/blob/master/src/u12.py

Regarding getFirmwareVersion, that is also a Windows only call. If you want to read the firmware version on Linux/Mac, call rawWatchdog which will return the firmware version. Do not configure the Watchdog settings and it will only read the firmware version. getFirmwareVersion is using the low-level Watchdog command-response to read the firmware version.

heroux.martin's picture

Thank you for the quick and informative response.

Much appreciated.

RoryCutler's picture

Hello, 

 

I'm currently delivering triggers via the FIO4 output of the U3-LV using the Python code below. However, we want to do exactly the same thing using the serial output port at the bottom of the U3-LV. Is this possible? If so, please could you let me know how to do this. 

from psychopy.hardware.labjacks import U3

d.getCalibrationData()

lj = U3()

if lj.getFIOState(0) == 1:
    lj.setFIOState(0,0) 

lj.setFIOState(4,1)

lj.setFIOState(4,0)

Thanks,

Rory

LabJack Support's picture

The DB15 contains 12 IO which can be used to generate pulses. All you will need to do is change the DIO# in your code.

DB15: https://labjack.com/support/datasheets/u3/hardware-description/db15

CB15: https://labjack.com/accessories/cb15-terminal-board

RoryCutler's picture

Thanks for your reply.

Ok, so if I were send a digital trigger via pin 6 of the DB15 (EIO4), would the following code be correct?

from psychopy.hardware.labjacks import U3 

d.getCalibrationData()

# start off with trigger off

if lj.getEIOState(0) == 1:
    lj.setEIOState(0,0)

lj.setEIOState(4,1) # trigger

lj.setEIOState(4,0) # trigger off

 

LabJack Support's picture

Working code would look like:

import u3

d = u3.U3()  # Open first found U3
d.getCalibrationData()

# These DIO line numbers correspond to these
# FIO, EIO and CIO lines.
#     0 - 7   = FIO0 - FIO7
#     8 - 15  = EIO0 - EIO7
#     16 - 19 = CIO0 - CIO3

# Trigger EIO4

# Start off with trigger off
d.setDOState(12, 0) # trigger off

d.setDOState(12, 1) # trigger

s.setDOState(12, 0) # trigger off

 

If using the psychopy's installation of our Python interface, use this import instead:

from labjack import u3

 

Keep in mind that the trigger code has no defined delay, so delay between calls (how long the trigger will stay on) is the USB overhead mainly. If you want the trigger to stay on for milliseconds look into adding a sleep call after the trigger is on. For better timing you can use the U3's hardware for timing, which requires use of the Feedback function with the BitStateWrite,  WaitShort and WaitLong IOTypes.

RoryCutler's picture

That's brilliant, thanks for letting me know. Is there a DIO line that you would recommend using if you wanted to minimize the delay between the LabJack trigger in Python and the receipt of the trigger in the external device?

LabJack Support's picture

Which DIO line to use will not matter in regards to delay, so no recommendation besides use your preference. The major delays are code and USB overhead. You can try the code above, which will be 3 command-responses (6 USB packets total transmitted) controlling the DIO, and see if it is acceptable in regards to delay. Otherwise you can do something like this for finer control which is 1 command-response with multiple commands to perform the triggering (trigger code), delay between DIO changes are U3 hardware controlled:

# When using WaitShort, the delay is time * 128 microseconds (us)
# When using WaitLong, the delay is time * 16 milliseconds (ms)

# Build the Feedback command.
# Set EIO4 to low, delay 128 us, set EIO4 to high, delay 128 us, and set EIO4 to low
command = [u3.BitStateWrite(12, 0), u3.WaitShort(1), u3.BitStateWrite(12, 1), u3.WaitShort(1), u3.BitStateWrite(12, 0)]

# Send the Feedback command
d.getFeedback(command)

 

RoryCutler's picture

Thank you! 

jjc's picture

Hi,

I must concede that I haven't been able to get an exodriver or labjackpython working for a number of recent macos versions. However, today I thought that I'd give it another shot and installed libusb 1.0.22 and exodriver. I was able to run the C examples for one of my 20 U3s, and another,  so that that was happy news. However, after installing LabJackPython 2.0 and trying just a simple import u3 I got the common error message:

"Could not load the Exodriver driver. Ethernet connectivity only.

Check that the Exodriver is installed, and the permissions are set correctly. 

The error message was: dlopen(liblabjackusb.dylib, 6): image not found"

I went through and checked permissions for the /usr/local/lib/, /usr/local/lib/liblabjackusb-2.6.0.dylib, and  /usr/local/lib/liblabjackusb.dylib. They all have read and execute for all users and write for admin. So that looks fine. any suggestions?

LabJack Support's picture

With the latest LabJackPython, the dlopen will have the full path to liblabjackusb.dylib (needed in newer macOS versions), which your dlopen error is missing. Check that the current LabJackPython version used is the latest:

import LabJackPython

print(LabJackPython.LABJACKPYTHON_VERSION)

The above should display 2.0.0. If it displays a date or something under 2.0.0, then an older version of LabJackPython is still being used. Double check that you downloaded and installed the latest 2.0.0 version of LabJackPython from our site or GitHub

If you did run our latest LabJackPython installer and Python is using an older version of LabJackPython, go to your Python "site-packages" folder and clean up LabJackPython related files. Delete LabJackPython egg and egg-info files, and the following files:

LabJackPython.py
Modbus.py
skymote.py
u12.py
u3.py
u6.py
ue9.py

Then run the LabJackPython 2.0.0 installer again. If you do not know where your Python "site-packages" folder is, you can check with:

import site

print(site.getsitepackages())

jjc's picture

As you suspected, an older version installed was getting loaded. Once I deleted all of the files except the newer egg everything started functioning fine. I'll have to go through the APIs a bit though because some old code,

lj.getFeedback([u3.BitDirRead(0),u3.c(0,1)])

gives an error...

Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'module' object has no attribute 'c'

 

Hopefully that shouldn't be too hard to fix.

clbpdxcom's picture

Instead of the CPython 3.7.4 reference implementation, I'd like to try out a scientific 3.x distribution of Python for Windows such as Anaconda or Winpython.  Scipy, Numpy, and Matplotlib are the most important for me.

Does anyone have any recommendations or advice about using either Anaconda or Winpython?

Thanks!

 

LabJack Support's picture

After installation, the latest version LabjackPython should run fine with Anaconda and WinPython with Python 3.x, and function the same as in CPython.

I have no general recommendations or advice in regards to different distributions of Python.

Pages