Skip to content

Commit

Permalink
TO BE REMOVED.
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen committed Nov 6, 2024
1 parent 92ba05a commit b544ef8
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions examples/diag_test.py
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()

0 comments on commit b544ef8

Please sign in to comment.