77 python3 server_sync.py
88"""
99import asyncio
10+ import logging
1011
11- from examples .client_async import _logger , run_client , setup_client
12+ from examples .client_async import run_async_client , setup_async_client
1213
1314
1415SLAVE = 0x01
1516
1617
17- async def handle_coils (client ):
18+ async def _handle_coils (client ):
1819 """Read/Write coils."""
1920 _logger .info ("### Reading Coil" )
2021 rr = await client .read_coils (1 , 1 , slave = SLAVE )
@@ -58,7 +59,7 @@ async def handle_coils(client):
5859 _logger .debug (txt )
5960
6061
61- async def handle_discrete_input (client ):
62+ async def _handle_discrete_input (client ):
6263 """Read discrete inputs."""
6364 _logger .info ("### Reading discrete input, Read address:0-7" )
6465 rr = await client .read_discrete_inputs (0 , 8 , slave = SLAVE )
@@ -67,7 +68,7 @@ async def handle_discrete_input(client):
6768 _logger .debug (txt )
6869
6970
70- async def handle_holding_registers (client ):
71+ async def _handle_holding_registers (client ):
7172 """Read/write holding registers."""
7273 _logger .info ("### write holding register and read holding registers" )
7374 rq = await client .write_register (1 , 10 , slave = SLAVE )
@@ -101,7 +102,7 @@ async def handle_holding_registers(client):
101102 _logger .debug (txt )
102103
103104
104- async def handle_input_registers (client ):
105+ async def _handle_input_registers (client ):
105106 """Read input registers."""
106107 _logger .info ("### read input registers" )
107108 rr = await client .read_input_registers (1 , 8 , slave = SLAVE )
@@ -110,14 +111,21 @@ async def handle_input_registers(client):
110111 _logger .debug (txt )
111112
112113
113- async def demonstrate_calls (client ):
114+ async def run_async_basic_calls (client ):
114115 """Demonstrate basic read/write calls."""
115- await handle_coils (client )
116- await handle_discrete_input (client )
117- await handle_holding_registers (client )
118- await handle_input_registers (client )
116+ await _handle_coils (client )
117+ await _handle_discrete_input (client )
118+ await _handle_holding_registers (client )
119+ await _handle_input_registers (client )
120+
121+ # --------------------------------------------------------------------------- #
122+ # Extra code, to allow commandline parameters instead of changing the code
123+ # --------------------------------------------------------------------------- #
124+ FORMAT = "%(asctime)-15s %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s"
125+ logging .basicConfig (format = FORMAT )
126+ _logger = logging .getLogger ()
119127
120128
121129if __name__ == "__main__" :
122- testclient = setup_client ()
123- asyncio .run (run_client (testclient , modbus_calls = demonstrate_calls ))
130+ testclient = setup_async_client ()
131+ asyncio .run (run_async_client (testclient , modbus_calls = run_async_basic_calls ))
0 commit comments