Server

Pymodbus offers servers with transport protocols for

  • Serial (RS-485) typically using a dongle

  • TCP

  • TLS

  • UDP

  • possibility to add a custom transport protocol

communication in 2 versions:

  • synchronous server,

  • asynchronous server using asyncio.

Remark All servers are implemented with asyncio, and the synchronous servers are just an interface layer allowing synchronous applications to use the server as if it was synchronous.

Server.

import external classes, to make them easier to use:

class pymodbus.server.ModbusSerialServer(context: ModbusServerContext, *, framer: FramerType = FramerType.RTU, ignore_missing_slaves: bool = False, identity: ModbusDeviceIdentification | None = None, broadcast_enable: bool = False, trace_packet: Callable[[bool, bytes], bytes] | None = None, trace_pdu: Callable[[bool, ModbusPDU], ModbusPDU] | None = None, trace_connect: Callable[[bool], None] | None = None, **kwargs)

Bases: ModbusBaseServer

A modbus threaded serial socket server.

We inherit and overload the socket server so that we can control the client threads as well as have a single server context instance.

class pymodbus.server.ModbusSimulatorServer(modbus_server: str = 'server', modbus_device: str = 'device', http_host: str = '0.0.0.0', http_port: int = 8080, log_file: str = 'server.log', json_file: str = 'setup.json', custom_actions_module: str | None = None)

Bases: object

ModbusSimulatorServer.

Parameters:
  • modbus_server – Server name in json file (default: “server”)

  • modbus_device – Device name in json file (default: “client”)

  • http_host – TCP host for HTTP (default: “localhost”)

  • http_port – TCP port for HTTP (default: 8080)

  • json_file – setup file (default: “setup.json”)

  • custom_actions_module – python module with custom actions (default: none)

if either http_port or http_host is none, HTTP will not be started. This class starts a http server, that serves a couple of endpoints:

  • “<addr>/” static files

  • “<addr>/api/log” log handling, HTML with GET, REST-API with post

  • “<addr>/api/registers” register handling, HTML with GET, REST-API with post

  • “<addr>/api/calls” call (function code / message) handling, HTML with GET, REST-API with post

  • “<addr>/api/server” server handling, HTML with GET, REST-API with post

Example:

from pymodbus.server import ModbusSimulatorServer

async def run():
    simulator = ModbusSimulatorServer(
        modbus_server="my server",
        modbus_device="my device",
        http_host="localhost",
        http_port=8080)
    await simulator.run_forever(only_start=True)
    ...
    await simulator.stop()
action_add(params, range_start, range_stop)

Build list of registers matching filter.

action_clear(_params, _range_start, _range_stop)

Clear register filter.

action_monitor(params, range_start, range_stop)

Start monitoring calls.

action_reset(_params, _range_start, _range_stop)

Reset call simulation.

action_set(params, _range_start, _range_stop)

Set register value.

action_simulate(params, _range_start, _range_stop)

Simulate responses.

action_stop(_params, _range_start, _range_stop)

Stop call monitoring.

build_html_calls(params: dict, html: str) str

Build html calls page.

build_html_log(_params, html)

Build html log page.

build_html_registers(params, html)

Build html registers page.

build_html_server(_params, html)

Build html server page.

build_json_calls(params: dict) dict

Build json calls response.

build_json_log(params)

Build json log page.

build_json_registers(params)

Build json registers response.

build_json_server(params)

Build html server page.

async handle_html(request)

Handle html.

async handle_html_static(request)

Handle static html.

async handle_json(request)

Handle api registers.

helper_handle_submit(params, submit_actions)

Build html register submit.

async run_forever(only_start=False)

Start modbus and http servers.

async start_modbus_server(app)

Start Modbus server as asyncio task.

async stop()

Stop modbus and http servers.

async stop_modbus_server(app)

Stop modbus server.

class pymodbus.server.ModbusTcpServer(context: ModbusServerContext, *, framer=FramerType.SOCKET, identity: ModbusDeviceIdentification | None = None, address: tuple[str, int] = ('', 502), ignore_missing_slaves: bool = False, broadcast_enable: bool = False, trace_packet: Callable[[bool, bytes], bytes] | None = None, trace_pdu: Callable[[bool, ModbusPDU], ModbusPDU] | None = None, trace_connect: Callable[[bool], None] | None = None)

Bases: ModbusBaseServer

A modbus threaded tcp socket server.

We inherit and overload the socket server so that we can control the client threads as well as have a single server context instance.

class pymodbus.server.ModbusTlsServer(context: ModbusServerContext, *, framer=FramerType.TLS, identity: ModbusDeviceIdentification | None = None, address: tuple[str, int] = ('', 502), sslctx=None, certfile=None, keyfile=None, password=None, ignore_missing_slaves=False, broadcast_enable=False, trace_packet: Callable[[bool, bytes], bytes] | None = None, trace_pdu: Callable[[bool, ModbusPDU], ModbusPDU] | None = None, trace_connect: Callable[[bool], None] | None = None)

Bases: ModbusTcpServer

A modbus threaded tls socket server.

We inherit and overload the socket server so that we can control the client threads as well as have a single server context instance.

class pymodbus.server.ModbusUdpServer(context: ModbusServerContext, *, framer=FramerType.SOCKET, identity: ModbusDeviceIdentification | None = None, address: tuple[str, int] = ('', 502), ignore_missing_slaves: bool = False, broadcast_enable: bool = False, trace_packet: Callable[[bool, bytes], bytes] | None = None, trace_pdu: Callable[[bool, ModbusPDU], ModbusPDU] | None = None, trace_connect: Callable[[bool], None] | None = None)

Bases: ModbusBaseServer

A modbus threaded udp socket server.

We inherit and overload the socket server so that we can control the client threads as well as have a single server context instance.

async pymodbus.server.ServerAsyncStop()

Terminate server.

pymodbus.server.ServerStop()

Terminate server.

async pymodbus.server.StartAsyncSerialServer(context=None, custom_functions=[], **kwargs)

Start and run a serial modbus server.

For parameter explanation see ModbusSerialServer.

parameter custom_functions: optional list of custom function classes.

async pymodbus.server.StartAsyncTcpServer(context, custom_functions=[], **kwargs)

Start and run a tcp modbus server.

For parameter explanation see ModbusTcpServer.

parameter custom_functions: optional list of custom function classes.

async pymodbus.server.StartAsyncTlsServer(context=None, custom_functions=[], **kwargs)

Start and run a tls modbus server.

For parameter explanation see ModbusTlsServer.

parameter custom_functions: optional list of custom function classes.

async pymodbus.server.StartAsyncUdpServer(context=None, custom_functions=[], **kwargs)

Start and run a udp modbus server.

For parameter explanation see ModbusUdpServer.

parameter custom_functions: optional list of custom function classes.

pymodbus.server.StartSerialServer(**kwargs)

Start and run a modbus serial server.

For parameter explanation see ModbusSerialServer.

pymodbus.server.StartTcpServer(**kwargs)

Start and run a modbus TCP server.

For parameter explanation see ModbusTcpServer.

pymodbus.server.StartTlsServer(**kwargs)

Start and run a modbus TLS server.

For parameter explanation see ModbusTlsServer.

pymodbus.server.StartUdpServer(**kwargs)

Start and run a modbus UDP server.

For parameter explanation see ModbusUdpServer.

pymodbus.server.get_simulator_commandline(extras=None, cmdline=None)

Get command line arguments.