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.
Warning The current framer implementation does not support running the server on a shared rs485 line (multipoint).
Server classes.
- 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, custom_pdu: list[type[ModbusPDU]] | None = None, **kwargs)
Bases:
ModbusBaseServer
A modbus threaded serial socket server.
Tip
Remember to call serve_forever to start server.
- 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, custom_pdu: list[type[ModbusPDU]] | None = None)
Bases:
ModbusBaseServer
A modbus threaded tcp socket server.
Tip
Remember to call serve_forever to start server.
- 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, custom_pdu: list[type[ModbusPDU]] | None = None)
Bases:
ModbusTcpServer
A modbus threaded tls socket server.
Tip
Remember to call serve_forever to start server.
- 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, custom_pdu: list[type[ModbusPDU]] | None = None)
Bases:
ModbusBaseServer
A modbus threaded udp socket server.
Tip
Remember to call serve_forever to start server.
- async pymodbus.server.ServerAsyncStop() None
Terminate server.
- pymodbus.server.ServerStop() None
Terminate server.
- async pymodbus.server.StartAsyncSerialServer(context: ModbusServerContext, custom_functions: list[type[ModbusPDU]] | None = None, **kwargs) None
Start and run a serial modbus server.
- Parameters:
context – Datastore object
custom_functions – optional list of custom PDU objects
kwargs – for parameter explanation see ModbusSerialServer
Tip
Only handles a single server !
Use ModbusSerialServer to allow multiple servers in one app.
- async pymodbus.server.StartAsyncTcpServer(context: ModbusServerContext, custom_functions: list[type[ModbusPDU]] | None = None, **kwargs) None
Start and run a tcp modbus server.
- Parameters:
context – Datastore object
custom_functions – optional list of custom PDU objects
kwargs – for parameter explanation see ModbusTcpServer
Tip
Only handles a single server !
Use ModbusTcpServer to allow multiple servers in one app.
- async pymodbus.server.StartAsyncTlsServer(context: ModbusServerContext, custom_functions: list[type[ModbusPDU]] | None = None, **kwargs) None
Start and run a tls modbus server.
- Parameters:
context – Datastore object
custom_functions – optional list of custom PDU objects
kwargs – for parameter explanation see ModbusTlsServer
Tip
Only handles a single server !
Use ModbusTlsServer to allow multiple servers in one app.
- async pymodbus.server.StartAsyncUdpServer(context: ModbusServerContext, custom_functions: list[type[ModbusPDU]] | None = None, **kwargs) None
Start and run a udp modbus server.
- Parameters:
context – Datastore object
custom_functions – optional list of custom PDU objects
kwargs – for parameter explanation see ModbusUdpServer
Tip
Only handles a single server !
Use ModbusUdpServer to allow multiple servers in one app.
- pymodbus.server.StartSerialServer(context: ModbusServerContext, custom_functions: list[type[ModbusPDU]] | None = None, **kwargs) None
Start and run a modbus serial server.
- Parameters:
context – Datastore object
custom_functions – optional list of custom PDU objects
kwargs – for parameter explanation see ModbusSerialServer
Tip
Only handles a single server !
Use ModbusSerialServer to allow multiple servers in one app.
- pymodbus.server.StartTcpServer(context: ModbusServerContext, custom_functions: list[type[ModbusPDU]] | None = None, **kwargs) None
Start and run a modbus TCP server.
- Parameters:
context – Datastore object
custom_functions – optional list of custom PDU objects
kwargs – for parameter explanation see ModbusTcpServer
Tip
Only handles a single server !
Use ModbusTcpServer to allow multiple servers in one app.
- pymodbus.server.StartTlsServer(context: ModbusServerContext, custom_functions: list[type[ModbusPDU]] | None = None, **kwargs) None
Start and run a modbus TLS server.
- Parameters:
context – Datastore object
custom_functions – optional list of custom PDU objects
kwargs – for parameter explanation see ModbusTlsServer
Tip
Only handles a single server !
Use ModbusTlsServer to allow multiple servers in one app.
- pymodbus.server.StartUdpServer(context: ModbusServerContext, custom_functions: list[type[ModbusPDU]] | None = None, **kwargs) None
Start and run a modbus UDP server.
- Parameters:
context – Datastore object
custom_functions – optional list of custom PDU objects
kwargs – for parameter explanation see ModbusUdpServer
Tip
Only handles a single server !
Use ModbusUdpServer to allow multiple servers in one app.
- pymodbus.server.get_simulator_commandline(extras=None, cmdline=None)
Get command line arguments.