@@ -231,19 +231,18 @@ class ReconnectingAsyncioModbusTcpClient:
231231 #: Maximum delay in milli seconds before reconnect is attempted.
232232 DELAY_MAX_MS = 1000 * 60 * 5
233233
234- def __init__ (self , protocol_class = None , loop = None , framer = None , ** kwargs ):
234+ def __init__ (self , protocol_class = None , framer = None , ** kwargs ):
235235 """Initialize ReconnectingAsyncioModbusTcpClient.
236236
237237 :param protocol_class: Protocol used to talk to modbus device.
238- :param loop: Event loop to use
239238 """
239+ # If there are no loop running a runtime error will be raised
240+ self .loop = asyncio .get_running_loop ()
240241 #: Protocol used to talk to modbus device.
241242 self .protocol_class = protocol_class or ModbusClientProtocol
242243 #: Current protocol instance.
243244 self .protocol = None
244245 self .framer = framer if framer else ModbusSocketFramer
245- #: Event loop to use.
246- self .loop = loop or asyncio .get_event_loop ()
247246 self .host = None
248247 self .port = 0
249248 self .connected = False
@@ -341,21 +340,20 @@ async def _reconnect(self):
341340class AsyncioModbusTcpClient :
342341 """Client to connect to modbus device over TCP/IP."""
343342
344- def __init__ (self , host = None , port = 502 , protocol_class = None , loop = None , framer = None , ** kwargs ):
343+ def __init__ (self , host = None , port = 502 , protocol_class = None , framer = None , ** kwargs ):
345344 """Initialize Asyncio Modbus Tcp Client
346345
347346 :param host: Host IP address
348347 :param port: Port to connect
349348 :param protocol_class: Protocol used to talk to modbus device.
350- :param loop: Asyncio Event loop
351349 """
350+ # If there are no loop running a runtime error will be raised
351+ self .loop = asyncio .get_running_loop ()
352352 #: Protocol used to talk to modbus device.
353353 self .protocol_class = protocol_class or ModbusClientProtocol
354354 #: Current protocol instance.
355355 self .protocol = None
356356 self .framer = framer if framer else ModbusSocketFramer
357- #: Event loop to use.
358- self .loop = loop or asyncio .get_event_loop ()
359357
360358 self .host = host
361359 self .port = port
@@ -419,17 +417,18 @@ def protocol_lost_connection(self, protocol):
419417class ReconnectingAsyncioModbusTlsClient (ReconnectingAsyncioModbusTcpClient ):
420418 """Client to connect to modbus device repeatedly over TLS."""
421419
422- def __init__ (self , protocol_class = None , loop = None , framer = None , ** kwargs ):
420+ def __init__ (self , protocol_class = None , framer = None , ** kwargs ):
423421 """Initialize ReconnectingAsyncioModbusTcpClient
424422
425423 :param protocol_class: Protocol used to talk to modbus device.
426- :param loop: Event loop to use
427424 """
425+ # If there are no loop running a runtime error will be raised
426+ self .loop = asyncio .get_running_loop ()
428427 self .framer = framer if framer else ModbusTlsFramer
429428 self .server_hostname = None
430429 self .sslctx = None
431430 ReconnectingAsyncioModbusTcpClient .__init__ (
432- self , protocol_class , loop , framer = self .framer , ** kwargs
431+ self , protocol_class , framer = self .framer , ** kwargs
433432 )
434433
435434 async def start (self , host , port = 802 , sslctx = None , server_hostname = None ):
@@ -489,19 +488,18 @@ class ReconnectingAsyncioModbusUdpClient:
489488 #: Maximum delay in milli seconds before reconnect is attempted.
490489 DELAY_MAX_MS = 1000 * 60 * 5
491490
492- def __init__ (self , protocol_class = None , loop = None , framer = None , ** kwargs ):
491+ def __init__ (self , protocol_class = None , framer = None , ** kwargs ):
493492 """Initialize ReconnectingAsyncioModbusUdpClient
494493
495494 :param protocol_class: Protocol used to talk to modbus device.
496- :param loop: Asyncio Event loop
497495 """
496+ # If there are no loop running a runtime error will be raised
497+ self .loop = asyncio .get_running_loop ()
498498 #: Protocol used to talk to modbus device.
499499 self .protocol_class = protocol_class or ModbusUdpClientProtocol
500500 #: Current protocol instance.
501501 self .protocol = None
502502 self .framer = framer if framer else ModbusSocketFramer
503- #: Event loop to use.
504- self .loop = loop or asyncio .get_event_loop ()
505503
506504 self .host = None
507505 self .port = 0
@@ -607,25 +605,22 @@ async def _reconnect(self):
607605class AsyncioModbusUdpClient :
608606 """Client to connect to modbus device over UDP."""
609607
610- def __init__ (self , host = None , port = 502 , protocol_class = None , loop = None , framer = None , ** kwargs ):
608+ def __init__ (self , host = None , port = 502 , protocol_class = None , framer = None , ** kwargs ):
611609 """Initialize Asyncio Modbus UDP Client.
612610
613611 :param host: Host IP address
614612 :param port: Port to connect
615613 :param protocol_class: Protocol used to talk to modbus device.
616- :param loop: Asyncio Event loop
617614 """
615+ # If there are no loop running a runtime error will be raised
616+ self .loop = asyncio .get_running_loop ()
618617 #: Protocol used to talk to modbus device.
619618 self .protocol_class = protocol_class or ModbusUdpClientProtocol
620619 #: Current protocol instance.
621620 self .protocol = None
622621 self .framer = framer if framer else ModbusSocketFramer
623- #: Event loop to use.
624- self .loop = loop or asyncio .get_event_loop ()
625-
626622 self .host = host
627623 self .port = port
628-
629624 self .connected = False
630625 self ._proto_args = kwargs
631626
@@ -704,7 +699,6 @@ def __init__(
704699 port ,
705700 protocol_class = None ,
706701 framer = None ,
707- loop = None ,
708702 baudrate = 9600 ,
709703 bytesize = 8 ,
710704 parity = "N" ,
@@ -716,14 +710,13 @@ def __init__(
716710 :param port: Port to connect
717711 :param protocol_class: Protocol used to talk to modbus device.
718712 :param framer: Framer to use
719- :param loop: Asyncio Event loop
720713 """
714+ # If there are no loop running a runtime error will be raised
715+ self .loop = asyncio .get_running_loop ()
721716 #: Protocol used to talk to modbus device.
722717 self .protocol_class = protocol_class or ModbusRtuFramer
723718 #: Current protocol instance.
724719 self .protocol = None
725- #: Event loop to use.
726- self .loop = loop or asyncio .get_event_loop ()
727720 self .port = port
728721 self .baudrate = baudrate
729722 self .bytesize = bytesize
@@ -797,26 +790,24 @@ def protocol_lost_connection(self, protocol):
797790 _logger .error (TEST_FACTORY )
798791
799792
800- async def init_tcp_client (proto_cls , loop , host , port , ** kwargs ):
793+ async def init_tcp_client (proto_cls , host , port , ** kwargs ):
801794 """Initialize tcp client with helper function.
802795
803796 :param proto_cls:
804- :param loop:
805797 :param host:
806798 :param port:
807799 :param kwargs:
808800 :return:
809801 """
810802 client = ReconnectingAsyncioModbusTcpClient (
811- protocol_class = proto_cls , loop = loop , ** kwargs
803+ protocol_class = proto_cls , ** kwargs
812804 )
813805 await client .start (host , port )
814806 return client
815807
816808
817809async def init_tls_client (
818810 proto_cls ,
819- loop ,
820811 host ,
821812 port ,
822813 sslctx = None ,
@@ -827,7 +818,6 @@ async def init_tls_client(
827818 """Initialize tcp client with Helper function.
828819
829820 :param proto_cls:
830- :param loop:
831821 :param host:
832822 :param port:
833823 :param sslctx:
@@ -837,24 +827,23 @@ async def init_tls_client(
837827 :return:
838828 """
839829 client = ReconnectingAsyncioModbusTlsClient (
840- protocol_class = proto_cls , loop = loop , framer = framer , ** kwargs
830+ protocol_class = proto_cls , framer = framer , ** kwargs
841831 )
842832 await client .start (host , port , sslctx , server_hostname )
843833 return client
844834
845835
846- async def init_udp_client (proto_cls , loop , host , port , ** kwargs ):
836+ async def init_udp_client (proto_cls , host , port , ** kwargs ):
847837 """Initialize UDP client with helper function.
848838
849839 :param proto_cls:
850- :param loop:
851840 :param host:
852841 :param port:
853842 :param kwargs:
854843 :return:
855844 """
856845 client = ReconnectingAsyncioModbusUdpClient (
857- protocol_class = proto_cls , loop = loop , ** kwargs
846+ protocol_class = proto_cls , ** kwargs
858847 )
859848 await client .start (host , port )
860849 return client
0 commit comments