|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Pymodbus Simulator Example. |
| 3 | +
|
| 4 | +An example of using simulator datastore with json interface. |
| 5 | +
|
| 6 | +usage: server_simulator.py [-h] [--path JSON_FILE] |
| 7 | + [--log {critical,error,warning,info,debug}] |
| 8 | + [--port PORT] |
| 9 | +
|
| 10 | +Command line options for examples |
| 11 | +
|
| 12 | +options: |
| 13 | + -h, --help show this help message and exit |
| 14 | + --path JSON_FILE path to json device configuration file |
| 15 | + --log {critical,error,warning,info,debug} |
| 16 | + "critical", "error", "warning", "info" or "debug" |
| 17 | + --port PORT the port to use |
| 18 | +
|
| 19 | +The corresponding client can be started as: |
| 20 | + python3 client_sync.py |
| 21 | +""" |
| 22 | +import argparse |
| 23 | +import asyncio |
| 24 | +import logging |
| 25 | + |
| 26 | +from pymodbus import pymodbus_apply_logging_config |
| 27 | +from pymodbus.datastore import ModbusServerContext, ModbusSimulatorContext |
| 28 | +from pymodbus.server import StartAsyncTcpServer |
| 29 | +from pymodbus.transaction import ModbusSocketFramer |
| 30 | + |
| 31 | + |
| 32 | +_logger = logging.getLogger() |
| 33 | + |
| 34 | + |
| 35 | +def get_commandline(): |
| 36 | + """Read and validate command line arguments""" |
| 37 | + parser = argparse.ArgumentParser(description="Run server simulator.") |
| 38 | + parser.add_argument( |
| 39 | + "--log", |
| 40 | + choices=["critical", "error", "warning", "info", "debug"], |
| 41 | + help="set log level, default is info", |
| 42 | + default="info", |
| 43 | + type=str, |
| 44 | + ) |
| 45 | + parser.add_argument( |
| 46 | + "--port", |
| 47 | + help="set port", |
| 48 | + type=str, |
| 49 | + ) |
| 50 | + parser.add_argument( |
| 51 | + "--json", |
| 52 | + help="path to json device configuration file", |
| 53 | + default=None, |
| 54 | + type=str, |
| 55 | + ) |
| 56 | + args = parser.parse_args() |
| 57 | + |
| 58 | + pymodbus_apply_logging_config() |
| 59 | + _logger.setLevel(args.log.upper()) |
| 60 | + args.framer = ModbusSocketFramer |
| 61 | + args.port = int(args.port) or 5020 |
| 62 | + return args |
| 63 | + |
| 64 | + |
| 65 | +def setup_server(args, json_dict=None): |
| 66 | + """Run server setup.""" |
| 67 | + _logger.info("### Create datastore") |
| 68 | + context = ModbusSimulatorContext() |
| 69 | + |
| 70 | + if args.path: |
| 71 | + context.load_file(args.path, None) |
| 72 | + else: |
| 73 | + context.load_dict(json_dict, None) |
| 74 | + args.context = ModbusServerContext(slaves=context, single=True) |
| 75 | + return args |
| 76 | + |
| 77 | + |
| 78 | +async def run_server_simulator(args): |
| 79 | + """Run server.""" |
| 80 | + _logger.info("### start server simulator") |
| 81 | + await StartAsyncTcpServer( |
| 82 | + context=args.context, |
| 83 | + address=("", args.port) if args.port else None, |
| 84 | + framer=args.framer, # The framer strategy to use |
| 85 | + allow_reuse_address=True, # allow the reuse of an address |
| 86 | + ) |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == "__main__": |
| 90 | + cmd_args = get_commandline() |
| 91 | + run_args = setup_server(cmd_args) |
| 92 | + asyncio.run(run_server_simulator(run_args), debug=True) |
0 commit comments