Skip to content

Instantly share code, notes, and snippets.

@pklaus
Last active April 15, 2019 09:24
Show Gist options
  • Save pklaus/cdc73246d6ebce9ccbc37a3fa4d8d08f to your computer and use it in GitHub Desktop.
Save pklaus/cdc73246d6ebce9ccbc37a3fa4d8d08f to your computer and use it in GitHub Desktop.
Test
Direct Modbus TCP Examples for Python/Pymodbus and LabJack Devices
support@labjack.com
Provided are simple examples demonstrating direct Modbus TCP usage with a
LabJack in Python using the pymodbus library.
Documentation for pymodbus can be found at:
http://pythonhosted.org/pymodbus/
For easy pymodbus installation, use your Python installation's pip or
easy_install package manager.
Alternatively, use the downloads from here for installation (pymodbus requires
pyserial):
https://pypi.python.org/pypi/pyserial
https://pypi.python.org/pypi/pymodbus
LabJack example code is licensed under MIT X11. The license can be found in the
LICENSE.TXT file.
'''
Helpful functions for converting register data to numbers and vice versa.
'''
import struct
def float2int(num):
return struct.unpack("=i", struct.pack("=f", num))[0]
def concatData(data):
tVal = 0
upper = True
for reg in data:
if upper:
tVal = ((reg & 0xFFFF) << 16)
upper = False
else:
tVal = tVal | (reg & 0xFFFF)
upper = True
return tVal
'''
Converting numbers to 16-bit data arrays
'''
def uint16_to_data(num):
return struct.unpack("=H", struct.pack("=H", num & 0xFFFF))[0]
def uint32_to_data(num):
data = [0, 0]
data[0] = struct.unpack("=H", struct.pack("=H", (num>>16)&0xffff))[0]
data[1] = struct.unpack("=H", struct.pack("=H", num&0xffff))[0]
return data
def int32_to_data(num):
data = [0, 0]
data[0] = struct.unpack("=H", struct.pack("=H", (num >> 16) & 0xffff))[0]
data[1] = struct.unpack("=H", struct.pack("=H", num & 0xffff))[0]
return data
def float32_to_data(num):
intNum = float2int(num)
data = [0, 0]
data[0] = (intNum >> 16) & 0xFFFF
data[1] = intNum & 0xFFFF
return data
'''
Converting data arrays to numbers
'''
def data_to_uint16(data):
return data[0]
def data_to_uint32(data):
return concatData(data)
def data_to_int32(data):
return struct.unpack("=i", struct.pack("=I", concatData(data)))[0]
def data_to_float32(data):
return struct.unpack("=f", struct.pack("=I", concatData(data)))[0]
Copyright (C) 2017 LabJack Corporation <support@labjack.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'''
This example reads AIN0 on a T7 at IP address 192.168.1.15 using pymodbus.
'''
# Import files
from convert_data import *
from pymodbus.client.sync import ModbusTcpClient
# Open TCP port
client = ModbusTcpClient("192.168.1.15")
# Read AIN0
rr = client.read_input_registers(0, 2)
data_to_float32(rr.registers)
print("AIN0 = %.4f V" % data_to_float32(rr.registers))
# Close TCP port
client.close()
'''
This example reads various test registers from a T7 using the pymodbus library.
'''
# Import files
from convert_data import *
from pymodbus.client.sync import ModbusTcpClient
# Open TCP port
client = ModbusTcpClient("192.168.1.15")
# Test writing registers
rq = client.write_register(55110, uint16_to_data(1234))
rq = client.write_registers(55120, uint32_to_data(1234567))
rq = client.write_registers(55122, int32_to_data(-1234))
rq = client.write_registers(55124, float32_to_data(1.234))
# Test reading registers
rr = client.read_input_registers(55100, 2)
print("LJ Test Register (UINT32) %s" % data_to_uint32(rr.registers))
rr = client.read_input_registers(55110, 1)
print("Test UINT16 %s" % data_to_uint16(rr.registers))
rr = client.read_input_registers(55120, 2)
print("Test UINT32 %s" % data_to_uint32(rr.registers))
rr = client.read_input_registers(55122, 2)
print("Test INT32 %s" % data_to_int32(rr.registers))
rr = client.read_input_registers(55124, 2)
print("Test FLOAT32 %s" % data_to_float32(rr.registers))
# Close TCP port
client.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment