def initializedXbee(serialDevice=None): """ Open a serial connection to the locally attached Xbee return an xbee API object representing the module, for sending frames. A signals.FRAME_RECEIVED signal will be sent when a frame is received. If FAKE_SERIAL is used for the serial device name, a fake object is created (and no communication is actually done). """ if serialDevice == FAKE_SERIAL: serialObj = FakeDevice() else: device = serialDevice or pickSerialDevice() serialObj = serial.Serial(device, Config.SERIAL_BAUD) def parseFrameAndSendSignal(rawData): frame = protocol.ParseFromDictSafe(rawData) if frame: responses = signals.FRAME_RECEIVED.send_robust( sender=None, frame=frame) signals.logErrors(responses) xb = xbee.ZigBee(serialObj, callback=parseFrameAndSendSignal) try: yield xb except KeyboardInterrupt as e: log.info('got Ctl-C') except Exception as e: log.error(e.message, exc_info=True) finally: xb.halt() serialObj.close()
def initializedXbee(serialDevice=None): """ Open a serial connection to the locally attached Xbee return an xbee API object representing the module, for sending frames. A signals.FRAME_RECEIVED signal will be sent when a frame is received. If FAKE_SERIAL is used for the serial device name, a fake object is created (and no communication is actually done). """ if serialDevice == FAKE_SERIAL: serialObj = FakeDevice() else: device = serialDevice or pickSerialDevice() serialObj = serial.Serial(device, Config.SERIAL_BAUD) def parseFrameAndSendSignal(rawData): frame = protocol.ParseFromDictSafe(rawData) if frame: responses = signals.FRAME_RECEIVED.send_robust(sender=None, frame=frame) signals.logErrors(responses) xb = xbee.ZigBee(serialObj, callback=parseFrameAndSendSignal) try: yield xb except KeyboardInterrupt as e: log.info('got Ctl-C') except Exception as e: log.error(e.message, exc_info=True) finally: xb.halt() serialObj.close()
def test_write_escaped(self): """ _write method should write the expected data to the serial device """ device = FakeDevice() xbee = XBeeBase(device, escaped=True) xbee._write(b'\x7E\x01\x7D\x11\x13') # Check resuting state of fake device expected_frame = b'\x7E\x00\x05\x7D\x5E\x01\x7D\x5D\x7D\x31\x7D\x33\xDF' self.assertEqual(device.data, expected_frame)
def test_write_again(self): """ _write method should write the expected data to the serial device """ device = FakeDevice() xbee = XBeeBase(device) xbee._write(b'\x00\x01\x02') # Check resuting state of fake device expected_frame = b'\x7E\x00\x03\x00\x01\x02\xFC' self.assertEqual(device.data, expected_frame)
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=stringToBytes('A'), command=stringToBytes('MY')) # Expect a full packet to be written to the device expected_data = b'\x7E\x00\x04\x08AMY\x10' self.assertEqual(serial_port.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. """ serial_port = FakeDevice() xbee = XBeePro900(serial_port) # Send an AT command xbee.send('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(serial_port.data, expected_data)
def setUp(self): """ Prepare a fake device to read from """ self.ser = FakeDevice() self.xbee = XBee(self.ser)