-
Notifications
You must be signed in to change notification settings - Fork 951
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
92ba05a
commit b544ef8
Showing
1 changed file
with
67 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#!/usr/bin/env python3 | ||
"""Pymodbus synchronous client example. | ||
An example of a single threaded synchronous client. | ||
usage: simple_sync_client.py | ||
All options must be adapted in the code | ||
The corresponding server must be started before e.g. as: | ||
python3 server_sync.py | ||
""" | ||
|
||
# --------------------------------------------------------------------------- # | ||
# import the various client implementations | ||
# --------------------------------------------------------------------------- # | ||
import pymodbus.client as ModbusClient | ||
from pymodbus import ( | ||
ExceptionResponse, | ||
FramerType, | ||
ModbusException, | ||
pymodbus_apply_logging_config, | ||
) | ||
|
||
|
||
def run_sync_simple_client(): | ||
"""Run sync client.""" | ||
# activate debugging | ||
pymodbus_apply_logging_config("DEBUG") | ||
|
||
print("get client") | ||
client = ModbusClient.ModbusSerialClient( | ||
"socket://127.0.0.1:5020", | ||
framer=FramerType.RTU, | ||
# timeout=10, | ||
# retries=3, | ||
baudrate=9600, | ||
bytesize=8, | ||
parity="N", | ||
stopbits=1, | ||
# handle_local_echo=False, | ||
) | ||
|
||
print("connect to server") | ||
client.connect() | ||
|
||
print("get and verify data") | ||
try: | ||
rr = client.read_device_information(slave=1, read_code=1, object_id=0) | ||
except ModbusException as exc: | ||
print(f"Received ModbusException({exc}) from library") | ||
client.close() | ||
return | ||
if rr.isError(): | ||
print(f"Received Modbus library error({rr})") | ||
client.close() | ||
return | ||
if isinstance(rr, ExceptionResponse): | ||
print(f"Received Modbus library exception ({rr})") | ||
# THIS IS NOT A PYTHON EXCEPTION, but a valid modbus message | ||
client.close() | ||
|
||
print("close connection") | ||
client.close() | ||
|
||
|
||
if __name__ == "__main__": | ||
run_sync_simple_client() |