@@ -36,6 +36,7 @@ Creating a Modbus TCP server is easy:
3636 import logging
3737 from socketserver import TCPServer
3838 from collections import defaultdict
39+ from argparse import ArgumentParser
3940
4041 from umodbus import conf
4142 from umodbus.server.tcp import RequestHandler, get_server
@@ -44,23 +45,37 @@ Creating a Modbus TCP server is easy:
4445 # Add stream handler to logger 'uModbus'.
4546 log_to_stream(level = logging.DEBUG )
4647
47- # A very simple data store which maps addresss against their values.
48+ # A very simple data store which maps addresses against their values.
4849 data_store = defaultdict(int )
4950
5051 # Enable values to be signed (default is False).
5152 conf.SIGNED_VALUES = True
5253
53- TCPServer.allow_reuse_address = True
54- app = get_server(TCPServer, (' localhost' , 502 ), RequestHandler)
54+ # Parse command line arguments
55+ parser = ArgumentParser()
56+ parser.add_argument(" -b" , " --bind" , default = " localhost:502" )
5557
58+ args = parser.parse_args()
59+ if " :" not in args.bind:
60+ args.bind += " :502"
61+ host, port = args.bind.rsplit(" :" , 1 )
62+ port = int (port)
5663
57- @app.route (slave_ids = [1 ], function_codes = [3 , 4 ], addresses = list (range (0 , 10 )))
64+ TCPServer.allow_reuse_address = True
65+ try :
66+ app = get_server(TCPServer, (host, port), RequestHandler)
67+ except PermissionError :
68+ print (" You don't have permission to bind on {} " .format(args.bind))
69+ print (" Hint: try with a different port (ex: --bind localhost:50200)" )
70+ exit (1 )
71+
72+ @app.route (slave_ids = [1 ], function_codes = [1 , 2 ], addresses = list (range (0 , 10 )))
5873 def read_data_store (slave_id , function_code , address ):
5974 """ " Return value of address. """
6075 return data_store[address]
6176
6277
63- @app.route (slave_ids = [1 ], function_codes = [6 , 16 ], addresses = list (range (0 , 10 )))
78+ @app.route (slave_ids = [1 ], function_codes = [5 , 15 ], addresses = list (range (0 , 10 )))
6479 def write_data_store (slave_id , function_code , address , value ):
6580 """ " Set value for address. """
6681 data_store[address] = value
@@ -82,26 +97,34 @@ Doing a Modbus request requires even less code:
8297
8398 # !/usr/bin/env python
8499 # scripts/examples/simple_tcp_client.py
85- import socket
100+ from argparse import ArgumentParser
101+ from socket import create_connection
86102
87103 from umodbus import conf
88104 from umodbus.client import tcp
89105
90106 # Enable values to be signed (default is False).
91107 conf.SIGNED_VALUES = True
92108
93- sock = socket.socket(socket.AF_INET , socket.SOCK_STREAM )
94- sock.connect((' localhost' , 502 ))
109+ # Parse command line arguments
110+ parser = ArgumentParser()
111+ parser.add_argument(" -a" , " --address" , default = " localhost:502" )
112+
113+ args = parser.parse_args()
114+ if " :" not in args.address:
115+ args.address += " :502"
116+ host, port = args.address.rsplit(" :" , 1 )
117+ port = int (port)
95118
96119 # Returns a message or Application Data Unit (ADU) specific for doing
97120 # Modbus TCP/IP.
98121 message = tcp.write_multiple_coils(slave_id = 1 , starting_address = 1 , values = [1 , 0 , 1 , 1 ])
99122
100- # Response depends on Modbus function code. This particular returns the
101- # amount of coils written, in this case it is.
102- response = tcp.send_message(message, sock)
123+ with create_connection((host, port)) as sock:
124+ # Response depends on Modbus function code. This particular returns the
125+ # amount of coils written, in this case it is.
126+ response = tcp.send_message(message, sock)
103127
104- sock.close()
105128
106129 Features
107130--------
0 commit comments