8
8
9
9
import asyncio
10
10
import logging
11
+ import sys
11
12
12
13
from pymodbus import FramerType , pymodbus_apply_logging_config
13
14
from pymodbus .datastore import (
19
20
from pymodbus .server import ModbusTcpServer
20
21
21
22
23
+ try :
24
+ import helper # type: ignore[import-not-found]
25
+ except ImportError :
26
+ print ("*** ERROR --> THIS EXAMPLE needs the example directory, please see \n \
27
+ https://pymodbus.readthedocs.io/en/latest/source/examples.html\n \
28
+ for more information." )
29
+ sys .exit (- 1 )
30
+
31
+
22
32
class Manipulator :
23
33
"""A Class to run the server."""
24
34
@@ -43,8 +53,9 @@ def trace_connect(self, connect: bool) -> None:
43
53
txt = "Connected" if connect else "Disconnected"
44
54
print (f"---> { txt } " )
45
55
46
- async def setup (self ):
56
+ async def setup (self , cmdline ):
47
57
"""Prepare server."""
58
+ args = helper .get_commandline (server = True , description = "server hooks" , cmdline = cmdline )
48
59
pymodbus_apply_logging_config (logging .DEBUG )
49
60
datablock = ModbusSequentialDataBlock (0x00 , [17 ] * 100 )
50
61
context = ModbusServerContext (
@@ -53,11 +64,12 @@ async def setup(self):
53
64
),
54
65
single = True ,
55
66
)
67
+ address : tuple [str , int ] = (args .host if args .host else "" , args .port if args .port else 0 )
56
68
self .server = ModbusTcpServer (
57
69
context ,
58
70
framer = FramerType .SOCKET ,
59
71
identity = None ,
60
- address = ( "127.0.0.1" , 5020 ) ,
72
+ address = address ,
61
73
trace_packet = self .trace_packet ,
62
74
trace_pdu = self .trace_pdu ,
63
75
trace_connect = self .trace_connect ,
@@ -68,10 +80,10 @@ async def run(self):
68
80
await self .server .serve_forever ()
69
81
70
82
71
- async def main ():
83
+ async def main (cmdline = None ):
72
84
"""Run example."""
73
85
server = Manipulator ()
74
- await server .setup ()
86
+ await server .setup (cmdline = cmdline )
75
87
await server .run ()
76
88
77
89
0 commit comments