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, framer=FramerType.RTU, identity=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, json_dict)

Build html calls page.

build_json_log(params, json_dict)

Build json log page.

build_json_registers(params, json_dict)

Build html registers page.

build_json_server(params, json_dict)

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_build_html_submit(params)

Build html register submit.

async run_forever(only_start=False)

Start modbus and http servers.

server_request_tracer(request, *_addr)

Trace requests.

All server requests passes this filter before being handled.

server_response_manipulator(response)

Manipulate responses.

All server responses passes this filter before being sent. The filter returns:

  • response, either original or modified

  • skip_encoding, signals whether or not to encode the response

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, framer=FramerType.SOCKET, identity=None, address=('', 502), ignore_missing_slaves=False, broadcast_enable=False, response_manipulator=None, request_tracer=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, framer=FramerType.TLS, identity=None, address=('', 502), sslctx=None, certfile=None, keyfile=None, password=None, ignore_missing_slaves=False, broadcast_enable=False, response_manipulator=None, request_tracer=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, framer=FramerType.SOCKET, identity=None, address=('', 502), ignore_missing_slaves=False, broadcast_enable=False, response_manipulator=None, request_tracer=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, identity=None, custom_functions=[], **kwargs)

Start and run a serial modbus server.

Parameters:
  • context – The ModbusServerContext datastore

  • identity – An optional identify structure

  • custom_functions – An optional list of custom function classes supported by server instance.

  • kwargs – The rest

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

Start and run a tcp modbus server.

Parameters:
  • context – The ModbusServerContext datastore

  • identity – An optional identify structure

  • address – An optional (interface, port) to bind to.

  • custom_functions – An optional list of custom function classes supported by server instance.

  • kwargs – The rest

async pymodbus.server.StartAsyncTlsServer(context=None, identity=None, address=None, sslctx=None, certfile=None, keyfile=None, password=None, custom_functions=[], **kwargs)

Start and run a tls modbus server.

Parameters:
  • context – The ModbusServerContext datastore

  • identity – An optional identify structure

  • address – An optional (interface, port) to bind to.

  • sslctx – The SSLContext to use for TLS (default None and auto create)

  • certfile – The cert file path for TLS (used if sslctx is None)

  • keyfile – The key file path for TLS (used if sslctx is None)

  • password – The password for for decrypting the private key file

  • custom_functions – An optional list of custom function classes supported by server instance.

  • kwargs – The rest

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

Start and run a udp modbus server.

Parameters:
  • context – The ModbusServerContext datastore

  • identity – An optional identify structure

  • address – An optional (interface, port) to bind to.

  • custom_functions – An optional list of custom function classes supported by server instance.

  • kwargs

pymodbus.server.StartSerialServer(**kwargs)

Start and run a serial modbus server.

pymodbus.server.StartTcpServer(**kwargs)

Start and run a serial modbus server.

pymodbus.server.StartTlsServer(**kwargs)

Start and run a serial modbus server.

pymodbus.server.StartUdpServer(**kwargs)

Start and run a serial modbus server.

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

Get command line arguments.