-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use non privileged tcp port in examples (#109)
The server example had port 502 hard coded. This port is a privileged port. Trying to use it without permissions could result in a failure of the example. Port 502 is still the default, but a nice error message is printed to warn the user about the problem. Also a flag has been introduced to change the port. To keep the example client working with the example server code a similar change has been made to the example client.
- Loading branch information
1 parent
4fd98ba
commit f1128a7
Showing
3 changed files
with
69 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,29 @@ | ||
#!/usr/bin/env python | ||
# scripts/examples/simple_tcp_client.py | ||
import socket | ||
from argparse import ArgumentParser | ||
from socket import create_connection | ||
|
||
from umodbus import conf | ||
from umodbus.client import tcp | ||
|
||
# Enable values to be signed (default is False). | ||
conf.SIGNED_VALUES = True | ||
|
||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
sock.connect(('localhost', 502)) | ||
# Parse command line arguments | ||
parser = ArgumentParser() | ||
parser.add_argument("-a", "--address", default="localhost:502") | ||
|
||
args = parser.parse_args() | ||
if ":" not in args.address: | ||
args.address += ":502" | ||
host, port = args.address.rsplit(":", 1) | ||
port = int(port) | ||
|
||
# Returns a message or Application Data Unit (ADU) specific for doing | ||
# Modbus TCP/IP. | ||
message = tcp.write_multiple_coils(slave_id=1, starting_address=1, values=[1, 0, 1, 1]) | ||
|
||
# Response depends on Modbus function code. This particular returns the | ||
# amount of coils written, in this case it is. | ||
response = tcp.send_message(message, sock) | ||
|
||
sock.close() | ||
with create_connection((host, port)) as sock: | ||
# Response depends on Modbus function code. This particular returns the | ||
# amount of coils written, in this case it is. | ||
response = tcp.send_message(message, sock) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters