def test_read_at(self): """ read and parse a parameterless AT command """ device = FakeReadDevice('\x7E\x00\x05\x88DMY\x01\x8c') xbee = XBee(device) info = xbee.wait_read_frame() expected_info = {'id':'at_response', 'frame_id':'D', 'command':'MY', 'status':'\x01'} self.assertEqual(info, expected_info)
class TestSendShorthand(unittest.TestCase): """ Tests shorthand for sending commands to an XBee provided by XBee.__getattr__ """ def setUp(self): """ Prepare a fake device to read from """ self.ser = FakeDevice() self.xbee = XBee(self.ser) def test_send_at_command(self): """ Send an AT command with a shorthand call """ # Send an AT command self.xbee.at(frame_id='A', command='MY') # Expect a full packet to be written to the device expected_data = '\x7E\x00\x04\x08AMY\x10' self.assertEqual(self.ser.data, expected_data) def test_send_at_command_with_param(self): """ calling send should write a full API frame containing the API AT command packet to the serial device. """ # Send an AT command self.xbee.at(frame_id='A', command='MY', parameter='\x00\x00') # Expect a full packet to be written to the device expected_data = '\x7E\x00\x06\x08AMY\x00\x00\x10' self.assertEqual(self.ser.data, expected_data) def test_shorthand_disabled(self): """ When shorthand is disabled, any attempt at calling a non-existant attribute should raise AttributeError """ self.xbee = XBee(self.ser, shorthand=False) try: self.xbee.at except AttributeError: pass else: self.fail("Specified shorthand command should not exist")
def test_send_at_command(self): """ calling send should write a full API frame containing the API AT command packet to the serial device. """ serial_port = FakeDevice() xbee = XBee(serial_port) # Send an AT command xbee.send('at', frame_id='A', command='MY') # Expect a full packet to be written to the device expected_data = '\x7E\x00\x04\x08AMY\x10' self.assertEqual(serial_port.data, expected_data)
def test_shorthand_disabled(self): """ When shorthand is disabled, any attempt at calling a non-existant attribute should raise AttributeError """ self.xbee = XBee(self.ser, shorthand=False) try: self.xbee.at except AttributeError: pass else: self.fail("Specified shorthand command should not exist")
def test_read_io_data(self): """ XBee class should properly read and parse incoming IO data """ ## Build IO data # One sample, ADC 0 enabled # DIO 1,3,5,7 enabled header = '\x01\x02\xAA' # First 7 bits ignored, DIO8 low, DIO 0-7 alternating # ADC0 value of 255 sample = '\x00\xAA\x00\xFF' data = header + sample ## Wrap data in frame # RX frame data rx_io_resp = '\x83\x00\x01\x28\x00' device = FakeReadDevice( '\x7E\x00\x0C'+ rx_io_resp + data + '\xfd' ) xbee = XBee(device) #pdb.set_trace() info = xbee.wait_read_frame() expected_info = {'id':'rx_io_data', 'source_addr':'\x00\x01', 'rssi':'\x28', 'options':'\x00', 'samples': [{'dio-1':True, 'dio-3':True, 'dio-5':True, 'dio-7':True, 'adc-0':255}] } self.assertEqual(info, expected_info)
def setUp(self): """ Prepare a fake device to read from """ self.ser = FakeDevice() self.xbee = XBee(self.ser)