Modbus

Python implementation

Modbus communication protocol

class hat.drivers.modbus.ModbusType(value)

Bases: enum.Enum

An enumeration.

  • TCP

  • RTU

  • ASCII

TCP = 0
RTU = 1
ASCII = 2
class hat.drivers.modbus.DataType(value)

Bases: enum.Enum

An enumeration.

  • COIL

  • DISCRETE_INPUT

  • HOLDING_REGISTER

  • INPUT_REGISTER

  • QUEUE

COIL = 1
DISCRETE_INPUT = 2
HOLDING_REGISTER = 3
INPUT_REGISTER = 4
QUEUE = 5
class hat.drivers.modbus.Error(value)

Bases: enum.Enum

An enumeration.

  • INVALID_FUNCTION_CODE

  • INVALID_DATA_ADDRESS

  • INVALID_DATA_VALUE

  • FUNCTION_ERROR

INVALID_FUNCTION_CODE = 1
INVALID_DATA_ADDRESS = 2
INVALID_DATA_VALUE = 3
FUNCTION_ERROR = 4
async hat.drivers.modbus.create_tcp_master(modbus_type, host, port)

Create TCP master

Parameters
  • modbus_type (hat.drivers.modbus.common.ModbusType) – modbus type

  • host (str) – remote host name

  • port (int) – remote TCP port

Return type

hat.drivers.modbus.master.Master

async hat.drivers.modbus.create_serial_master(modbus_type, port, *, silent_interval=0.005, **kwargs)

Create serial master

Parameters
  • modbus_type (hat.drivers.modbus.common.ModbusType) – modbus type

  • port (str) – port name (see hat.drivers.serial.open())

  • silent_interval (float) – silent interval (see hat.drivers.serial.open())

  • kwargs – additional arguments used for opening serial connection (see hat.drivers.serial.open())

Return type

hat.drivers.modbus.master.Master

class hat.drivers.modbus.Master

Bases: object

Modbus master

For creating new instances of this class see create_tcp_master() or create_serial_master().

property closed

Closed future

async async_close()

Async close

async read(device_id, data_type, start_address, quantity=1)

Read data from modbus device

Argument quantity is ignored if data_type is QUEUE.

Parameters
  • device_id (int) – slave device identifier

  • data_type (hat.drivers.modbus.common.DataType) – data type

  • start_address (int) – starting modbus data address

  • quantity (int) – number of data values

Raises

ConnectionError

Return type

Union[List[int], hat.drivers.modbus.common.Error]

async write(device_id, data_type, start_address, values)

Write data to modbus device

Data types DISCRETE_INPUT, INPUT_REGISTER and QUEUE are not supported.

Parameters
  • device_id (int) – slave device identifier

  • data_type (hat.drivers.modbus.common.DataType) – data type

  • start_address (int) – starting modbus data address

  • values (List[int]) – values

Raises

ConnectionError

Return type

Optional[hat.drivers.modbus.common.Error]

hat.drivers.modbus.SlaveCb

The central part of internal API.

This represents a generic version of type ‘origin’ with type arguments ‘params’. There are two kind of these aliases: user defined and special. The special ones are wrappers around builtin collections and ABCs in collections.abc. These must have ‘name’ always set. If ‘inst’ is False, then the alias can’t be instantiated, this is used by e.g. typing.List and typing.Dict.

alias of Callable[[Slave], Union[None, Awaitable[None]]]

hat.drivers.modbus.ReadCb

The central part of internal API.

This represents a generic version of type ‘origin’ with type arguments ‘params’. There are two kind of these aliases: user defined and special. The special ones are wrappers around builtin collections and ABCs in collections.abc. These must have ‘name’ always set. If ‘inst’ is False, then the alias can’t be instantiated, this is used by e.g. typing.List and typing.Dict.

alias of Callable[[Slave, int, hat.drivers.modbus.common.DataType, int, Optional[int]], Union[List[int], hat.drivers.modbus.common.Error, Awaitable[Union[List[int], hat.drivers.modbus.common.Error]]]]

hat.drivers.modbus.WriteCb

The central part of internal API.

This represents a generic version of type ‘origin’ with type arguments ‘params’. There are two kind of these aliases: user defined and special. The special ones are wrappers around builtin collections and ABCs in collections.abc. These must have ‘name’ always set. If ‘inst’ is False, then the alias can’t be instantiated, this is used by e.g. typing.List and typing.Dict.

alias of Callable[[Slave, int, hat.drivers.modbus.common.DataType, int, List[int]], Union[hat.drivers.modbus.common.Error, None, Awaitable[Optional[hat.drivers.modbus.common.Error]]]]

async hat.drivers.modbus.create_tcp_server(modbus_type, host, port, slave_cb, read_cb, write_cb)

Create TCP server

Parameters
  • modbus_type (hat.drivers.modbus.common.ModbusType) – modbus type

  • host (str) – local listening host name

  • port (int) – local listening TCP port

  • slave_cb (Callable[[Slave], Union[None, Awaitable[None]]]) – slave callback

  • read_cb (Callable[[Slave, int, hat.drivers.modbus.common.DataType, int, Optional[int]], Union[List[int], hat.drivers.modbus.common.Error, Awaitable[Union[List[int], hat.drivers.modbus.common.Error]]]]) – read callback

  • write_cb (Callable[[Slave, int, hat.drivers.modbus.common.DataType, int, List[int]], Union[hat.drivers.modbus.common.Error, None, Awaitable[Optional[hat.drivers.modbus.common.Error]]]]) – write callback

Return type

hat.drivers.modbus.slave.TcpServer

async hat.drivers.modbus.create_serial_slave(modbus_type, port, read_cb, write_cb, *, silent_interval=0.005, **kwargs)

Create serial slave

Parameters
  • modbus_type (hat.drivers.modbus.common.ModbusType) – modbus type

  • port (str) – port name (see hat.drivers.serial.open())

  • read_cb (Callable[[Slave, int, hat.drivers.modbus.common.DataType, int, Optional[int]], Union[List[int], hat.drivers.modbus.common.Error, Awaitable[Union[List[int], hat.drivers.modbus.common.Error]]]]) – read callback

  • write_cb (Callable[[Slave, int, hat.drivers.modbus.common.DataType, int, List[int]], Union[hat.drivers.modbus.common.Error, None, Awaitable[Optional[hat.drivers.modbus.common.Error]]]]) – write callback

  • silent_interval (float) – silent interval (see hat.drivers.serial.open())

  • kwargs – additional arguments used for opening serial connection (see hat.drivers.serial.open())

Return type

hat.drivers.modbus.slave.Slave

class hat.drivers.modbus.TcpServer

Bases: object

TCP server

For creating new instances of this class see create_tcp_server().

property closed

Closed future

async async_close()

Async close server and all active associated slaves

class hat.drivers.modbus.Slave

Bases: object

Modbus slave

For creating new instances of this class see create_tcp_server() or create_serial_slave().

property closed

Closed future

async async_close()

Async close