« Close

Datasheets and User Guides

App Notes

Software & Driver

 

Python for U12 - Windows, Mac, Linux

Our LabJackPython module works with the U12, and as of May 2010 it works on Windows, Linux, and Mac OS X.

Use it like this:

  
>>> import u12
>>> d = u12.U12()
>>> d.eAnalogIn(0)
{'overVoltage': 0, 'idnum': 12, 'voltage': 1.4599609375}
  

For more U12 examples visit the Low-Level Function Reference section of the U12 User’s Guide.

7 comments

How do I chek if the labjack is plugged or not!

I noticed there was a bug if no U12s are connected and you try to open one. Please get the latest version of LabJackPython from GitHub.

With the updated version, if your U12 is not connected, LabJackPython will raise an exception:


>>> import u12
>>> d = u12.U12()
open called
Traceback (most recent call last):
  File "", line 1, in 
  File "u12.py", line 408, in __init__
    self.open(id, serialNumber)
  File "u12.py", line 477, in open
    raise Exception("Couldn't open a U12. Check that one is connected and try again.")
Exception: Couldn't open a U12. Check that one is connected and try again.
>>>

Hi

Could someone please assist. I am a beginner in python programming and in using the Labjack U12 . I'm looking for a beginner's tutorial with detailed instructions on how to use the Labjacks U12 device to send and read data using voltages. I will be using a vizard API to program and  will be programming in Python.

 

I want to control a mobile platform by sending signals to move  or position it  and read the commands sent by the platform to direct movement in the  virtual world. The Labjack U12 is the middle man between the computer running the virtual application and PXI a data acquisition device which is connected to the mobile platform I want to control . I need to know how to send and receive data in the virtual world using python commands. How do I use the voltages , how do I program so the voltages send and receive data.

 

 

 

Python support for the U12 is provided in LabJackPython, so you will want to go to that page for installation instructions.  There is a quickstart on that page for the U3, but calls are not the same on the U12 beyond the similarity of importing the module, creating a class instance and using the methods provided by classes.  At the moment there is no Python tutorial for the U12, but there are more examples in the Low-Level Function Reference section of the User's Guide.  I updated this page to provide a link to that.

I will do a quick explanation of the example code on this page below with comments.

 

import U12 #Importing the U12 module

d = u12.U12() #Creating a new class instance of the U12 class named d.  The constructor opens the  U12.

d.eAnalogIn(0) #The newly created instance d provides methods to send/receive U12 data.  The eAnalogIn method reads an AI voltage.  In this case AI0.

 

It would be a good idea to do some Python language reading to better understand what is going on in our examples.  In particular learn about classes and methods, as our U12 Python interface uses it.  U12 class and method documentation can be found in the source code.  For reading one analog input, use the eAnalogIn U12 class method.  To read multiple analog inputs at once, use the aiSample (Windows only) or rawAISample (Linux/Mac only) method.  For setting analog outputs use the eAnalogOut method.

If you have further questions in regards to programming, the better location to post your questions is on our forum.

Hi there,

Am just beginning in Python, and am using a U12 to read in analog voltages using Voltage = d.eAnalogIn(0) but it only returns a string, I was expecting a voltage in a variable. Am I doing something wrong here?

Cheers

Dave

 

Python 3 seems to work....

I had to go through the following files:

  • LabJack.py
  • skymote.py
  • u12,py
  • u3.py
  • u6.py
  • ue9.py

And change print's and Exception's to encapsulate args in strings:

    print "blah blah blah %s" % aaa  --> print("blah blah blah %s" % aaa)

    except OSError, e:                      --> except(OSError, e):

    raise Exception "you messed up!" --> raise Exception("you messed up!")

I haven't done extensive testing yet, but it's all good so far.

Thanks for posting this information. Most changes as you mentioned are probably print and exception related, though there may be more to it. An alternative to editing the code by hand is the 2to3 converter script:

http://docs.python.org/2.7/library/2to3.html