From 0b03fcb84f28da678a7ca30ced416c0b9d04dc26 Mon Sep 17 00:00:00 2001 From: TonyB Date: Wed, 1 Apr 2020 04:23:38 -0500 Subject: [PATCH] Optionally specify operating_mode during open. digidotcom/xbee-python#162 --- digi/xbee/devices.py | 9 +++++++-- digi/xbee/profile.py | 8 +++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/digi/xbee/devices.py b/digi/xbee/devices.py index 3981858..891d1e5 100755 --- a/digi/xbee/devices.py +++ b/digi/xbee/devices.py @@ -2231,7 +2231,8 @@ def create_xbee_device(cls, comm_port_data): flow_control=comm_port_data["flowControl"], _sync_ops_timeout=comm_port_data["timeout"]) - def open(self, force_settings=False): + def open(self, force_settings=False, initial_operating_mode=OperatingMode.API_MODE): + """ Opens the communication with the XBee device and loads some information about it. @@ -2239,6 +2240,10 @@ def open(self, force_settings=False): force_settings (Boolean, optional): ``True`` to open the device ensuring/forcing that the specified serial settings are applied even if the current configuration is different, ``False`` to open the device with the current configuration. Default to False. + initial_operating_mode (OperatingMode, optional): If the operating mode is known ahead of time (e.g. + when re-opening for serial setting updates) it can be explicitly specified. Useful if + operating in ESCAPED_API_MODE when the next frame may contain escaped characters. Default to + API_MODE. Raises: TimeoutException: if there is any problem with the communication. @@ -2311,7 +2316,7 @@ def open(self, force_settings=False): self._packet_listener.add_route_record_received_callback(route_record_cbs) self._packet_listener.add_route_info_received_callback(route_info_cbs) - self._operating_mode = OperatingMode.API_MODE + self._operating_mode = initial_operating_mode self._packet_listener.start() self._packet_listener.wait_until_started() diff --git a/digi/xbee/profile.py b/digi/xbee/profile.py index 85b813f..b29991d 100644 --- a/digi/xbee/profile.py +++ b/digi/xbee/profile.py @@ -28,6 +28,7 @@ from digi.xbee.filesystem import LocalXBeeFileSystemManager, FileSystemException, FileSystemNotSupportedException from digi.xbee.models.atcomm import ATStringCommand from digi.xbee.models.hw import HardwareVersion +from digi.xbee.models.mode import OperatingMode from digi.xbee.models.protocol import XBeeProtocol from digi.xbee.util import utils from enum import Enum, unique @@ -1250,7 +1251,12 @@ def _check_port_settings_changed(self): parity_changed = False stop_bits_changed = False cts_flow_control_changed = False + operating_mode = OperatingMode.API_MODE for setting in self._xbee_profile.profile_settings: + if setting.name.upper() == ATStringCommand.AP.command: + new_operating_mode = OperatingMode.get(int(setting.value)) + if new_operating_mode != OperatingMode.UNKNOWN: + operating_mode = new_operating_mode if setting.name.upper() in _PARAMETERS_SERIAL_PORT: if setting.name.upper() == ATStringCommand.BD.command: baudrate_changed = True @@ -1291,7 +1297,7 @@ def _check_port_settings_changed(self): try: self._xbee_device.close() # This is necessary to stop the frames read thread. self._xbee_device.serial_port.apply_settings(port_parameters) - self._xbee_device.open() + self._xbee_device.open(operating_mode=operating_mode) except (XBeeException, SerialException) as e: raise UpdateProfileException(_ERROR_UPDATE_SERIAL_PORT % str(e))