def test_send_at_command(self): """ Send an AT command with a shorthand call """ # Send an AT command self.xbee.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(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=stringToBytes('A'), command=stringToBytes('MY'), parameter=b'\x00\x00') # Expect a full packet to be written to the device expected_data = b'\x7E\x00\x06\x08AMY\x00\x00\x10' self.assertEqual(self.ser.data, expected_data)
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 _build_command(self, cmd, **kwargs): """ _build_command: string (binary data) ... -> binary data _build_command will construct a command packet according to the specified command's specification in api_commands. It will expect named arguments for all fields other than those with a default value or a length of 'None'. Each field will be written out in the order they are defined in the command definition. """ try: cmd_spec = self.api_commands[cmd] except AttributeError: raise NotImplementedError( "API command specifications could not be " "found; use a derived class which defines" " 'api_commands'.") packet = b'' for field in cmd_spec: try: # Read this field's name from the function arguments dict data = kwargs[field['name']] if isinstance(data, str): data = stringToBytes(data) except KeyError: # Data wasn't given # Only a problem if the field has a specific length if field['len'] is not None: # Was a default value specified? default_value = field['default'] if default_value: # If so, use it data = default_value else: # Otherwise, fail raise KeyError("The expected field {} of length {} " "was not provided".format( field['name'], field['len'])) else: # No specific length, ignore it data = None # Ensure that the proper number of elements will be written if field['len'] and len(data) != field['len']: raise ValueError("The data provided for '{}' was not {} " "bytes long".format(field['name'], field['len'])) # Add the data to the packet, if it has been specified. # Otherwise, the parameter was of variable length, and not given. if data: packet += data return packet
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 = Serial() xbee = XBee(serial_port, io_loop=self._patch_io) # 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' result_data = serial_port.get_data_written() self.assertEqual(result_data, expected_data)
def _build_response(self, frame_type, **kwargs): """ _build_response: string (binary data) ... -> binary data _build_response will construct a response packet according to the specified response's specification in api_responses. It will expect named arguments for all fields other than those with a default value or a length of 'None'. Each field will be written out in the order they are defined in the response definition. """ try: rsp_spec = self.api_responses[frame_type]['structure'] except AttributeError: raise NotImplementedError("API response specification could not be " "found; use a derived class which defines" " 'api_responses'.") # the api_responses has no field names for frame type and frame id packet = frame_type + b'\00' for field in rsp_spec: try: # Read this field's name from the function arg dict data = kwargs[field['name']] if isinstance(data, str): data = stringToBytes(data) except KeyError: # Needs keyword arg if the field has a specific lenght if field['len'] is not None: # Is there a default value? if field['default']: data = field['default'] else: raise KeyError( "The expected field {} of length {} " "was not provided".format( field['name'], field['len'] ) ) else: # No specific lenghth, ignore data = None # Ensure that the proper number of elements will be written if field['len'] and len(data) != field['len']: raise ValueError( "The data provided for '{}' was not {} " "bytes long".format(field['name'], field['len']) ) # Add the data to the packet, if it has been specified. # Otherwise, the parameter was of variable length, and not given. if data: packet += data return packet
def test_build_at_with_default(self): """ _build_command should build a valid at command packet which has no parameter data to be saved and no frame specified (the default value of \x00 should be used) """ at_command = stringToBytes("MY") data = self.xbee._build_command("at", command=at_command) expected_data = b'\x08\x00MY' self.assertEqual(data, expected_data)
def test_build_at(self): """ _build_command should build a valid at command packet which has no parameter data to be saved """ at_command = stringToBytes("MY") frame = intToByte(43) data = self.xbee._build_command("at", frame_id=frame, command=at_command) expected_data = b'\x08+MY' self.assertEqual(data, expected_data)
def test_build_at(self): """ _build_command should build a valid at command packet which has no parameter data to be saved """ at_command = stringToBytes("MY") frame = intToByte(43) data = self.xbee._build_command( "at", frame_id=frame, command=at_command ) expected_data = b'\x08+MY' self.assertEqual(data, expected_data)
base_interval = 0.02 single_interval = 0 sys_counter = datetime.datetime.now() from WatchMan import WatchMan watch_man = WatchMan() while True: try: for id in mocap_bodies: mocap_body = mocap_bodies[id] if mocap_body.is_ready: xbee_device.send('tx_explicit', frame_id=stringToBytes('1'), dest_addr_long=bytearray.fromhex( mocap_body.xbee_address), src_endpoint=bytearray.fromhex('E8'), dest_endpoint=bytearray.fromhex('E8'), cluster=bytearray.fromhex('0011'), profile=bytearray.fromhex('C105'), data=mocap_body.att_pos_msg) single_interval = (datetime.datetime.now() - sys_counter).total_seconds() base_interval = 0.03 - single_interval print(len(mocap_bodies), base_interval, single_interval) if base_interval > 0: time.sleep(base_interval)
def _build_command(self, cmd, **kwargs): """ _build_command: string (binary data) ... -> binary data _build_command will construct a command packet according to the specified command's specification in api_commands. It will expect named arguments for all fields other than those with a default value or a length of 'None'. Each field will be written out in the order they are defined in the command definition. """ try: cmd_spec = self.api_commands[cmd] except AttributeError: raise NotImplementedError("API command specifications could not be " "found; use a derived class which defines" " 'api_commands'.") packet = b'' for field in cmd_spec: try: # Read this field's name from the function arguments dict data = kwargs[field['name']] if isinstance(data, str): data = stringToBytes(data) except KeyError: # Data wasn't given # Only a problem if the field has a specific length if field['len'] is not None: # Was a default value specified? default_value = field['default'] if default_value: # If so, use it data = default_value else: # Otherwise, fail raise KeyError( "The expected field {} of length {} " "was not provided".format( field['name'], field['len'] ) ) else: # No specific length, ignore it data = None # Ensure that the proper number of elements will be written if field['len'] and len(data) != field['len']: raise ValueError( "The data provided for '{}' was not {} " "bytes long".format(field['name'], field['len']) ) # Add the data to the packet, if it has been specified. # Otherwise, the parameter was of variable length, and not given. if data: packet += data return packet