Skip to content

Commit b00e5f2

Browse files
committed
Add UDP mode
1 parent 17f0772 commit b00e5f2

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/app/modbus_server.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Author: Michael Oberdorf IT-Consulting
55
Datum: 2020-03-30
66
Last modified by: Michael Oberdorf
7-
Last modified at: 2024-07-19
7+
Last modified at: 2024-10-22
88
*************************************************************************** """
99
import argparse
1010
import json
@@ -21,11 +21,11 @@
2121
ModbusSparseDataBlock,
2222
)
2323
from pymodbus.device import ModbusDeviceIdentification
24-
from pymodbus.server.sync import StartTcpServer, StartTlsServer
24+
from pymodbus.server.sync import StartTcpServer, StartTlsServer, StartUdpServer
2525

2626
# default configuration file path
2727
default_config_file = "/app/modbus_server.json"
28-
VERSION = "1.3.2"
28+
VERSION = "1.4.0"
2929

3030
log = logging.getLogger()
3131

@@ -55,6 +55,7 @@ def get_ip_address() -> str:
5555
def run_server(
5656
listener_address: str = "0.0.0.0",
5757
listener_port: int = 5020,
58+
protocol: str = "TCP",
5859
tls_cert: str = None,
5960
tls_key: str = None,
6061
zero_mode: bool = False,
@@ -67,6 +68,7 @@ def run_server(
6768
Run the modbus server(s)
6869
@param listener_address: string, IP address to bind the listener (default: '0.0.0.0')
6970
@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')
7072
@param tls_cert: boolean, path to certificate to start tcp server with TLS (default: None)
7173
@param tls_key: boolean, path to private key to start tcp server with TLS (default: None)
7274
@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(
157159
address=(listener_address, listener_port),
158160
)
159161
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))
164170

165171

166172
def prepare_register(
@@ -306,13 +312,18 @@ def prepare_register(
306312
initialize_undefined_registers=CONFIG["registers"]["initializeUndefinedRegisters"],
307313
)
308314

315+
# add TCP protocol to configuration if not defined
316+
if "protocol" not in CONFIG["server"]:
317+
CONFIG["server"]["protocol"] = "TCP"
318+
309319
# try to get the interface IP address
310320
local_ip_addr = get_ip_address()
311321
if local_ip_addr != "":
312322
log.info(f"Outbound device IP address is: {local_ip_addr}")
313323
run_server(
314324
listener_address=CONFIG["server"]["listenerAddress"],
315325
listener_port=CONFIG["server"]["listenerPort"],
326+
protocol=CONFIG["server"]["protocol"],
316327
tls_cert=CONFIG["server"]["tlsParams"]["privateKey"],
317328
tls_key=CONFIG["server"]["tlsParams"]["certificate"],
318329
zero_mode=CONFIG["registers"]["zeroMode"],

0 commit comments

Comments
 (0)