|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Pymodbus synchronous forwarder. |
| 3 | +
|
| 4 | +This is a repeater or converter and an example of just how powerful datastore is. |
| 5 | +
|
| 6 | +It consist of a server (any comm) and a client (any comm) and basically all request |
| 7 | +received by the server is sent by client and all responses received by the |
| 8 | +client is sent back by the server. |
| 9 | +
|
| 10 | +Both server and client are tcp based, but it can be easily modified to any server/client |
| 11 | +(see client_sync.py and server_sync.py for other communication types) |
| 12 | +""" |
| 13 | +import argparse |
| 14 | +import logging |
| 15 | + |
| 16 | +from pymodbus.client.sync import ModbusTcpClient |
| 17 | +from pymodbus.server.sync import StartTcpServer |
| 18 | +from pymodbus.datastore import ModbusServerContext |
| 19 | +from pymodbus.datastore.remote import RemoteSlaveContext |
| 20 | + |
| 21 | + |
| 22 | +def run_forwarder(): |
| 23 | + """Run forwarder setup.""" |
| 24 | + port_server, port_client = get_commandline() |
| 25 | + |
| 26 | + client = ModbusTcpClient( |
| 27 | + host="localhost", |
| 28 | + port=port_client, |
| 29 | + ) |
| 30 | + |
| 31 | + # If required to communicate with a specified client use unit=<unit_id> |
| 32 | + # in RemoteSlaveContext |
| 33 | + # For e.g to forward the requests to slave with unit address 1 use |
| 34 | + # store = RemoteSlaveContext(client, unit=1) |
| 35 | + store = RemoteSlaveContext(client) |
| 36 | + context = ModbusServerContext(slaves=store, single=True) |
| 37 | + |
| 38 | + # start forwarding client and server |
| 39 | + client.connect() |
| 40 | + StartTcpServer(context, address=("localhost", port_server)) |
| 41 | + # loop forever |
| 42 | + |
| 43 | + |
| 44 | +# --------------------------------------------------------------------------- # |
| 45 | +# Extra code, to allow commandline parameters instead of changing the code |
| 46 | +# --------------------------------------------------------------------------- # |
| 47 | +FORMAT = "%(asctime)-15s %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s" |
| 48 | +logging.basicConfig(format=FORMAT) |
| 49 | +_logger = logging.getLogger() |
| 50 | + |
| 51 | + |
| 52 | +def get_commandline(): |
| 53 | + """Read and validate command line arguments""" |
| 54 | + parser = argparse.ArgumentParser(description="Command line options for examples") |
| 55 | + parser.add_argument( |
| 56 | + "--log", |
| 57 | + choices=["critical", "error", "warning", "info", "debug"], |
| 58 | + help='"critical", "error", "warning", "info" or "debug"', |
| 59 | + type=str, |
| 60 | + ) |
| 61 | + parser.add_argument( |
| 62 | + "--port", |
| 63 | + help='the port to use', |
| 64 | + type=int, |
| 65 | + ) |
| 66 | + parser.add_argument( |
| 67 | + "--port_client", |
| 68 | + help='the port to use', |
| 69 | + type=int, |
| 70 | + ) |
| 71 | + args = parser.parse_args() |
| 72 | + |
| 73 | + # set defaults |
| 74 | + _logger.setLevel(args.log.upper() if args.log else logging.INFO) |
| 75 | + if not args.port: |
| 76 | + args.port = 5020 |
| 77 | + if not args.port_client: |
| 78 | + args.port_client = 5010 |
| 79 | + |
| 80 | + return args.port, args.port_client |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + run_forwarder() |
0 commit comments