Skip to content
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

Optionally specify operating_mode during open. #163

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions digi/xbee/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -2231,14 +2231,19 @@ 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.

Args:
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.
Expand Down Expand Up @@ -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()

Expand Down
8 changes: 7 additions & 1 deletion digi/xbee/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))

Expand Down