Modbus Quickstart
This quickstart uses a U3-HV, but the principles and most of the code apply to all LabJack devices.
Opening the device
Here’s how to open a U3:
>>> import u3
>>> d = u3.U3()
The constructor for the U3 class will try to automatically open the first found U3. This is good if you only have one U3 connected.
Basic I/O: DAC0, AIN0, FIO4, FIO6
Wire a jumper from DAC0 to AIN0, connect an LED from FIO4 to VS and jumper FIO6 to GND. Wire the anode (longer-lead) of the LED to VS and the cathode to FIO4. For more information on controlling LEDs, refer to the LED app note . The LED connection we are using is configuration 2 in the app note.
Set DAC0 to 1.5 V, and read the voltage on AIN0:
>>> DAC0_REGISTER = 5000
>>> d.writeRegister(DAC0_REGISTER, 1.5) # Set DAC0 to 1.5 V
1.5
>>> AIN0_REGISTER = 0
>>> d.readRegister(AIN0_REGISTER) # Read from AIN0
1.5252180099487305
In this example, we’re using the LabJack’s Modbus interface, which is register-based. The values of DAC0_REGISTER and AIN0_REGISTER are taken from the UD Modbus map. Find the table at the bottom of the UD Modbus page in the "UD Modbus Map" section. What register is DAC1 located at? Check the map, and confirm that it’s at 5002.
Now toggle the LED by setting FIO4 to output low and output high:
>>> FIO4_STATE_REGISTER = 6004
>>> d.writeRegister(FIO4_STATE_REGISTER, 0) # Set FIO4 low, LED on
0
>>> d.writeRegister(FIO4_STATE_REGISTER, 1) # Set FIO4 high, LED off
1
The value of FIO4_STATE_REGISTER is also taken from the UD Modbus map. Verify that the LED toggles on and off as FIO4’s state changes from low to high.
Now use FIO6 for digital input. First, we configure it for input, then we read its state:
>>> FIO6_DIR_REGISTER = 6106
>>> FIO6_STATE_REGISTER = 6006
>>> d.writeRegister(FIO6_DIR_REGISTER, 0) # Set FIO4 to digital input
0
>>> d.readRegister(FIO6_STATE_REGISTER) # Read the state of FIO6
0
>>> # Disconnect GND or connect FIO6 to VS
>>> d.readRegister(FIO6_STATE_REGISTER)
1
>>> # Reconnect GND
>>> d.readRegister(FIO6_STATE_REGISTER)
0
With FIO6 connected to GND, reading digital input on FIO6 reads low (0). When GND is disconnected, the FIO’s internal pull-up resistor causes its state to be high (1). With FIO6 connected to VS, reading the digital input on FIO6 reads high (1).
Going further with Modbus
In this quickstart, we’ve used the LabJack’s Modbus interface to use DAC0, AIN0, FIO4 and FIO6. For more examples of using Modbus, consult the workingWithModbus.py distributed with LabJackPython. The UD Modbus map is also essential for involved Modbus work.
Beyond Modbus: Low-level commands
The Low-level Commands Quickstart.