Skip to content

XBee Serial API Usage Guide

This guide provides examples of how to use the XBee API for GCS and vehicle communication.


This example shows how to open an XBee connection before sending or receiving data.

  • XBee.__init__(port, baudrate, status, logger)
  • XBee.open()
ParameterTypeDescription
portstrSerial port name (e.g., /dev/cu.usbserial-D30DWZKY)
baudrateintXBee baud rate (default: 115200)
statusboolEnable automatic status reception (default: False)
loggerLoggerLogger instance for debugging
  • True if the connection opens successfully.
  • False if it fails.
from Communication.XBee import XBee
from Logger.Logger import Logger
# Configuration
PORT = "/dev/cu.usbserial-D30DWZKY"
BAUD_RATE = 115200
LOGGER = Logger()
# Initialize XBee
xbee = XBee(port=PORT, baudrate=BAUD_RATE, logger=LOGGER)
# Open XBee connection
if xbee.open():
print("[INFO] XBee connection opened successfully.")
else:
print("[ERROR] Failed to open XBee connection.")

🔼 Back to Table of Contents


This example demonstrates how to properly close the XBee serial connection when it is no longer needed.

  • XBee.close()
  • True if the serial port is successfully closed.
  • False if the port is already closed or an error occurs.
from Communication.XBee import XBee
# Configuration
PORT = "/dev/cu.usbserial-D30DWZKY"
BAUD_RATE = 115200
# Initialize XBee without Logger
xbee = XBee(port=PORT, baudrate=BAUD_RATE, logger=None)
# Open the XBee connection
if xbee.open():
print("[INFO] XBee connection opened successfully.")
# Perform communication tasks here...
# Close the connection
if xbee.close():
print("[INFO] XBee connection closed successfully.")
else:
print("[WARNING] XBee was already closed.")
else:
print("[ERROR] Failed to open XBee connection.")

🔼 Back to Table of Contents


3️⃣ Sending Data to a Specified XBee Address

Section titled “3️⃣ Sending Data to a Specified XBee Address”

This example demonstrates how to send data from GCS to a vehicle (or any XBee module) using the transmit_data() method.

  • XBee.transmit_data(data, address, retrieveStatus)
ParameterTypeDescription
datastrThe message or command to be sent.
addressstrThe 64-bit address of the destination XBee module. Use "0000000000000000" for broadcast.
retrieveStatusboolIf True, retrieves a transmit status response.
  • True if the transmission is successful.
  • False if the serial port is closed or an error occurs.
from Communication.XBee import XBee
# Configuration
PORT = "/dev/cu.usbserial-D30DWZKY"
BAUD_RATE = 115200
DEST_ADDRESS = "0013A20040B5XXXX" # Replace with actual 64-bit address
# Initialize XBee without Logger
xbee = XBee(port=PORT, baudrate=BAUD_RATE, logger=None)
# Open XBee connection
if xbee.open():
print("[INFO] XBee connection opened successfully.")
# Message to send
message = "Hello Vehicle, this is GCS!"
# Send data to the specified XBee address
if xbee.transmit_data(data=message, address=DEST_ADDRESS, retrieveStatus=False):
print(f"[INFO] Data sent to {DEST_ADDRESS} successfully.")
else:
print(f"[ERROR] Failed to send data to {DEST_ADDRESS}.")
# Close XBee connection after sending data
xbee.close()
else:
print("[ERROR] Failed to open XBee connection.")

🔼 Back to Table of Contents


This example demonstrates how to retrieve incoming data packets from a vehicle using retrieve_data().

  • XBee.retrieve_data()
  • A decoded message if data is received successfully.
  • None if no data is available.
from Communication.XBee import XBee
# Configuration
PORT = "/dev/cu.usbserial-D30DWZKY"
BAUD_RATE = 115200
# Initialize and open XBee connection
xbee = XBee(port=PORT, baudrate=BAUD_RATE)
if xbee.open():
print("[INFO] XBee connection opened. Listening for incoming data...")
while True:
try:
received_data = xbee.retrieve_data()
if received_data:
print(f"[INFO] Received Data: {received_data}")
except KeyboardInterrupt:
print("\n[INFO] Stopping data reception.")
break
else:
print("[ERROR] Failed to open XBee connection.")

🔼 Back to Table of Contents