labjack / labjack-ljm-python Goto Github PK
View Code? Open in Web Editor NEWLJM library Python wrapper for LabJack T4, T7 and T8.
License: MIT License
LJM library Python wrapper for LabJack T4, T7 and T8.
License: MIT License
Are there any plans for creating wrapper classes for the LJM-supported models (T4 / T7) like the https://github.com/labjack/LabJackPython library?
The methods in the LJM Python library are rather low-level, e.g.
ljm.eWriteAddress(handle, address, dataType, value)
Using the example code for loading a lua script, some scripts fail with this message (file with 1804 bytes)
lua: [string "--[[..."]:70.000000: unexpected symbol near 'char(5.000000)'
where the line number is the last line of the program, and char() changes, sometimes its a printable ascii character.
The failure is inconsistent, as it seems to depend on what bad bytes follow the loaded program in python. In all of my tests, the scripts can randomly succeed, and all lua has been verified in the kipling debugger.
The problem can be fixed two ways:
script = file.read()
script += '\0'
MB.W(6000, 1, 0)
with MB.W(6000.00, 1, 0)
It's not clear what the root cause is, but it sure looks like a string length problem. This test was performed on Win64 machine.
The loading code is:
with open(self.filepath, mode="r", encoding="utf-8") as file:
script = file.read()
# script = script + '\0'
scriptLen = len(script)
ljm.eWriteName(self.handle, "LUA_SOURCE_SIZE", scriptLen)
ljm.eWriteNameByteArray(self.handle, "LUA_SOURCE_WRITE", scriptLen, script)
ljm.eWriteName(self.handle, "LUA_DEBUG_ENABLE", 1)
ljm.eWriteName(self.handle, "LUA_DEBUG_ENABLE_DEFAULT", 1)
print(f'Script {self.filepath} ({scriptLen} bytes) loaded')
My starting script (without any length manipulation) is
--[[
Cystal Monitor Test Fixture Lua for Labjack
DUT PS Power Regulator
--]]
MB.writeName("LUA_NO_WARN_TRUNCATION", 1)
-- print("Starting dut ps script")
local DATA_READY_REG = "USER_RAM0_U16" -- will be 1 when the data is ready
local MV_SETTING_REG = "USER_RAM1_U16"
------------------------
-- Common I2C Configuration and Misc Utilities
------------------------
local I2C_SCL_REG = 3-- FIO4
local I2C_SDA_REG = 4 -- FIO3
local I2C_SPEED = 65516 -- 0,65536 = 450Khz, 1=40Hz, 65516=100kHz
local I2C_OPTIONS = 3 -- resetAtStart*1+noStopAtStarting*2
local I2C_SLAVE_ADDRESS_REG = 5104 -- uint16
function config_i2c_bus(address)
-- print("Configuring i2c bus")
I2C.config(I2C_SDA_REG, I2C_SCL_REG, I2C_SPEED, I2C_OPTIONS, address)
end
function sleep_ms(inteval_ms)
local CheckInterval = LJ.CheckInterval
LJ.IntervalConfig(1, inteval_ms)
while not CheckInterval(1) do
end
end
----------------------------
-- DUT PS
----------------------------
MB.writeName(DATA_READY_REG, 0)
function dut_ps_set_voltage()
local DUT_PS_REGULATOR_I2C_ADDR = 0x0D
local DUT_PS_DEFAULT_MV = 4500 -- 750 to 4800
local DUT_MAX_MV = 4800 -- depends on jig
local DUT_MIN_MV = 750
local in_mv = MB.readName(MV_SETTING_REG)
-- print(string.format("setting dut_ps to %d mv", in_mv))
local data = (DUT_MAX_MV - in_mv)
if data > 4095 then data = 4095
elseif data < 0 then data = 0
end
b1 = bit.rshift(bit.band(data, 0xff00), 8)
b0 = bit.band(data, 0xff)
-- print(string.format("setting regulator to %02X %02X", b1, b0))
config_i2c_bus(DUT_PS_REGULATOR_I2C_ADDR)
I2C.write({b1, b0})
-- Common Ending Code
MB.writeName(DATA_READY_REG, 1)
end
dut_ps_set_voltage()
-- print("Stopping script")
MB.W(6000, 1, 0)
Is it possible for the labjack team to create an official repo here for the LJM library as well ? https://labjack.com/support/software/examples/ljm/python
Also publish to pypi. Would make it far easier to manage as dependency
It seems that labjack recently updated their website. Some of the "Relevant Documentation" link is now broken showing 404 when attempting to link to the webpage from the Python script.
For example:
Opening and Closing:
https://labjack.com/support/software/api/ljm/function-reference/opening-and-closing
this link does not work.
Now attempting to use this library with the Windows Subsytem for Linux (specifically Ubuntu 20.04 LTS). Everything installs fine, and I'm able to get access to the Labjack over USB via https://learn.microsoft.com/en-us/windows/wsl/connect-usb .
I'm trying to run this example program as a sanity check
Running it results in the following error. If WSL is unsupported or out of scope feel free to close this issue, but it would be nice to support if possible.
Beginning...
Error calling ljm.open(deviceType=0, connectionType=0, identifier=ANY)
Traceback (most recent call last):
File "/home/jmarkow/dev/labjack_test.py", line 116, in <module>
main()
File "/home/jmarkow/dev/labjack_test.py", line 95, in main
handle = openLJMDevice(ljm.constants.dtANY, ljm.constants.ctANY, "ANY")
File "/home/jmarkow/dev/labjack_test.py", line 52, in openLJMDevice
handle = ljm.open(deviceType, connectionType, identifier)
File "/home/jmarkow/miniconda3/envs/dearpygui-test/lib/python3.10/site-packages/labjack/ljm/ljm.py", line 382, in open
raise LJMError(error)
labjack.ljm.ljm.LJMError: LJM library error code 1230 LJME_DEVICE_CURRENTLY_CLAIMED_BY_ANOTHER_PROCESS
First, thanks so much for this library, developing for the Labjack is mostly a breeze!
We're running a program where we read in AIN0, AIN1 and FIO_STATE while using eReadStream. At the same time we're sending out TTL pulses via periodicStreamOut. To collect timestamps for our data reads we're also collecting CORE_TIMER – analog inputs are collected first, then FIO_STATE, then CORE_TIMER in the scan list. Even though the modbus map lists CORE_TIMER as a uint32, the returned values roll over at 2^16 rather than 2^32 as expected. Not sure if there's something in our configuration that would result in this happening.
by defining the same LJMError
class in the module where it is used, there exists a chicken/egg problem where the LJMError cannot be used to catch exception when importing LJM library.
For example:
try:
from labjack import ljm
except labjack.ljm.ljm.LJMError as exception:
raise LabjackLibraryNotInstalled from exception
cannot work because the LJMError is defined in the same module. A better practice is to seperate the exception class into it's own module that only depends on Python standard library, this ensures Labjack package exceptions can be used for module imports that wrap library code, just as labjack-ljm-python
wraps the LJM library that is required to be installed.
Another issue is that the _loadLibrary()
function catches the LJMError exception, prints the error, then returns None. Why? If the python package doesn't work without the LJM library, then it shouldn't handle the exception, it should just raise it and let the calling module handle the exception.
Please remove from _loadLibrary()
:
except LJMError:
ljme = sys.exc_info()[1]
print(str(type(ljme)) + ": " + str(ljme))
return None
Looks like the compiled libraries are not build against arm64. After pip installing I get the following error when importing ljm
<class 'labjack.ljm.ljm.LJMError'>: Cannot load the LJM library /usr/local/lib/libLabJackM.dylib. dlopen(libLabJackM.dylib, 0x0006): tried: '/Users/jeffreymarkowitz/miniconda3/envs/labjack-test/lib/python3.10/lib-dynload/../../libLabJackM.dylib' (no such file), '/Users/jeffreymarkowitz/miniconda3/envs/labjack-test/bin/../lib/libLabJackM.dylib' (no such file), 'libLabJackM.dylib' (no such file), '/usr/local/lib/libLabJackM.dylib' (mach-o file, but is an incompatible architecture (have (x86_64), need (arm64e))), '/usr/lib/libLabJackM.dylib' (no such file), '/Users/jeffreymarkowitz/libLabJackM.dylib' (no such file)
Are there plans to update for supporting M1 macs?
Hi I just purchased an T7 Pro. I need to use UART.
Is it possible to control UART TX and RX on T7 via python?
If not, what else can I do?
I ran the labjack installation script from the website and am now attempting to access the labjack via ljm in Python – using a T7-Pro over USB. All works well in Windows, but on Ubuntu I get the following error message when attempting to set up PeriodicStreamOut
AttributeError: /usr/local/lib/libLabJackM.so: undefined symbol: LJM_PeriodicStreamOut
The code is pretty simple, happy to post if that would help debug this.
When I first start my python IDE and run my code the voltage reading on my thermocouple is ~1.7 volts. If I close my program, start ljstream (without loading anything), wait for the voltage readings come in then close out ljstream I get ~ 8e-05 volts. The second value is the expected voltage as my thermal couple is close to room temp. Is there some initializing I need to do in my code? Here is the relevant code:
class labControl:
def __init__(self):
self.handle = ljm.openS("T7", "ANY","ANY")
info = ljm.getHandleInfo(self.handle)
print("\nOpened a LabJack with Device type: %i, Connection type: %i,\n"
"Serial number: %i, IP address: %s, Port: %i,\nMax bytes per MB: %i" %
(info[0], info[1], info[2], ljm.numberToIP(info[3]), info[4], info[5]))
def thermo(self,channel,cj,cal_data):
volts = ljm.eReadName(self.handle, channel)
# Sometimes this reading is way off and I dont know why. Opening and starting then closing lj-stream seems to clear it
print(volts)
mV = volts * float(1000)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.