4242 must be enabled. For instructions about how to do this, see
4343 the documentation for your XBee device.
4444
45- Synchonous Mode
45+ .. note ::
46+ The default implementation of the python-xbee library is to
47+ use threads. There is an implementation that utilizes the
48+ Tornado IOLoop, if imported.
49+
50+ Threaded Synchonous Mode
4651~~~~~~~~~~~~~~~
4752
4853The following code demonstrates a minimal use-case for the
@@ -67,7 +72,7 @@ print out any data frames which arrive from a connected XBee
6772device. Be aware that wait_read_frame() will block until
6873a valid frame is received from the associated XBee device.
6974
70- Asynchronous Mode
75+ Threaded Asynchronous Mode
7176~~~~~~~~~~~~~~~~~
7277
7378The xbee package is used only slightly differently when
@@ -110,6 +115,46 @@ to handle receiving and processing incoming data from an
110115XBee device. This example is functionally equivalent to the non-asyncronous
111116example above.
112117
118+ Tornado IOLoop
119+ ~~~~~~~~~~~~~~~~~~
120+ Tornado provides a simple and easy to use IOLoop for asynchronous listening
121+ for XBee communications. The library usage is seemingly identical to the
122+ threaded implementation, excepting importing and yielding. The example
123+ highlights the key differences::
124+
125+ import serial
126+ from tornado import ioloop, gen
127+ from xbee.tornado import XBee
128+
129+ serial_port = serial.Serial('/dev/ttyUSB0', 9600)
130+
131+ def print_data(data):
132+ """
133+ This method is called whenever data is received
134+ from the associated XBee device. Its first and
135+ only argument is the data contained within the
136+ frame.
137+ """
138+ print data
139+
140+ @gen.coroutine
141+ def main():
142+ xbee = XBee(serial_port, callback=print_data)
143+
144+ try:
145+ while True:
146+ yield gen.sleep(0.001)
147+ except KeyboardInterrupt:
148+ ioloop.IOLoop.current().stop()
149+ finally
150+ xbee.halt()
151+ serial_port.close()
152+
153+ ioloop.IOLoop.current().spawn_callback(main)
154+ ioloop.IOLoop.current().start()
155+ ioloop.IOLoop.current().close()
156+
157+
113158Additional Examples
114159~~~~~~~~~~~~~~~~~~~
115160
0 commit comments