|
7 | 7 |
|
8 | 8 | Works out of the box together with payload_server.py |
9 | 9 | """ |
| 10 | +import asyncio |
10 | 11 | import logging |
11 | 12 | from collections import OrderedDict |
12 | 13 |
|
13 | | -from pymodbus.client import ModbusTcpClient as ModbusClient |
| 14 | +from pymodbus import pymodbus_apply_logging_config |
| 15 | +from pymodbus.client import AsyncModbusTcpClient |
14 | 16 | from pymodbus.constants import Endian |
15 | 17 | from pymodbus.payload import BinaryPayloadBuilder, BinaryPayloadDecoder |
16 | 18 |
|
17 | 19 |
|
18 | | -# --------------------------------------------------------------------------- # |
19 | | -# configure the client logging |
20 | | -# --------------------------------------------------------------------------- # |
21 | | - |
22 | | -FORMAT = ( |
23 | | - "%(asctime)-15s %(threadName)-15s" |
24 | | - " %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s" |
25 | | -) |
26 | | -logging.basicConfig(format=FORMAT) |
27 | | -log = logging.getLogger() |
28 | | -log.setLevel(logging.INFO) |
29 | | - |
| 20 | +_logger = logging.getLogger() |
30 | 21 | ORDER_DICT = {"<": "LITTLE", ">": "BIG"} |
31 | 22 |
|
32 | 23 |
|
33 | | -def run_binary_payload_client(): |
| 24 | +async def run_binary_payload_client(port): |
34 | 25 | """Run binary payload.""" |
| 26 | + pymodbus_apply_logging_config() |
| 27 | + _logger.setLevel(logging.DEBUG) |
| 28 | + |
35 | 29 | # ----------------------------------------------------------------------- # |
36 | 30 | # We are going to use a simple sync client to send our requests |
37 | 31 | # ----------------------------------------------------------------------- # |
38 | | - client = ModbusClient("127.0.0.1", port=5020) |
39 | | - client.connect() |
| 32 | + client = AsyncModbusTcpClient("127.0.0.1", port=port) |
| 33 | + await client.connect() |
40 | 34 |
|
41 | 35 | # ----------------------------------------------------------------------- # |
42 | 36 | # If you need to build a complex message to send, you can use the payload |
@@ -97,23 +91,26 @@ def run_binary_payload_client(): |
97 | 91 | print("\n") |
98 | 92 | payload = builder.build() |
99 | 93 | address = 0 |
| 94 | + slave = 1 |
100 | 95 | # We can write registers |
101 | | - client.write_registers(address, registers, unit=1) |
| 96 | + rr = await client.write_registers(address, registers, slave=slave) |
| 97 | + assert not rr.isError() |
102 | 98 | # Or we can write an encoded binary string |
103 | | - client.write_registers(address, payload, skip_encode=True, unit=1) |
| 99 | + rr = await client.write_registers(address, payload, skip_encode=True, slave=1) |
| 100 | + assert not rr.isError() |
104 | 101 |
|
105 | 102 | # ----------------------------------------------------------------------- # |
106 | 103 | # If you need to decode a collection of registers in a weird layout, the |
107 | 104 | # payload decoder can help you as well. |
108 | 105 | # ----------------------------------------------------------------------- # |
109 | 106 | print("Reading Registers:") |
110 | | - address = 0x0 |
111 | 107 | count = len(payload) |
112 | | - result = client.read_holding_registers(address, count, slave=1) |
113 | | - print(result.registers) |
| 108 | + rr = await client.read_holding_registers(address, count, slave=slave) |
| 109 | + assert not rr.isError() |
| 110 | + print(rr.registers) |
114 | 111 | print("\n") |
115 | 112 | decoder = BinaryPayloadDecoder.fromRegisters( |
116 | | - result.registers, byteorder=byte_endian, wordorder=word_endian |
| 113 | + rr.registers, byteorder=byte_endian, wordorder=word_endian |
117 | 114 | ) |
118 | 115 | # Make sure word/byte order is consistent between BinaryPayloadBuilder and BinaryPayloadDecoder |
119 | 116 | assert ( |
@@ -146,17 +143,14 @@ def run_binary_payload_client(): |
146 | 143 | ) |
147 | 144 | print("Decoded Data") |
148 | 145 | for name, value in iter(decoded.items()): |
149 | | - print( |
150 | | - "%s\t" % name, # pylint: disable=consider-using-f-string |
151 | | - hex(value) if isinstance(value, int) else value, |
152 | | - ) |
| 146 | + print(f"{name}\t{hex(value) if isinstance(value, int) else value}") |
153 | 147 | print("\n") |
154 | 148 |
|
155 | 149 | # ----------------------------------------------------------------------- # |
156 | 150 | # close the client |
157 | 151 | # ----------------------------------------------------------------------- # |
158 | | - client.close() |
| 152 | + await client.close() |
159 | 153 |
|
160 | 154 |
|
161 | 155 | if __name__ == "__main__": |
162 | | - run_binary_payload_client() |
| 156 | + asyncio.run(run_binary_payload_client(5020)) |
0 commit comments