{"id":16190625,"url":"https://github.com/tiagocoutinho/modbus-proxy","last_synced_at":"2025-04-15T10:12:21.180Z","repository":{"id":45359754,"uuid":"312387285","full_name":"tiagocoutinho/modbus-proxy","owner":"tiagocoutinho","description":"Connect multiple clients to modbus devices","archived":false,"fork":false,"pushed_at":"2024-09-23T05:36:16.000Z","size":96,"stargazers_count":74,"open_issues_count":8,"forks_count":13,"subscribers_count":12,"default_branch":"master","last_synced_at":"2024-10-11T07:43:46.270Z","etag":null,"topics":["modbus","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tiagocoutinho.png","metadata":{"files":{"readme":"README.md","changelog":"HISTORY.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-11-12T20:24:29.000Z","updated_at":"2024-10-08T08:30:51.000Z","dependencies_parsed_at":"2024-06-21T20:19:53.521Z","dependency_job_id":"f0ee0260-82f5-4892-a454-8845c0345296","html_url":"https://github.com/tiagocoutinho/modbus-proxy","commit_stats":{"total_commits":49,"total_committers":3,"mean_commits":"16.333333333333332","dds":0.04081632653061229,"last_synced_commit":"bc3f120457ef5f5eca695baa1388e0d9bdfb604b"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tiagocoutinho%2Fmodbus-proxy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tiagocoutinho%2Fmodbus-proxy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tiagocoutinho%2Fmodbus-proxy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tiagocoutinho%2Fmodbus-proxy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tiagocoutinho","download_url":"https://codeload.github.com/tiagocoutinho/modbus-proxy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249048740,"owners_count":21204306,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["modbus","python"],"created_at":"2024-10-10T07:43:47.176Z","updated_at":"2025-04-15T10:12:21.148Z","avatar_url":"https://github.com/tiagocoutinho.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ModBus TCP proxy\n\n[![ModBus proxy][pypi-version]](https://pypi.python.org/pypi/modbus-proxy)\n[![Python Versions][pypi-python-versions]](https://pypi.python.org/pypi/modbus-proxy)\n[![Pypi status][pypi-status]](https://pypi.python.org/pypi/modbus-proxy)\n![License][license]\n[![CI][CI]](https://github.com/tiagocoutinho/modbus-proxy/actions/workflows/ci.yml)\n\nMany modbus devices support only one or very few clients. This proxy acts as a bridge between the client and the modbus device. It can be seen as a\nlayer 7 reverse proxy.\nThis allows multiple clients to communicate with the same modbus device.\n\nWhen multiple clients are connected, cross messages are avoided by serializing communication on a first come first served REQ/REP basis.\n\n## Installation\n\nFrom within your favorite python 3 environment type:\n\n`$ pip install modbus-proxy`\n\nNote: On some systems `pip` points to a python 2 installation.\nYou might need to use `pip3` command instead.\n\nAdditionally, if you want logging configuration:\n* YAML: `pip install modbus-proxy[yaml]` (see below)\n* TOML: `pip install modbus-proxy[toml]` (see below)\n\n## Running the server\n\nFirst, you will need write a configuration file where you specify for each modbus device you which to control:\n\n* modbus connection (the modbus device url)\n* listen interface (to which url your clients should connect)\n\nConfiguration files can be written in YAML (*.yml* or *.yaml*) or TOML (*.toml*).\n\nSuppose you have a PLC modbus device listening on *plc1.acme.org:502* and you want your clients to\nconnect to your machine on port 9000. A YAML configuration would look like this:\n\n```yaml\ndevices:\n- modbus:\n    url: plc1.acme.org:502     # device url (mandatory)\n    timeout: 10                # communication timeout (s) (optional, default: 10)\n    connection_time: 0.1       # delay after connection (s) (optional, default: 0)\n  listen:\n    bind: 0:9000               # listening address (mandatory)\n  unit_id_remapping:           # remap/forward unit IDs (optional, empty by default)\n    1: 0\n```\n\nAssuming you saved this file as `modbus-config.yml`, start the server with:\n\n```bash\n$ modbus-proxy -c ./modbus-config.yml\n```\n\nNow, instead of connecting your client(s) to `plc1.acme.org:502` you just need to\ntell them to connect to `*machine*:9000` (where *machine* is the host where\nmodbus-proxy is running).\n\nNote that the server is capable of handling multiple modbus devices. Here is a\nconfiguration example for 2 devices:\n\n```yaml\ndevices:\n- modbus:\n    url: plc1.acme.org:502\n  listen:\n    bind: 0:9000\n- modbus:\n    url: plc2.acme.org:502\n  listen:\n    bind: 0:9001\n```\n\nIf you have a *single* modbus device, you can avoid writting a configuration file by\nproviding all arguments in the command line:\n\n```bash\nmodbus-proxy -b tcp://0:9000 --modbus tcp://plc1.acme.org:502\n```\n\n(hint: run `modbus-proxy --help` to see all available options)\n\n### Forwarding Unit Identifiers\n\nYou can also forward one unit ID to another whilst proxying. This is handy if the target\nmodbus server has a unit on an index that is not supported by one of your clients.\n\n```yaml\ndevices:\n- modbus: ... # see above.\n  listen: ... # see above.\n  unit_id_remapping:\n    1: 0\n```\n\nThe above forwards requests to unit ID 1 to your modbus-proxy server to unit ID 0 on the\nactual modbus server.\n\nNote that **the reverse also applies**: if you forward unit ID 1 to unit ID 0, **all** responses coming from unit 0 will look as if they are coming from 1, so this may pose problems if you want to use unit ID 0 for some clients and unit ID 1 for others (use unit ID 1 for all in that case).\n\n## Running the examples\n\nTo run the examples you will need to have\n[umodbus](https://github.com/AdvancedClimateSystems/uModbus) installed (do it\nwith `pip install umodbus`).\n\nStart the `simple_tcp_server.py` (this will simulate an actual modbus hardware):\n\n```bash\n$ python examples/simple_tcp_server.py -b :5020\n```\n\nYou can run the example client just to be sure direct communication works:\n\n```bash\n$ python examples/simple_tcp_client.py -a 0:5020\nholding registers: [1, 2, 3, 4]\n```\n\nNow for the real test:\n\nStart a modbus-proxy bridge server with:\n\n```bash\n$ modbus-proxy -b tcp://:9000 --modbus tcp://:5020\n```\n\nFinally run a the example client but now address the proxy instead of the server\n(notice we are now using port *9000* and not *5020*):\n\n```bash\n$ python examples/simple_tcp_client.py -a 0:9000\nholding registers: [1, 2, 3, 4]\n```\n## Running as a Service\n1. move the config file to a location you can remember, for example: to `/usr/lib/mproxy-conf.yaml`\n2. go to `/etc/systemd/system/`\n3. use nano or any other text editor of your choice to create a service file `mproxy.service`\n4. the file should contain the following information:\n```\n[Unit]\nDescription=Modbus-Proxy\nAfter=network.target\n\n[Service]\nType=simple\nRestart=always\nExecStart = modbus-proxy -c ./usr/lib/mproxy-conf.yaml\n\n[Install]\nWantedBy=multi-user.target\n```\n5. `run systemctl daemon-reload`\n6. `systemctl enable mproxy.service`\n7. `systemctl start mproxy.service`\n\nThe file names given here are examples, you can choose other names, if you wish.\n\n## Docker\n\nThis project ships with a basic [Dockerfile](\n./Dockerfile) which you can use\nas a base to launch modbus-proxy inside a docker container.\n\nFirst, build the docker image with:\n\n```bash\n$ docker build -t modbus-proxy .\n```\n\n\nTo bridge a single modbus device without needing a configuration file is\nstraight forward:\n\n```bash\n$ docker run -d -p 5020:502 modbus-proxy -b tcp://0:502 --modbus tcp://plc1.acme.org:502\n```\n\nNow you should be able to access your modbus device through the modbus-proxy by\nconnecting your client(s) to `\u003cyour-hostname/ip\u003e:5020`.\n\nIf, instead, you want to use a configuration file, you must mount the file so\nit is visible by the container.\n\nAssuming you have prepared a `conf.yml` in the current directory:\n\n```yaml\ndevices:\n- modbus:\n    url: plc1.acme.org:502\n  listen:\n    bind: 0:502\n```\n\nHere is an example of how to run the container:\n\n```bash\ndocker run -p 5020:502 -v $PWD/conf.yml:/config/modbus-proxy.yml modbus-proxy\n```\n\nBy default the Dockerfile will run `modbus-proxy -c /config/modbus-proxy.yml` so\nif your mounting that volume you don't need to pass any arguments.\n\nNote that for each modbus device you add in the configuration file you need\nto publish the corresponding bind port on the host\n(`-p \u003chost port\u003e:\u003ccontainer port\u003e` argument).\n\n## Logging configuration\n\nLogging configuration can be added to the configuration file by adding a new `logging` keyword.\n\nThe logging configuration will be passed to\n[logging.config.dictConfig()](https://docs.python.org/library/logging.config.html#logging.config.dictConfig)\nso the file contents must obey the\n[Configuration dictionary schema](https://docs.python.org/library/logging.config.html#configuration-dictionary-schema).\n\nHere is a YAML example:\n\n```yaml\ndevices:\n- modbus:\n    url: plc1.acme.org:502\n  listen:\n    bind: 0:9000\nlogging:\n  version: 1\n  formatters:\n    standard:\n      format: \"%(asctime)s %(levelname)8s %(name)s: %(message)s\"\n  handlers:\n    console:\n      class: logging.StreamHandler\n      formatter: standard\n  root:\n    handlers: ['console']\n    level: DEBUG\n```\n\n### `--log-config-file` (deprecated)\n\nLogging configuration file.\n\nIf a relative path is given, it is relative to the current working directory.\n\nIf a `.conf` or `.ini` file is given, it is passed directly to\n[logging.config.fileConfig()](https://docs.python.org/library/logging.config.html#logging.config.fileConfig) so the file contents must\nobey the\n[Configuration file format](https://docs.python.org/library/logging.config.html#configuration-file-format).\n\nA simple logging configuration (also available at [log.conf](examples/log.conf))\nwhich mimics the default configuration looks like this:\n\n```toml\n[formatters]\nkeys=standard\n\n[handlers]\nkeys=console\n\n[loggers]\nkeys=root\n\n[formatter_standard]\nformat=%(asctime)s %(levelname)8s %(name)s: %(message)s\n\n[handler_console]\nclass=StreamHandler\nformatter=standard\n\n[logger_root]\nlevel=INFO\nhandlers=console\n```\n\nA more verbose example logging with a rotating file handler:\n[log-verbose.conf](examples/log-verbose.conf)\n\nThe same example above (also available at [log.yml](examples/log.yml)) can be achieved in YAML with:\n\n```yaml\nversion: 1\nformatters:\n  standard:\n    format: \"%(asctime)s %(levelname)8s %(name)s: %(message)s\"\nhandlers:\n  console:\n    class: logging.StreamHandler\n    formatter: standard\nroot:\n  handlers: ['console']\n  level: DEBUG\n```\n\n## Kubernetes\n\nBased on the existing [Dockerfile](Dockerfile) an example Kubernetes manifest has been created and is located at [examples/kubernetes](examples/kubernetes).\n\n* `ConfigMap` for modbus-proxy config: [`examples/kubernetes/10_modbus-proxy.configmap.yaml`](examples/kubernetes/10_modbus-proxy.configmap.yaml)\n\nAfter the adjustment of the configmap, the manifest could get applied to your Kubernetes Cluster:\n```\n# adjust config\nvim examples/kubernetes/10_modbus-proxy.configmap.yaml\n\n# apply to cluster\nkubectl apply -f examples/kubernetes\n```\n\n## Credits\n\n### Development Lead\n\n* Tiago Coutinho \u003ccoutinhotiago@gmail.com\u003e\n\n### Contributors\n\nNone yet. Why not be the first?\n\n[pypi-python-versions]: https://img.shields.io/pypi/pyversions/modbus-proxy.svg\n[pypi-version]: https://img.shields.io/pypi/v/modbus-proxy.svg\n[pypi-status]: https://img.shields.io/pypi/status/modbus-proxy.svg\n[license]: https://img.shields.io/pypi/l/modbus-proxy.svg\n[CI]: https://github.com/tiagocoutinho/modbus-proxy/actions/workflows/ci.yml/badge.svg\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftiagocoutinho%2Fmodbus-proxy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftiagocoutinho%2Fmodbus-proxy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftiagocoutinho%2Fmodbus-proxy/lists"}