Skip to content

pymodbus.simulator ignores command line arguments "http_port" and "json_file" #1455

@chkr-private

Description

@chkr-private

Versions

  • Python: 3.10.10
  • OS: Fedora 36
  • Pymodbus: 3.2.2

Pymodbus Specific

  • Server: tcp
  • Client: tcp

Description

  • when starting pymodbus.simulator with a specific json file, an error stating a different filename is shown
$ pymodbus.simulator --json_file /tmp/setup.json 
2023-03-25 20:06:03,532 INFO  logging:96 Start simulator
Traceback (most recent call last):
  File "/usr/bin/pymodbus.simulator", line 8, in <module>
    sys.exit(main())
  File "/usr/lib/python3.10/site-packages/pymodbus/server/simulator/main.py", line 119, in main
    asyncio.run(run_main(), debug=True)
  File "/usr/lib64/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib64/python3.10/asyncio/base_events.py", line 649, in run_until_complete
    return future.result()
  File "/usr/lib/python3.10/site-packages/pymodbus/server/simulator/main.py", line 113, in run_main
    task = ModbusSimulatorServer(**cmd_args)
  File "/usr/lib/python3.10/site-packages/pymodbus/server/simulator/http_server.py", line 130, in __init__
    with open(json_file, encoding="utf-8") as file:
FileNotFoundError: [Errno 2] No such file or directory: './pymodbus/server/simulator/setup.json'
  • the problem is in
    cmd_args["json_file"] = "./pymodbus/server/simulator/setup.json"
    where the command line arguments are overwritten
  • if it is the intention having default values, a patch keeping exactly these defaults would look like this:
diff --git a/pymodbus/server/simulator/main.py b/pymodbus/server/simulator/main.py
index 09af492..443e1a6 100755
--- a/pymodbus/server/simulator/main.py
+++ b/pymodbus/server/simulator/main.py
@@ -70,6 +70,7 @@ def get_commandline():
     parser.add_argument(
         "--http_port",
         help="use <http_port> as port to bind http listen",
+        default=8081,
         type=str,
     )
     parser.add_argument(
@@ -82,6 +83,7 @@ def get_commandline():
     parser.add_argument(
         "--json_file",
         help='name of json file, default is "setup.json"',
+        default="./pymodbus/server/simulator/setup.json",
         type=str,
     )
     parser.add_argument(
@@ -109,8 +111,6 @@ def get_commandline():
 async def run_main():
     """Run server async."""
     cmd_args = get_commandline()
-    cmd_args["http_port"] = 8081
-    cmd_args["json_file"] = "./pymodbus/server/simulator/setup.json"
     task = ModbusSimulatorServer(**cmd_args)
     await task.run_forever()
  • however, for running pymodbus.simulator after installing without any arguments, I would suggest the following:
    • install the default setup.json e.g. into the same directory as main.py of the simulator
    • use a diff like this:
diff --git a/pymodbus/server/simulator/main.py b/pymodbus/server/simulator/main.py
index 09af492..086d724 100755
--- a/pymodbus/server/simulator/main.py
+++ b/pymodbus/server/simulator/main.py
@@ -41,6 +41,7 @@ options:
 """
 import argparse
 import asyncio
+import os
 
 from pymodbus import pymodbus_apply_logging_config
 from pymodbus.logging import Log
@@ -70,6 +71,7 @@ def get_commandline():
     parser.add_argument(
         "--http_port",
         help="use <http_port> as port to bind http listen",
+        default=8081,
         type=str,
     )
     parser.add_argument(
@@ -82,6 +84,7 @@ def get_commandline():
     parser.add_argument(
         "--json_file",
         help='name of json file, default is "setup.json"',
+        default=os.path.join(os.path.dirname(__file__), "setup.json"),
         type=str,
     )
     parser.add_argument(
@@ -109,8 +112,6 @@ def get_commandline():
 async def run_main():
     """Run server async."""
     cmd_args = get_commandline()
-    cmd_args["http_port"] = 8081
-    cmd_args["json_file"] = "./pymodbus/server/simulator/setup.json"
     task = ModbusSimulatorServer(**cmd_args)
     await task.run_forever()
 
  • please provide some guidance which way to go, then I'll create an according pull request

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions