Skip to content

XBee Serial API

API for the XBee serial library

__init__(port=None, baudrate=115200, status=False, logger=None)

Configure the serial port


Parameters
  • port (str or None) - Port of serial device.
  • baudrate (int) - Baudrate of serial device.
  • status (bool) - Automatically receive status packets after a transmission.
  • logger (Logger) - Logger object from Logger.Logger used to record data such as sent and received data.

See Serial Port for details on finding the correct serial port name.

baudrate should be the same for both device (XBee RF module) and serial port. XBees will be configured to 115200 by default.

See Frame Details for details regarding the XBee status packet (Frame type 0x89).

A Logger instance will be created if it is not provided. You should only create your own instance of Logger if you want to log data that is not already logged by the XBee library.

Example:

from Communication.XBee.XBee import XBee
PORT = "/dev/cu.usbserial-D30DWZKT"
BAUD_RATE = 115200
xbee = XBee(PORT, BAUD_RATE) # status and logger will be set to False and None respectively
open()

Open a connection over the serial port. This method does not return anything if a port is successfully opened.


ReturnsTrue if success, False if failure (There is already an open port)
Raises:SerialException if there is an error opening the serial port.

A SerialException typically occurs if:

  • The XBee module is not plugged in
  • The port/device name is incorrect
  • The port is in use (e.g. There is another program accessing the same port )
close()

Close a connection over the serial port.


ReturnsTrue if success, False if failure.
Return typebool

transmit_data(data, address="0000000000000000")

Send data to another XBee module(s)


Parameters
  • data (str) - String data to transmit.
  • address (str) - Address of destination XBee module. "0000000000000000" if no value is provided.
ReturnsStatus of transmit request. See 0x89 Tx (Transmit) Status for more details.
Return typex89 or None
RaisesSerialException if serial port is not open

data can be at most 100 bytes (100 characters)

address can be set to 000000000000FFFF in order to broadcast a message

0x89: (frame_type, frame_id, status)

Returns None if no status frame is received


retrieve_data()

Check for incomming data


Returnsstr if there is incomming data. None otherwise.
Return type0x81 or None

0x81: (frame_type, source_address, rssi, options, data)


request_at_command_data(self, id)

Request and retrieve configuration detail of XBee device.

Parametersid (str) - Identifier of AT command
ReturnsAT command response. See 0x88 AT Command Response for more details.
Return type0x88
RaisesSerialException if serial port is not open
read_config(self, filename)

This method reads a config file and executes AT commands to retrieve configuration data of an XBee module. All returned data is written to a log file.

Parametersfilename (str) - Filename of AT Commands to execute.
RaisesSerialException if serial port is not open