Git Product home page Git Product logo

Comments (3)

tatianaleon avatar tatianaleon commented on August 20, 2024

Hi @KyleIm,

To change the operating mode of your XBee you can use XCTU for example. Using the library you can only use API modes, that is AP=1 or AP=2, since transparent mode (AP=0) is not supported. You can use:

from digi.xbee.models.atcomm import ATStringCommand
from digi.xbee.models.mode import OperatingMode

[...]

# Set operating mode to 1 (API without escapes)
xbee0.set_parameter(ATStringCommand.AP, bytearray([OperatingMode.API_MODE.code]))

# Set operating mode to 2 (API with escapes)
xbee0.set_parameter(ATStringCommand.AP, bytearray([OperatingMode.ESCAPED_API_MODE.code]))

[...]

The method open(force_settings=True) of an XBeeDevice object forces the XBee configuration to the one provided when you instances it. In your case to 9600 bauds, 8 data bits, no parity, 1 stop bit, and no flow control. It also forces the operating mode of the XBee to AP=1 if it is not configured to use an API mode (that is, if AP!=1 and AP!=2)
See Open the XBee connection.

The reset() method does not restore the configuration of your XBee. It performs a software reset of the module, it executes the command FR. See Reset the device.

To restore default values for your module you must execute RE command (see RE (Restore Defaults)). After restoring:

  1. To permanently store changes on the XBee you must write them (write_changes())
  2. Reset the module so it use the established default configuration (reset())
  3. Then close the connection (close())

If after this, you need to continue communicating with the XBee, you must open again the connection. This means, after using open(force_settings=True), some parameters will be modified to use the communication parameters you set on the object instance and the AP setting value to be 1, since the default value, 0 (transparent mode) is not supported by the library (see Open the XBee connection)

For example:

import sys

from digi.xbee.models.atcomm import ATStringCommand
from digi.xbee.devices import XBeeDevice
from digi.xbee.exception import XBeeException

DEFAULT_NODE_ID = " "
NODE_ID = "xbee0"


def restore_defaults_and_close(xbee):
    try:
        xbee.execute_command(ATStringCommand.RE, apply=False)
        xbee.write_changes()
        print("XBee default configuration restored!")
    except XBeeException as exc:
        print("Error restoring XBee default configuration: %s" % str(exc))
        sys.exit(1)

    try:
        xbee.reset()
    except XBeeException as exc:
        # This is expected if the previous XBee connection parameters
        # are different to the default ones
        print("   [Expected] Error resetting the XBee: %s" % str(exc))
    finally:
        xbee.close()


def open_xbee(xbee):
    try:
        xbee.open(force_settings=True)
    except XBeeException as exc:
        print("Unable to establish communication with XBee: %s" % str(exc))
        sys.exit(1)


def run():
    xbee0 = XBeeDevice("/dev/ttyUSB5", 115200)
    open_xbee(xbee0)

    # Set node ID to a non default value, i.e., xbee0
    try:
        xbee0.set_node_id(NODE_ID)
        xbee0.write_changes()
        xbee0.apply_changes()
        # Read the node ID to test the value
        node_id = xbee0.get_node_id()
        print("\nNode id BEFORE restoring settings: '%s'\n" % node_id)
        if node_id != NODE_ID:
            print("ERROR: Expected value '%s', retrieved '%s'" % (NODE_ID, node_id))
            sys.exit(1)
    except XBeeException as exc:
        print("Unable to set XBee node ID: %s" % str(exc))
        sys.exit(1)

    # Restore defaults and close the connection
    restore_defaults_and_close(xbee0)

    # Here you can reopen the device, but some configuration parameters will
    # be changed. For example:
    #    * the baudrate to be 115200 (since 'xbee0' was instantiated to use
    #      115200 instead the defaults 9600)
    #    * or the operating mode (AP), since the default mode, transparent
    #      (AP=0) is not supported by the library. The XBee will be reconfigured
    #      to use API without escapes (AP=1).
    print("Re-opening XBee")
    open_xbee(xbee0)

    # Read the node ID, it should be a white space (the default value)
    node_id = xbee0.get_node_id()
    print("\nNode id AFTER restoring settings: '%s'\n" % node_id)
    if node_id != DEFAULT_NODE_ID:
        print("ERROR: Expected value '%s', retrieved '%s'" % (DEFAULT_NODE_ID, node_id))
        sys.exit(1)

    sys.exit(0)


if __name__ == '__main__':
    run()

The output is:

Node id BEFORE restoring settings: 'xbee0'

XBee default configuration restored!
   [Expected] Error resetting the XBee: Timeout waiting for the modem status packet.
Re-opening XBee

Node id AFTER restoring settings: ' '

Hope this helps.

from xbee-python.

KyleIm avatar KyleIm commented on August 20, 2024

Thanks I solved it. However, xbee0.set_parameter(ATStringCommand.AP, bytearray([OperatingMode.API_MODE.code])) should be xbee0.set_parameter(('AP'), bytearray([OperatingMode.API_MODE.code]))

When I copied it, it showed TypeError: object of type 'ATStringCommand' has no len().

from xbee-python.

tatianaleon avatar tatianaleon commented on August 20, 2024

Great @KyleIm!

Using ATStringCommand.AP in get_parameter(), set_parameter(), and execute_parameter() methods should work since version 1.4.0 of the library.

Best Regards.

from xbee-python.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.