Labjack with Python - Reading all AINS is slow | LabJack
 

Labjack with Python - Reading all AINS is slow

4 posts / 0 new
Last post
PancakeMSTR
PancakeMSTR's picture
Labjack with Python - Reading all AINS is slow

Hi, I'm running a labjack UE9 with python and I'm trying to read all 14 AINS at the same time and it's really slow. The following is more or less what I'm doing: 

while runwhile == 1: 

AINS[0] = AINS[0] + [d.readRegister(0)]

AINS[1] = AINS[1] + [d.readRegister(1)]

AINS[2] = AINS[2] + [d.readRegister(2)]

AINS[3] = AINS[3] + [d.readRegister(3)]

AINS[4] = AINS[4] + [d.readRegister(4)]

AINS[5] = AINS[5] + [d.readRegister(5)]

AINS[6] = AINS[6] + [d.readRegister(6)]

AINS[7] = AINS[7] + [d.readRegister(7)]

AINS[8] = AINS[8] + [d.readRegister(8)]

AINS[9] = AINS[9] + [d.readRegister(9)]

AINS[10] = AINS[10] + [d.readRegister(10)]

AINS[11] = AINS[11] + [d.readRegister(11)]

AINS[12] = AINS[12] + [d.readRegister(12)]

AINS[13] = AINS[13] + [d.readRegister(13)]

if while_ind == 50: 

runwhile = 0 

while_ind += 1

This approach takes way too much time I feel like. This is my first time using the labjack with python, so I really don't know what I'm doing. Can anyone help me with this? It would be greatly appreciated. Thanks. 

LabJack Support
LabJack Support's picture
Your code is slower since you

Your code is slower since you are performing 14 USB command-response communications instead of grouping multiple commands for less USB communications. Also, analog input readings are 2 registers, so correct addresses when using readRegister are 0 (AIN0), 2 (AIN1), 4 (AIN2), 6 (AIN3), etc.. Since addresses are consecutive, you can use readRegister to read multiple analog inputs for better efficiency:

# Starting register address = 0 (AIN0), read 28 registers (14 AINs)
ainsRead = d.readRegister(addr=0, numReg=28)
for i in range(14):
    AINS[i] += ainsRead[i]

Command-response times are documented here:

https://labjack.com/support/datasheets/ue9/operation/command-response

Note that Modbus adds some additional overhead since binary to float conversions are performed on the UE9, and with Feedback the computer performs the float conversions.

PancakeMSTR
PancakeMSTR's picture
I'm having a little trouble

I'm having a little trouble finding, I guess, the documentation I'd like for labjack with python. Is there a list somewhere of Labjack python commands? 

Also can you explain in a little more detail what the line "ainsRead = d.readRegister(addr=0, numReg=28)" is doing? 

Thanks for the help. 

LabJack Support
LabJack Support's picture
You can run this command for

You can run this command for ue9 module documentation which details the UE9 class and its methods:

import ue9
help(ue9)

You can also look in the source:

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

Note that UE9 is a child of the Device class in LabJackPython.py, so the UE9 class has its methods as well. readRegister is documented there or use "help(ue9.UE9.readRegister)".

readRegister uses the Modbus protocol where sensors/functionality are mapped to addresses:

https://labjack.com/support/software/api/modbus/ud-modbus

"ainsRead = d.readRegister(addr=0, numReg=28)" is reading AIN0-AIN13, whose voltages are returned in a list. addr=0 indicates to use starting address 0 which maps to AIN0, and numReg=28 indicates to read 28 registers which is 14 analog inputs. readRegister will convert the register bytes to the float values.

To get more Modbus technical, 1 register is 2-bytes of data. Analog inputs are a 32-bit float value (single) and are 2 registers. They start at address 0, and following analog input channels are offset by 2 since the data type requires 2 registers. So AIN0 = address 0, AIN1 = 2, AIN2 = 4, AIN3 = 6. etc.. For a different example, DIO State reads a single digital I/O state where starting address 6000 is FIO0. It is a 1-register value (1 = high, 0 = low), so consecutive addresses are offset by 1. So FIO1 is 6001, FIO2 is 6002, etc.. The Modbus map is provided in the link above. It's a table with starting addresses and number of registers (Min Regs) to write/read for different functionality.

Also, there is a LabJackPython Modbus quickstart here:

https://labjack.com/support/software/examples/ud/labjackpython/modbus