MODBUS Library

This module contains all functionalities to communicate with a Modbus device. The communication can be of the serial or TCP type. When a connection with a slave device has been established, the registers can be read and write.
read_coils(address, n)
Parameters:
  • address – The starting address
  • n – the number of coils to read, starting from address.

Reads the status of n coils, starting from address.

Returns a list containing the status of coils read if the read has been successfull, otherwhise an exception will be raised.

read_discrete(address, n)
Parameters:
  • address – The starting address
  • n – the number of discrete register to read, starting from address.

Reads the status of n discrete registers, starting from address.

Returns a list containing the status of discrete registers read if the read has been successfull, otherwhise an exception will be raised.

read_holding(address, n)
Parameters:
  • address – The starting address
  • n – the number of holding register to read, starting from address.

Reads the values of n holding registers, starting from address.

Returns a list containing the values of holding registers read if the read has been successfull, otherwhise an exception will be raised.

read_input(address, n)
Parameters:
  • address – The starting address
  • n – the number of input register to read, starting from address.

Reads the values of n input registers, starting from address.

Returns a list containing the values of input registers read if the read has been successfull, otherwhise an exception will be raised.

write_coil(address, value)
Parameters:
  • address – The address of the coil
  • value – is the value to write.

Returns the value written in the coil if the write has been successfull, otherwhise an exception will be raised.

write_register(address, value)
Parameters:
  • address – The address of the register
  • value – is the value to write.

Returns the value written in the holding register if the write has been successfull, otherwhise an exception will be raised.

write_multiple_coils(address, n, values)
Parameters:
  • address – The address of the first coil
  • n – the number of coils
  • values – a list containing the new values to write.

Returns the number of coils written if the write has been successfull, otherwhise an exception will be raised.

write_multiple_registers(address, n, values)
Parameters:
  • address – The address of the first holding register
  • n – the number of registers
  • values – a list containing the new values to write.

Returns the number of holding registers written if the write has been successfull, otherwhise an exception will be raised.

ModbusTCP class

class ModbusTCP(identifier)

Creates an instance of the ModbusTCP class which allow Modbus communication with slave device using TCP.

It inherits all of its methods from MODBUS Library described above.

Parameters:identifier – The slave device identifier, used in the header of every packet.
connect(address, port = 502)
Parameters:
  • address – The ip address of the slave device
  • port – port on which the slave device is listening to (default value is 502).

Create the connection with the slave device.

close()

Close the connection with the slave device.

ConfigSerial class

class ConfigSerial(drvname, baud, rs485en = None, stopbits = STOPBIT_1, parity = PARITY_NONE, receive_sleep = 250)

Creates an object to set some parameters to communicate with serial port.

Parameters:
  • drvname – is the serial name
  • baud – is the baudrate
  • rs485en – the pin used for RS485 communication (default value is None)
  • stopbitsSTOPBIT_1 , STOPBIT_1_HALF, STOPBIT_2 (default value is STOPBIT_1)
  • parityPARITY_NONE or PARITY_EVEN (default value is PARITY_NONE)
  • receive_sleep – timeout on the receiving function (default value is 250).

ModbusSerial class

class ModbusSerial(identifier, drvname = None, baud = None, rs485en = None, cfg = None)

Creates an instance of the ModbusSerial class which allow Modbus communication with slave device using RTU.

It inherits all of its methods from MODBUS Library described above.

Parameters:
  • identifier – The slave device identifier
  • drvname – is the serial name (default value is None)
  • baud – is the baudrate (default value is None)
  • rs485en – is the pin used for RS485 communication (default value is None)
  • cfg – is an object of the ConfigSerial class described above (default value is None).

If you want communicate with a device through serial communication, you can create a ConfigSerial class object, in order to set more parameters, like showing in this example:

from modbus import modbus

...

config_serial = modbus.ConfigSerial(drvname=SERIAL1, baud=9600)
master = modbus.ModbusSerial(1, cfg = config_serial)
result = master.write_register(2, 10)
print("Value written in the register: ", result)

or you can pass the drvname, baudrate and rs485en directly to ModbusSerial like showing in this other example:

from modbus import modbus

...

master = modbus.ModbusSerial(1, drvname=SERIAL1, baud=9600, rs485en=D40)
result = master.write_register(2, 10)
print("Value written in the register: ", result)
close()

Close the serial port.