forked from pymodbus-dev/pymodbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_async_client.py
executable file
·120 lines (107 loc) · 3.35 KB
/
simple_async_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python3
"""Pymodbus asynchronous client example.
An example of a single threaded synchronous client.
usage: simple_client_async.py
All options must be adapted in the code
The corresponding server must be started before e.g. as:
python3 server_sync.py
"""
import asyncio
from pymodbus import Framer, pymodbus_apply_logging_config
from pymodbus.client import (
AsyncModbusSerialClient,
AsyncModbusTcpClient,
AsyncModbusTlsClient,
AsyncModbusUdpClient,
)
from pymodbus.exceptions import ModbusException
from pymodbus.pdu import ExceptionResponse
async def run_async_simple_client(comm, host, port, framer=Framer.SOCKET):
"""Run async client."""
# activate debugging
pymodbus_apply_logging_config("DEBUG")
print("get client")
if comm == "tcp":
client = AsyncModbusTcpClient(
host,
port=port,
framer=framer,
# timeout=10,
# retries=3,
# retry_on_empty=False,
# close_comm_on_error=False,
# strict=True,
# source_address=("localhost", 0),
)
elif comm == "udp":
client = AsyncModbusUdpClient(
host,
port=port,
framer=framer,
# timeout=10,
# retries=3,
# retry_on_empty=False,
# close_comm_on_error=False,
# strict=True,
# source_address=None,
)
elif comm == "serial":
client = AsyncModbusSerialClient(
port,
framer=framer,
# timeout=10,
# retries=3,
# retry_on_empty=False,
# close_comm_on_error=False,
# strict=True,
baudrate=9600,
bytesize=8,
parity="N",
stopbits=1,
# handle_local_echo=False,
)
elif comm == "tls":
client = AsyncModbusTlsClient(
host,
port=port,
framer=Framer.TLS,
# timeout=10,
# retries=3,
# retry_on_empty=False,
# close_comm_on_error=False,
# strict=True,
# sslctx=sslctx,
certfile="../examples/certificates/pymodbus.crt",
keyfile="../examples/certificates/pymodbus.key",
# password="none",
server_hostname="localhost",
)
else: # pragma no cover
print(f"Unknown client {comm} selected")
return
print("connect to server")
await client.connect()
# test client is connected
assert client.connected
print("get and verify data")
try:
# See all calls in client_calls.py
rr = await client.read_coils(1, 1, slave=1)
except ModbusException as exc: # pragma no cover
print(f"Received ModbusException({exc}) from library")
client.close()
return
if rr.isError(): # pragma no cover
print(f"Received Modbus library error({rr})")
client.close()
return
if isinstance(rr, ExceptionResponse): # pragma no cover
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__":
asyncio.run(
run_async_simple_client("tcp", "127.0.0.1", 5020), debug=True
) # pragma: no cover