Hi, I was looking at using pymodbus to interface to a slave temperature controller via a serial port using binary protocol. I am trying to read 1 register at address 100 (16 bit value). Does the ModbusSerialClient in sync mode work? I am struggling a little bit with the examples and how to do this and noticed a couple of things: - Is it possible to specify the COM port? It looks like it's hardcoded to COM1. - How do you specify the slave address? - If I want to read a single register, is the code below what I need to do? Thanks. BTW, this looks like a very handy library. Thanks for putting it out there...
I'll be glad to contribute, but it's been a couple years since I've written python...
-Mike --- #---------------------------------------------------------------------------# # import the various server implementations #---------------------------------------------------------------------------# from pymodbus.client.sync import ModbusSerialClient as ModbusClient client = ModbusClient(port=0,stopbits=1,bytesize=8,parity=N,baudrate=19200) # do I need to call .connect? # How to set the slave address? rr = client.read_input_registers(100,1)
import pymodbus
import serial
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
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)
#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="/dev/ttyUSB0",stopbits = 1, bytesize = 8, parity = 'E' baudrate= 9600)
#connect to the serial modbus server
connection = client.connect()
print connection
#starting add, num of reg to read, slave unit.
result= client.read_holding_registers(0x00,2,unit= 0xff)
print(result)
#closes the underlying socket connection
client.close()The output I am having is shown below:True
DEBUG:pymodbus.transaction:Running transaction 1
DEBUG:pymodbus.transaction:getting transaction 1
NoneWhat could be the mistake here ?