2222"""
2323import argparse
2424import asyncio
25- from threading import Thread
2625import logging
2726
2827
4241)
4342
4443
45- async def setup_async_client (loop ):
44+ async def setup_async_client ():
4645 """Run client setup."""
4746 args = get_commandline ()
4847 _logger .info ("### Create client object" )
@@ -57,7 +56,6 @@ async def setup_async_client(loop):
5756 retry_on_empty = False , # Is an empty response a retry
5857 source_address = ("localhost" , 0 ), # bind socket to address
5958 strict = True , # use strict timing, t1.5 for Modbus RTU
60- loop = loop ,
6159 )
6260 elif args .comm == "udp" :
6361 client = await AsyncModbusUDPClient (
@@ -69,7 +67,6 @@ async def setup_async_client(loop):
6967 retry_on_empty = False , # Is an empty response a retry
7068 source_address = ("localhost" , 0 ), # bind socket to address
7169 strict = True , # use strict timing, t1.5 for Modbus RTU
72- loop = loop ,
7370 )
7471 elif args .comm == "serial" :
7572 client = await AsyncModbusSerialClient (
@@ -82,7 +79,6 @@ async def setup_async_client(loop):
8279 handle_local_echo = False , # Handle local echo of the USB-to-RS485 adaptor
8380 timeout = 1 , # waiting time for request to complete
8481 strict = True , # use strict timing, t1.5 for Modbus RTU
85- loop = loop ,
8682 )
8783 elif args .comm == "tls" :
8884 client = await AsyncModbusTLSClient (
@@ -98,44 +94,16 @@ async def setup_async_client(loop):
9894 retry_on_empty = False , # Is an empty response a retry
9995 source_address = ("localhost" , 0 ), # bind socket to address
10096 strict = True , # use strict timing, t1.5 for Modbus RTU
101- loop = loop ,
10297 )
10398 return client
10499
105100
106101async def run_async_client (modbus_calls = None ):
107102 """Run sync client."""
108103 _logger .info ("### Client ready" )
109-
110- def done (future ): # pylint: disable=unused-argument
111- """Done."""
112- _logger .info ("Done !!!" )
113-
114- def start_loop (loop ):
115- """Start Loop"""
116- asyncio .set_event_loop (loop )
117- loop .run_forever ()
118-
119- loop = asyncio .new_event_loop ()
120- mythread = Thread (target = start_loop , args = [loop ])
121- mythread .daemon = True
122- # Start the loop
123- mythread .start ()
124- await asyncio .sleep (1 )
125- assert loop .is_running () # nosec
126- asyncio .set_event_loop (loop )
127-
128- client = await setup_async_client (loop )
129-
130- # Run supplied modbus calls
104+ client = await setup_async_client ()
131105 if modbus_calls :
132- future = asyncio .run_coroutine_threadsafe (
133- modbus_calls (client .protocol ), loop = loop
134- )
135- future .add_done_callback (done )
136- while not future .done ():
137- await asyncio .sleep (0.1 )
138- loop .stop ()
106+ await modbus_calls (client .protocol )
139107 _logger .info ("### End of Program" )
140108
141109
0 commit comments