4
4
Author: Michael Oberdorf IT-Consulting
5
5
Datum: 2020-03-30
6
6
Last modified by: Michael Oberdorf
7
- Last modified at: 2024-07-19
7
+ Last modified at: 2024-10-22
8
8
*************************************************************************** """
9
9
import argparse
10
10
import json
21
21
ModbusSparseDataBlock ,
22
22
)
23
23
from pymodbus .device import ModbusDeviceIdentification
24
- from pymodbus .server .sync import StartTcpServer , StartTlsServer
24
+ from pymodbus .server .sync import StartTcpServer , StartTlsServer , StartUdpServer
25
25
26
26
# default configuration file path
27
27
default_config_file = "/app/modbus_server.json"
28
- VERSION = "1.3.2 "
28
+ VERSION = "1.4.0 "
29
29
30
30
log = logging .getLogger ()
31
31
@@ -55,6 +55,7 @@ def get_ip_address() -> str:
55
55
def run_server (
56
56
listener_address : str = "0.0.0.0" ,
57
57
listener_port : int = 5020 ,
58
+ protocol : str = "TCP" ,
58
59
tls_cert : str = None ,
59
60
tls_key : str = None ,
60
61
zero_mode : bool = False ,
@@ -67,6 +68,7 @@ def run_server(
67
68
Run the modbus server(s)
68
69
@param listener_address: string, IP address to bind the listener (default: '0.0.0.0')
69
70
@param listener_port: integer, TCP port to bin the listener (default: 5020)
71
+ @param protocol: string, defines if the server listenes to TCP or UDP (default: 'TCP')
70
72
@param tls_cert: boolean, path to certificate to start tcp server with TLS (default: None)
71
73
@param tls_key: boolean, path to private key to start tcp server with TLS (default: None)
72
74
@param zero_mode: boolean, request to address(0-7) will map to the address (0-7) instead of (1-8) (default: False)
@@ -157,10 +159,14 @@ def run_server(
157
159
address = (listener_address , listener_port ),
158
160
)
159
161
else :
160
- log .info (f"Starting Modbus TCP server on { listener_address } :{ listener_port } " )
161
- StartTcpServer (context , identity = identity , address = (listener_address , listener_port ))
162
- # TCP with different framer
163
- # StartTcpServer(context, identity=identity, framer=ModbusRtuFramer, address=(listener_address, listener_port))
162
+ if protocol == "UDP" :
163
+ log .info (f"Starting Modbus UDP server on { listener_address } :{ listener_port } " )
164
+ StartUdpServer (context , identity = identity , address = (listener_address , listener_port ))
165
+ else :
166
+ log .info (f"Starting Modbus TCP server on { listener_address } :{ listener_port } " )
167
+ StartTcpServer (context , identity = identity , address = (listener_address , listener_port ))
168
+ # TCP with different framer
169
+ # StartTcpServer(context, identity=identity, framer=ModbusRtuFramer, address=(listener_address, listener_port))
164
170
165
171
166
172
def prepare_register (
@@ -306,13 +312,18 @@ def prepare_register(
306
312
initialize_undefined_registers = CONFIG ["registers" ]["initializeUndefinedRegisters" ],
307
313
)
308
314
315
+ # add TCP protocol to configuration if not defined
316
+ if "protocol" not in CONFIG ["server" ]:
317
+ CONFIG ["server" ]["protocol" ] = "TCP"
318
+
309
319
# try to get the interface IP address
310
320
local_ip_addr = get_ip_address ()
311
321
if local_ip_addr != "" :
312
322
log .info (f"Outbound device IP address is: { local_ip_addr } " )
313
323
run_server (
314
324
listener_address = CONFIG ["server" ]["listenerAddress" ],
315
325
listener_port = CONFIG ["server" ]["listenerPort" ],
326
+ protocol = CONFIG ["server" ]["protocol" ],
316
327
tls_cert = CONFIG ["server" ]["tlsParams" ]["privateKey" ],
317
328
tls_key = CONFIG ["server" ]["tlsParams" ]["certificate" ],
318
329
zero_mode = CONFIG ["registers" ]["zeroMode" ],
0 commit comments