-
Notifications
You must be signed in to change notification settings - Fork 1k
Fix AsyncioModbusSerialClient (serial options and create_serial_connection usage) #268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8ddd937
8756520
8864dfd
c8ad91f
25fad28
960bc0f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -659,7 +659,8 @@ class AsyncioModbusSerialClient(object): | |
| transport = None | ||
| framer = None | ||
|
|
||
| def __init__(self, port, protocol_class=None, framer=None, loop=None): | ||
| def __init__(self, port, protocol_class=None, framer=None, loop=None, | ||
| baudrate=9600, bytesize=8, parity='N', stopbits=1): | ||
| """ | ||
| Initializes Asyncio Modbus Serial Client | ||
| :param port: Port to connect | ||
|
|
@@ -674,30 +675,31 @@ def __init__(self, port, protocol_class=None, framer=None, loop=None): | |
| #: Event loop to use. | ||
| self.loop = loop or asyncio.get_event_loop() | ||
| self.port = port | ||
| self.baudrate = baudrate | ||
| self.bytesize = bytesize | ||
| self.parity = parity | ||
| self.stopbits = stopbits | ||
| self.framer = framer | ||
| self._connected = False | ||
| self._connected_event = asyncio.Event() | ||
|
|
||
| def stop(self): | ||
| """ | ||
| Stops connection | ||
| :return: | ||
| """ | ||
| if self._connected: | ||
| if self._connected.is_set(): | ||
| if self.protocol: | ||
| if self.protocol.transport: | ||
| self.protocol.transport.close() | ||
|
|
||
| def _create_protocol(self): | ||
| """ | ||
| Factory function to create initialized protocol instance. | ||
| """ | ||
| from serial_asyncio import create_serial_connection | ||
|
|
||
| def factory(): | ||
| return self.protocol_class(framer=self.framer) | ||
| protocol = self.protocol_class(framer=self.framer) | ||
| protocol.factory = self | ||
| return protocol | ||
|
|
||
| cor = create_serial_connection(self.loop, factory, self.port) | ||
| return cor | ||
| @property | ||
| def _connected(self): | ||
| return self._connected_event.is_set() | ||
|
|
||
| @asyncio.coroutine | ||
| def connect(self): | ||
|
|
@@ -707,19 +709,24 @@ def connect(self): | |
| """ | ||
| _logger.debug('Connecting.') | ||
| try: | ||
| yield from self.loop.create_connection(self._create_protocol) | ||
| _logger.info('Connected to %s:%s.' % (self.host, self.port)) | ||
| from serial_asyncio import create_serial_connection | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. Please move this change to serial factory. https://github.com/cbergmiller/pymodbus/blob/fix/serial_async_client/pymodbus/client/async/factory/serial.py#L82.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the review. I updated the pull request. |
||
|
|
||
| yield from create_serial_connection( | ||
| self.loop, self._create_protocol, self.port, baudrate=self.baudrate, | ||
| bytesize=self.bytesize, stopbits=self.stopbits | ||
| ) | ||
| yield from self._connected_event.wait() | ||
| _logger.info('Connected to %s', self.port) | ||
| except Exception as ex: | ||
| _logger.warning('Failed to connect: %s' % ex) | ||
| # asyncio.async(self._reconnect(), loop=self.loop) | ||
| _logger.warning('Failed to connect: %s', ex) | ||
|
|
||
| def protocol_made_connection(self, protocol): | ||
| """ | ||
| Protocol notification of successful connection. | ||
| """ | ||
| _logger.info('Protocol made connection.') | ||
| if not self._connected: | ||
| self._connected = True | ||
| self._connected_event.set() | ||
| self.protocol = protocol | ||
| else: | ||
| _logger.error('Factory protocol connect ' | ||
|
|
@@ -735,7 +742,7 @@ def protocol_lost_connection(self, protocol): | |
| _logger.error('Factory protocol callback called' | ||
| ' from unexpected protocol instance.') | ||
|
|
||
| self._connected = False | ||
| self._connected_event.clear() | ||
| self.protocol = None | ||
| # if self.host: | ||
| # asyncio.async(self._reconnect(), loop=self.loop) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be
if self._connected:as you are returningself._connected_event.is_set()from the property_connected?