Python ModbusClient – Methods

Class:  ModbusClient


Constructor def __init__(self, *params)

Constructor for Modbus RTU (serial line):
modbusClient = ModbusClient.ModbusClient(‘COM1’)
First Parameter is the serial Port

Constructor for Modbus TCP:
modbusClient = ModbusClient.ModbusClient(‘127.0.0.1’, 502)
First Parameter ist the IP-Address of the Server to connect to
Second Parameter is the Port the Server listens to


def Connect(self)

Connects to a Modbus-TCP Server or a Modbus-RTU Slave with the given Parameters


def close(self)

Closes Serial port, or TCP-Socket connection


def ReadDiscreteInputs(self, startingAddress, quantity)

Read Discrete Inputs from Server device (Function code 2)
startingAddress: First discrete input to be read
quantity: Numer of discrete Inputs to be read
returns: Boolean Array [0..quantity-1] which contains the discrete Inputs


def ReadCoils(self, startingAddress, quantity)

Read Coils from Server device (Function code 1)
startingAddress: First coil to be read
quantity: Numer of coils to be read
returns: Boolean Array [0..quantity-1] which contains the coils


def ReadHoldingRegisters(self, startingAddress, quantity)

Read Holding Registers from Server device (Function code 3)
startingAddress: First holding register to be read
quantity: Number of holding registers to be read
returns: Int Array [0..quantity-1] which contains the holding registers


def ReadInputRegisters(self, startingAddress, quantity)

Read Input Registers from Server device (Function code 4)
startingAddress : First input register to be read
quantity: Number of input registers to be read
returns: Int Array [0..quantity-1] which contains the input registers


def WriteSingleCoil(self, startingAddress, value)

Write single Coil to Server device (Function code 5)
startingAddress: Coil to be written
value: Coil Value to be written


def WriteSingleRegister(self, startingAddress, value)

Write single Register to Server device (Function code 6)
startingAddress: Register to be written
value: Register Value to be written


def WriteMultipleCoils(self, startingAddress, values)

Write multiple coils to Server device (Function code 15)
startingAddress : First coil to be written
values: Coil Values [0..quantity-1] to be written


def WriteMultipleRegisters(self, startingAddress, values)

Write multiple registers to Server device (Function code 16)
startingAddress: First register to be written
values: Register Values [0..quantity-1] to be written