|
| 1 | +--- |
| 2 | +title: XBee Serial API Usage Guide |
| 3 | +description: Examples of how to use the xbee serial library |
| 4 | +--- |
| 5 | +This guide provides examples of how to use the XBee API for **GCS and vehicle communication**. |
| 6 | + |
| 7 | +## Table of Contents |
| 8 | +- [1️⃣ Initializing the XBee Connection](#1️⃣-initializing-the-xbee-connection) |
| 9 | +- [2️⃣ Closing the XBee Connection](#2️⃣-closing-the-xbee-connection) |
| 10 | +- [3️⃣ Sending Data to a Specified XBee Address](#3️⃣-sending-data-to-a-specified-xbee-address) |
| 11 | +- [4️⃣ Receiving Data from Another XBee](#4️⃣-receiving-data-from-another-xbee) |
| 12 | + |
| 13 | +--- |
| 14 | +## **1️⃣ Initializing the XBee Connection** |
| 15 | +This example shows how to **open an XBee connection** before sending or receiving data. |
| 16 | + |
| 17 | +### **Methods Used:** |
| 18 | +- `XBee.__init__(port, baudrate, status, logger)` |
| 19 | +- `XBee.open()` |
| 20 | + |
| 21 | +### **Parameters:** |
| 22 | +| Parameter | Type | Description | |
| 23 | +|-----------|------|-------------| |
| 24 | +| `port` | `str` | Serial port name (e.g., `/dev/cu.usbserial-D30DWZKY`) | |
| 25 | +| `baudrate` | `int` | XBee baud rate (default: `115200`) | |
| 26 | +| `status` | `bool` | Enable automatic status reception (default: `False`) | |
| 27 | +| `logger` | `Logger` | Logger instance for debugging | |
| 28 | + |
| 29 | +### **Returns:** |
| 30 | +- `True` if the connection opens successfully. |
| 31 | +- `False` if it fails. |
| 32 | + |
| 33 | +:::note[Note] |
| 34 | +`Logger` is not required. It is used for **testing and debugging** in GCS but can be omitted (logger=None). |
| 35 | +::: |
| 36 | + |
| 37 | +### **Example:** |
| 38 | +```python |
| 39 | +from Communication.XBee import XBee |
| 40 | +from Logger.Logger import Logger |
| 41 | + |
| 42 | +# Configuration |
| 43 | +PORT = "/dev/cu.usbserial-D30DWZKY" |
| 44 | +BAUD_RATE = 115200 |
| 45 | +LOGGER = Logger() |
| 46 | + |
| 47 | +# Initialize XBee |
| 48 | +xbee = XBee(port=PORT, baudrate=BAUD_RATE, logger=LOGGER) |
| 49 | + |
| 50 | +# Open XBee connection |
| 51 | +if xbee.open(): |
| 52 | + print("[INFO] XBee connection opened successfully.") |
| 53 | +else: |
| 54 | + print("[ERROR] Failed to open XBee connection.") |
| 55 | +``` |
| 56 | +[🔼 Back to Table of Contents](#table-of-contents) |
| 57 | + |
| 58 | + |
| 59 | +<br> |
| 60 | + |
| 61 | +## **2️⃣ Closing the XBee Connection** |
| 62 | +This example demonstrates how to **properly close the XBee serial connection** when it is no longer needed. |
| 63 | + |
| 64 | +### **Methods Used:** |
| 65 | +- XBee.close() |
| 66 | + |
| 67 | +### **Returns:** |
| 68 | +- `True` if the serial port is successfully closed. |
| 69 | +- `False` if the port is already closed or an error occurs. |
| 70 | + |
| 71 | +:::note[Note] |
| 72 | +It is good practice to **always close the XBee connection** when it is no longer in use to free up system resources. |
| 73 | +::: |
| 74 | +### **Example:** |
| 75 | +```python |
| 76 | +from Communication.XBee import XBee |
| 77 | + |
| 78 | +# Configuration |
| 79 | +PORT = "/dev/cu.usbserial-D30DWZKY" |
| 80 | +BAUD_RATE = 115200 |
| 81 | + |
| 82 | +# Initialize XBee without Logger |
| 83 | +xbee = XBee(port=PORT, baudrate=BAUD_RATE, logger=None) |
| 84 | + |
| 85 | +# Open the XBee connection |
| 86 | +if xbee.open(): |
| 87 | + print("[INFO] XBee connection opened successfully.") |
| 88 | + |
| 89 | + # Perform communication tasks here... |
| 90 | + |
| 91 | + # Close the connection |
| 92 | + if xbee.close(): |
| 93 | + print("[INFO] XBee connection closed successfully.") |
| 94 | + else: |
| 95 | + print("[WARNING] XBee was already closed.") |
| 96 | +else: |
| 97 | + print("[ERROR] Failed to open XBee connection.") |
| 98 | +``` |
| 99 | +[🔼 Back to Table of Contents](#table-of-contents) |
| 100 | + |
| 101 | + |
| 102 | +<br> |
| 103 | + |
| 104 | +## **3️⃣ Sending Data to a Specified XBee Address** |
| 105 | +This example demonstrates how to **send data from GCS to a vehicle (or any XBee module)** using the `transmit_data()` method. |
| 106 | + |
| 107 | +### **Methods Used:** |
| 108 | +- `XBee.transmit_data(data, address, retrieveStatus)` |
| 109 | + |
| 110 | +### **Parameters:** |
| 111 | +| Parameter | Type | Description | |
| 112 | +|-----------|------|-------------| |
| 113 | +| `data` | `str` | The message or command to be sent. | |
| 114 | +| `address` | `str` | The 64-bit address of the destination XBee module. Use `"0000000000000000"` for **broadcast**. | |
| 115 | +| `retrieveStatus` | `bool` | If `True`, retrieves a transmit status response. | |
| 116 | + |
| 117 | +### **Returns:** |
| 118 | +- `True` if the transmission is **successful**. |
| 119 | +- `False` if the **serial port is closed** or an error occurs. |
| 120 | + |
| 121 | +:::note[Note] |
| 122 | +This method **does not confirm** whether the message was delivered successfully to the target. You should check the **Transmit Status Frame (`0x89`)** to confirm delivery. This method is still work-in-progress. Once the functionality of receiving a **Transmit Status Frame (`0x89`)** is implemented, a new example will be added. |
| 123 | +::: |
| 124 | + |
| 125 | +### **Example:** |
| 126 | +```python |
| 127 | +from Communication.XBee import XBee |
| 128 | + |
| 129 | +# Configuration |
| 130 | +PORT = "/dev/cu.usbserial-D30DWZKY" |
| 131 | +BAUD_RATE = 115200 |
| 132 | +DEST_ADDRESS = "0013A20040B5XXXX" # Replace with actual 64-bit address |
| 133 | + |
| 134 | +# Initialize XBee without Logger |
| 135 | +xbee = XBee(port=PORT, baudrate=BAUD_RATE, logger=None) |
| 136 | + |
| 137 | +# Open XBee connection |
| 138 | +if xbee.open(): |
| 139 | + print("[INFO] XBee connection opened successfully.") |
| 140 | + |
| 141 | + # Message to send |
| 142 | + message = "Hello Vehicle, this is GCS!" |
| 143 | + |
| 144 | + # Send data to the specified XBee address |
| 145 | + if xbee.transmit_data(data=message, address=DEST_ADDRESS, retrieveStatus=False): |
| 146 | + print(f"[INFO] Data sent to {DEST_ADDRESS} successfully.") |
| 147 | + else: |
| 148 | + print(f"[ERROR] Failed to send data to {DEST_ADDRESS}.") |
| 149 | + |
| 150 | + # Close XBee connection after sending data |
| 151 | + xbee.close() |
| 152 | +else: |
| 153 | + print("[ERROR] Failed to open XBee connection.") |
| 154 | +``` |
| 155 | +[🔼 Back to Table of Contents](#table-of-contents) |
| 156 | + |
| 157 | +<br> |
| 158 | + |
| 159 | +## **4️⃣ Receiving Data from Another XBee** |
| 160 | +This example demonstrates how to **retrieve incoming data packets** from a vehicle using `retrieve_data()`. |
| 161 | + |
| 162 | +### **Methods Used:** |
| 163 | +- `XBee.retrieve_data()` |
| 164 | + |
| 165 | +### **Returns:** |
| 166 | +- A decoded message if data is received successfully. |
| 167 | +- `None` if no data is available. |
| 168 | + |
| 169 | +:::note[Note] |
| 170 | +Ensure that the XBee connection is **open** before attempting to retrieve data. |
| 171 | +::: |
| 172 | + |
| 173 | +### **Example:** |
| 174 | +```python |
| 175 | +from Communication.XBee import XBee |
| 176 | + |
| 177 | +# Configuration |
| 178 | +PORT = "/dev/cu.usbserial-D30DWZKY" |
| 179 | +BAUD_RATE = 115200 |
| 180 | + |
| 181 | +# Initialize and open XBee connection |
| 182 | +xbee = XBee(port=PORT, baudrate=BAUD_RATE) |
| 183 | + |
| 184 | +if xbee.open(): |
| 185 | + print("[INFO] XBee connection opened. Listening for incoming data...") |
| 186 | + |
| 187 | + while True: |
| 188 | + try: |
| 189 | + received_data = xbee.retrieve_data() |
| 190 | + |
| 191 | + if received_data: |
| 192 | + print(f"[INFO] Received Data: {received_data}") |
| 193 | + except KeyboardInterrupt: |
| 194 | + print("\n[INFO] Stopping data reception.") |
| 195 | + break |
| 196 | +else: |
| 197 | + print("[ERROR] Failed to open XBee connection.") |
| 198 | +``` |
| 199 | + |
| 200 | +[🔼 Back to Table of Contents](#table-of-contents) |
| 201 | + |
| 202 | + |
| 203 | + |
0 commit comments