Modbus

Overview

This is the central main component for your tests. In the first line of each Testfile you should import this module. Its highly recommended to import always the whole module:

from testframework import *

The testframework contains multiple functions:

Overview of testframework functions

Group

Available functions

Coils (bool values)

Registers (int values)

Others

Functions

class Modbus.ModbusMaster(_rs485: <module 'ClassRS485' from '/github/workspace/ports/stm32/boards/CHECKBOX/Testframework/ClassRS485.py'>)
async readCoils(slaveId: int, registerStart: int, amount: int)

Reads the discrete output value of multiple coils (function code 1)

Parameters:
  • slaveId (int) – Slave ID to be read from

  • registerStart (int) – Start address of the first coil

  • amount (int) – Amount of coils which should be read

Returns:

Array with values of the coils

Return type:

array of bool

Example usage:

Reads from Slave ID 1 the value of coils 2, 3, 4, 5

from testframework import *

#ext.init()     #only neccessary if you run this code in debug console, outside of a testrun
rs485 = ext.getRS485(0)
modbus = ModbusMaster(rs485)

val = await modbus.readCoils(1, 2, 4)
print(val)
async readDiscreteInputs(slaveId: int, registerStart: int, amount: int)

Reads the discrete input values (function code 2)

Parameters:
  • slaveId (int) – Slave ID to be read from

  • registerStart (int) – Start address of the first coil

  • amount (int) – Amount of coils which should be read

Returns:

Array with values of the coils

Return type:

array of bool

Example usage:

Reads from Slave ID 7 the input values 4 and 5

from testframework import *

#ext.init()     #only neccessary if you run this code in debug console, outside of a testrun
rs485 = ext.getRS485(0)
modbus = ModbusMaster(rs485)

val = await modbus.readDiscreteInputs(7, 4, 2)
print(val)
async readHoldingRegisters(slaveId: int, registerStart: int, amount: int)

Reads multiple output holding registers (function code 3)

Parameters:
  • slaveId (int) – Slave ID to be read from

  • registerStart (int) – Start address of the first coil

  • amount (int) – Amount of coils which should be read

Returns:

Value of the register, None on error

Return type:

array of int

Example usage:

from testframework import *

#ext.init()     #only neccessary if you run this code in debug console, outside of a testrun
rs485 = ext.getRS485(0)
modbus = ModbusMaster(rs485)

val = await modbus.readHoldingRegisters(1, 0, 4)
print(val)
async readInputRegisters(slaveId: int, registerStart: int, amount: int)

Reads an input register (function code 4)

Parameters:
  • slaveId (int) – Slave ID to be read from

  • registerStart (int) – Start address of the first coil

  • amount (int) – Amount of coils which should be read

Returns:

Value of the register

Return type:

array of int

Example usage:

from testframework import *

#ext.init()     #only neccessary if you run this code in debug console, outside of a testrun
rs485 = ext.getRS485(0)
modbus = ModbusMaster(rs485)

val = await modbus.readInputRegisters(1, 0, 1)
print(val)
async writeSingleCoil(slaveId: int, address: int, value: bool)

Writes to a single discrete output (function code 5)

Parameters:
  • slaveId (int) – Slave ID to be read from

  • address (int) – address to write

  • value (bool) – value to write

Returns:

True on success, False on failure

Return type:

bool

Example usage:

from testframework import *

#ext.init()     #only neccessary if you run this code in debug console, outside of a testrun
rs485 = ext.getRS485(0)
modbus = ModbusMaster(rs485)

await modbus.writeSingleCoil(1, 3, True)
async writeSingleRegister(slaveId: int, address: int, value: int)

Writes to a single output register (function code 6)

Parameters:
  • slaveId (int) – Slave ID to be read from

  • address (int) – address to write

  • value (int) – value to write

Returns:

True on success, False on failure

Return type:

bool

Example usage:

from testframework import *

#ext.init()     #only neccessary if you run this code in debug console, outside of a testrun
rs485 = ext.getRS485(0)
modbus = ModbusMaster(rs485)

await modbus.writeSingleRegister(1, 3, 2500)
async writeMultipleCoils(slaveId: int, startAddress: int, values: list)

Writes multiple discrete outputs (function code 15)

Parameters:
  • slaveId (int) – Slave ID to be read from

  • startAddress (int) – first address to start

  • values (list of bool) – values to write

Returns:

True on success, False on failure

Return type:

bool

Example usage:

from testframework import *

#ext.init()     #only neccessary if you run this code in debug console, outside of a testrun
rs485 = ext.getRS485(0)
modbus = ModbusMaster(rs485)

await modbus.writeMultipleCoils(7, 0, (True, True, False))
async writeMultipleRegisters(slaveId: int, startAddress: int, values: list)

Writes multiple registers (function code 16)

Parameters:
  • slaveId (int) – Slave ID to be read from

  • startAddress (int) – first address to start

  • values (list of int) – values to write

Returns:

True on success, False on failure

Return type:

bool

Example usage:

from testframework import *

#ext.init()     #only neccessary if you run this code in debug console, outside of a testrun
rs485 = ext.getRS485(0)
modbus = ModbusMaster(rs485)

await modbus.writeMultipleRegisters(3, 2, (15, 20400, 4950, 0))
async doBaudrateCheck(baudrate, command: str) int

Checks which is the minimum and maximum baudrate where an answer is received

Parameters:
  • baudrate (int) – baudrate from which the test should be start

  • command (str) – function inclusive parameter which should be called. This have to be a function of the ModbusMaster class.

Returns:

BaudrateCheckResult object with the min, max and mean baudrate

Return type:

BaudrateCheckResult

Example usage:


async def BaudrateCheck():

rs485 = ext.getRS485(0) rs485.setBaudrate(115200) rs485.setTermination(True)

modbus = ModbusMaster(rs485) result = await modbus.doBaudrateCheck(115200, “readCoils(1, 0, 1)”)

report.putInfoValue(“Minimum working baudrate”, result.min) report.putInfoValue(“Maximum working baudrate”, result.max) report.putInfoValue(“Mean working baudrate”, result.mean)