Created
April 28, 2017 19:01
-
-
Save muttiopenbts/27ec73f16980ecda4b7586813167c05d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Test script for interacting with device over modbus-rtc. | |
Need to improve received strings to output correctly after decoding. | |
''' | |
import pymodbus | |
import serial | |
from pymodbus.constants import Endian | |
from pymodbus.pdu import ModbusRequest | |
from pymodbus.client.sync import ModbusSerialClient as ModbusClient #initialize a serial RTU client instance | |
from pymodbus.transaction import ModbusRtuFramer | |
import logging | |
from pymodbus.payload import BinaryPayloadDecoder | |
from pymodbus.payload import BinaryPayloadBuilder | |
import hexdump | |
logging.basicConfig() | |
log = logging.getLogger() | |
log.setLevel(logging.DEBUG) | |
#usb_device = '/dev/tty.wchusbserial1420' | |
usb_device = '/dev/tty.usbserial-AL00KDH2' | |
#count= the number of registers to read | |
#unit= the slave unit this request is targeting | |
#address= the starting address to read from | |
client= ModbusClient(method = "rtu", port=usb_device,stopbits = 1, bytesize = 8, parity = 'N', baudrate= 9600, timeout=3, reconnects=2) | |
#Connect to the serial modbus server | |
connection = client.connect() | |
print connection | |
reg = 101 | |
num_of_reg = 8 | |
#Starting add, num of reg to read, slave unit. | |
result = client.read_input_registers(reg, num_of_reg, unit=0x18) # address, count, slave address | |
if result and hasattr(result, 'registers'): | |
decoder = BinaryPayloadDecoder.fromRegisters(result.registers, endian=Endian.Little) | |
decoded = { | |
'raw': result.registers, | |
'string8': decoder.decode_string(8), | |
'string16': decoder.decode_string(16), | |
'string32': decoder.decode_string(32), | |
# 'float': decoder.decode_32bit_float(), | |
# '16uint': decoder.decode_16bit_uint(), | |
# '8int': decoder.decode_8bit_int(), | |
# 'bits': decoder.decode_bits(), | |
} | |
print "-" * 60 | |
print "Decoded Data" | |
print "-" * 60 | |
for name, value in decoded.iteritems(): | |
print ("%s\t" % name), value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment