8 Tests the XBee (IEEE 802.15.4) implementation class for XBee API compliance
16 Base initalization class
20 Initialize XBee object
26 _build_command should properly build a command packet
31 if not enough or incorrect data is provided, an exception should
35 self.
xbee._build_command(
"at")
42 "An exception was not raised with improper data supplied"
47 if data of incorrect length is provided, an exception should be
51 self.
xbee._build_command(
"at", frame_id=
"AB", command=
"MY")
58 "An exception was not raised with improper data length"
63 _build_command should build a valid at command packet which has
64 no parameter data to be saved
69 data = self.
xbee._build_command(
75 expected_data =
'\x08+MY'
76 self.assertEqual(data, expected_data)
80 _build_command should build a valid at command packet which has
81 no parameter data to be saved and no frame specified (the
82 default value of \x00 should be used)
86 data = self.
xbee._build_command(
"at", command=at_command)
88 expected_data =
'\x08\x00MY'
89 self.assertEqual(data, expected_data)
93 _split_response should properly split a response packet
98 if a response begins with an unrecognized id byte,
99 _split_response should raise an exception
101 data =
'\x23\x00\x00\x00'
104 self.
xbee._split_response(data)
114 if a response doesn't match the specification's layout,
115 _split_response should raise an exception
118 data =
'\x8a\x00\x00\x00'
119 self.assertRaises(ValueError, self.
xbee._split_response, data)
123 if a response doesn't match the specification's layout,
124 _split_response should raise an exception
128 self.assertRaises(ValueError, self.
xbee._split_response, data)
132 _split_response should properly split a status response packet
136 info = self.
xbee._split_response(data)
137 expected_info = {
'id':
'status',
140 self.assertEqual(info, expected_info)
144 _split_response should properly split an at_response packet which
145 has no parameter data
149 info = self.
xbee._split_response(data)
150 expected_info = {
'id':
'at_response',
154 self.assertEqual(info, expected_info)
158 _split_response should properly split an at_response packet which
162 data =
'\x88DMY\x01ABCDEF'
163 info = self.
xbee._split_response(data)
164 expected_info = {
'id':
'at_response',
168 'parameter':
'ABCDEF'}
169 self.assertEqual(info, expected_info)
174 XBee class should properly parse IO data received from an XBee
180 _parse_samples should properly parse a packet containing a single
181 sample of only digital io data
184 header =
'\x01\x01\xFF'
188 data = header + sample
190 expected_results = [{
'dio-0':
True,
200 results = self.
xbee._parse_samples(data)
202 self.assertEqual(results, expected_results)
206 _parse_samples should properly parse a packet containing a single
207 sample of only digital io data, which alternates between on and
211 header =
'\x01\x01\xFF'
215 data = header + sample
217 expected_results = [{
'dio-0':
False,
227 results = self.
xbee._parse_samples(data)
229 self.assertEqual(results, expected_results)
233 _parse_samples should properly parse a packet containing a single
234 sample of only digital io data for only a subset of the
239 header =
'\x01\x00\xAA'
243 data = header + sample
245 expected_results = [{
'dio-1':
True,
250 results = self.
xbee._parse_samples(data)
252 self.assertEqual(results, expected_results)
256 _parse_samples should properly parse a packet containing a single
257 sample of only digital io data for only a subset of the
262 header =
'\x01\x00\x01'
266 data = header + sample
268 expected_results = [{
'dio-0':
False}]
270 results = self.
xbee._parse_samples(data)
272 self.assertEqual(results, expected_results)
276 _parse_samples should properly parse a packet containing two
277 samples of only digital io data for one dio line
281 header =
'\x02\x00\x01'
284 sample =
'\x00\xAA' +
'\x00\x01'
285 data = header + sample
287 expected_results = [{
'dio-0':
False},
290 results = self.
xbee._parse_samples(data)
292 self.assertEqual(results, expected_results)
296 _parse_samples should properly parse a packet containing three
297 samples of only digital io data
300 header =
'\x03\x01\xFF'
306 sample =
'\x01\xFF' +
'\x00\xAA' +
'\x00\x00'
307 data = header + sample
309 expected_results = [{
'dio-0':
True,
337 results = self.
xbee._parse_samples(data)
339 self.assertEqual(results, expected_results)
343 _parse_samples should parse a data packet containing multiple
344 samples of adc data from multiple pins in the proper order
348 header =
'\x02\x06\x00'
355 sample =
'\x00\x00' +
'\x00\xFF' +
'\x00\x05' +
'\x00\x07'
356 data = header + sample
358 expected_results = [{
'adc-0':0,
363 results = self.
xbee._parse_samples(data)
365 self.assertEqual(results, expected_results)
369 _parse_samples should properly parse a packet containing a single
370 sample of digital and analog io data for only a subset of the
375 header =
'\x01\x02\xAA'
379 sample =
'\x00\xAA\x00\xFF'
380 data = header + sample
382 expected_results = [{
'dio-1':
True,
388 results = self.
xbee._parse_samples(data)
390 self.assertEqual(results, expected_results)
394 XBee class should properly write binary data in a valid API
395 frame to a given serial device, including a valid command packet.
400 calling send should write a full API frame containing the
401 API AT command packet to the serial device.
405 xbee =
XBee(serial_port)
408 xbee.send(
'at', frame_id=
'A', command=
'MY')
411 expected_data =
'\x7E\x00\x04\x08AMY\x10'
412 self.assertEqual(serial_port.data, expected_data)
417 calling send should write a full API frame containing the
418 API AT command packet to the serial device.
422 xbee =
XBee(serial_port)
433 expected_data =
'\x7E\x00\x06\x08AMY\x00\x00\x10'
434 self.assertEqual(serial_port.data, expected_data)
438 Tests shorthand for sending commands to an XBee provided by
444 Prepare a fake device to read from
451 Send an AT command with a shorthand call
454 self.
xbee.at(frame_id=
'A', command=
'MY')
457 expected_data =
'\x7E\x00\x04\x08AMY\x10'
458 self.assertEqual(self.
ser.data, expected_data)
462 calling send should write a full API frame containing the
463 API AT command packet to the serial device.
467 self.
xbee.at(frame_id=
'A', command=
'MY', parameter=
'\x00\x00')
470 expected_data =
'\x7E\x00\x06\x08AMY\x00\x00\x10'
471 self.assertEqual(self.
ser.data, expected_data)
475 When shorthand is disabled, any attempt at calling a
476 non-existant attribute should raise AttributeError
482 except AttributeError:
485 self.fail(
"Specified shorthand command should not exist")
489 XBee class should properly read and parse binary data from a serial
494 read and parse a parameterless AT command
499 info = xbee.wait_read_frame()
500 expected_info = {
'id':
'at_response',
504 self.assertEqual(info, expected_info)
508 read and parse an AT command with a parameter
511 '\x7E\x00\x08\x88DMY\x01\x00\x00\x00\x8c'
515 info = xbee.wait_read_frame()
516 expected_info = {
'id':
'at_response',
520 'parameter':
'\x00\x00\x00'}
521 self.assertEqual(info, expected_info)
525 XBee class should properly read and parse incoming IO data
530 header =
'\x01\x02\xAA'
534 sample =
'\x00\xAA\x00\xFF'
535 data = header + sample
539 rx_io_resp =
'\x83\x00\x01\x28\x00'
542 '\x7E\x00\x0C'+ rx_io_resp + data +
'\xfd'
546 info = xbee.wait_read_frame()
547 expected_info = {
'id':
'rx_io_data',
548 'source_addr':
'\x00\x01',
551 'samples': [{
'dio-1':
True,
557 self.assertEqual(info, expected_info)
560 if __name__ ==
'__main__':