|
| 1 | +# Copyright 2019, Digi International Inc. |
| 2 | +# |
| 3 | +# Permission to use, copy, modify, and/or distribute this software for any |
| 4 | +# purpose with or without fee is hereby granted, provided that the above |
| 5 | +# copyright notice and this permission notice appear in all copies. |
| 6 | +# |
| 7 | +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 10 | +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 12 | +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 13 | +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 14 | + |
| 15 | +from digi.xbee.devices import XBeeDevice |
| 16 | +from datetime import datetime |
| 17 | + |
| 18 | +# TODO: Replace with the serial port where your local module is connected to. |
| 19 | +PORT = "COM1" |
| 20 | +# TODO: Replace with the baud rate of your local module. |
| 21 | +BAUD_RATE = 9600 |
| 22 | + |
| 23 | +SEPARATOR = "@@@" |
| 24 | + |
| 25 | +MSG_START = "START" + SEPARATOR |
| 26 | +MSG_END = "END" |
| 27 | +MSG_ACK = "OK" |
| 28 | + |
| 29 | + |
| 30 | +def main(): |
| 31 | + print(" +---------------------------------------------------+") |
| 32 | + print(" | XBee Python Library Receive Bluetooth File Sample |") |
| 33 | + print(" +---------------------------------------------------+\n") |
| 34 | + |
| 35 | + file = None |
| 36 | + |
| 37 | + device = XBeeDevice(PORT, BAUD_RATE) |
| 38 | + |
| 39 | + try: |
| 40 | + device.open() |
| 41 | + |
| 42 | + def bluetooth_data_callback(data): |
| 43 | + global file |
| 44 | + |
| 45 | + # Check if the data is 'START' or 'END'. |
| 46 | + if data.startswith(bytearray(MSG_START, 'utf-8')): |
| 47 | + # Get the file name. |
| 48 | + file_name = data.decode('utf-8').split(SEPARATOR)[1] |
| 49 | + # Open the file for writing. |
| 50 | + file = open(file_name, "w+b") |
| 51 | + print(">> START message received, saving data to file...") |
| 52 | + send_ack(device) |
| 53 | + elif data == bytearray(MSG_END, 'utf-8'): |
| 54 | + file.close() |
| 55 | + print(">> END message received, file '%s'\n" % file.name) |
| 56 | + send_ack(device) |
| 57 | + elif file is not None: |
| 58 | + payload = data[:-1] |
| 59 | + checksum = data[-1] |
| 60 | + # Validate the checksum. |
| 61 | + if 0xFF - (sum(payload) & 0xFF) == checksum: |
| 62 | + # Write block to file. |
| 63 | + file.write(payload) |
| 64 | + send_ack(device) |
| 65 | + |
| 66 | + device.add_bluetooth_data_received_callback(bluetooth_data_callback) |
| 67 | + |
| 68 | + print("Waiting for data from the Bluetooth interface...\n") |
| 69 | + input() |
| 70 | + |
| 71 | + finally: |
| 72 | + if file is not None and file.closed: |
| 73 | + file.close() |
| 74 | + if device is not None and device.is_open(): |
| 75 | + device.close() |
| 76 | + |
| 77 | + |
| 78 | +def send_ack(device): |
| 79 | + """ |
| 80 | + Sends the ACK message to the given XBee device. |
| 81 | +
|
| 82 | + Args: |
| 83 | + device: XBee device to send the message to. |
| 84 | + """ |
| 85 | + device.send_bluetooth_data(MSG_ACK.encode("utf-8")) |
| 86 | + |
| 87 | + |
| 88 | +if __name__ == '__main__': |
| 89 | + main() |
0 commit comments