def _send(self, packet): ''' Send a packet over the connection ''' raw = protocols.to8bit( packet.getPacketRawBytes() ) self._getConnection().write(protocols.toBinaryString(raw)) self._getConnection().flushOutput() # Log packet hex self._debug('Packet sent to Serial comms connection: %s' % ','.join(protocols.toHex(raw)))
def recieve(self): '''Check for and recieve packets waiting in the connection''' # If nothing in buffer if not len(self._buffer): return # Get protocol protocol = protocols.getProtocol() # Check for any complete packets cache = copy.copy(self._buffer) try: packet = protocol.processRecieveBuffer(self._buffer) except Exception, msg: raise logger.error(msg) logger.error('processRecieveBuffer failed to parse packet from buffer: %s' % join(protocols.toHex(cache))) self._buffer = [] return
def getPacketHex(self): '''Return a packet as hex strings''' packet = self.getEscaped() return protocols.toHex(packet)
def recieve(self): '''Check for and recieve packets waiting in the connection''' conn = self.getConnection() buffer_size = conn.inWaiting() # If nothing in serial buffer, and nothing unprocessed # don't hold up the UI any longer than we have to if not buffer_size and not len(self._buffer): return # If anything new in the buffer, convert to bytes and # append to what we have left unprocessed if buffer_size: buffer = conn.read(buffer_size) for char in buffer: self._buffer.append(ord(char)) # Get protocol protocol = protocols.getProtocol() # Check for any complete packets try: cache = copy.copy(self._buffer) packet = protocol.processRecieveBuffer(self._buffer) except Exception, msg: logger.error(msg) logger.error('processRecieveBuffer failed to parse packet from buffer: %s' % join(protocols.toHex(cache))) self._buffer = [] return
def processRecieveBuffer(self, buffer): '''Check for any incoming packets and return if found''' # Check to make sure the first byte is a start byte # If not, clean up bad bytes if buffer[0] != START_BYTE: # Remove everything before start = buffer.index(START_BYTE) logger.debug('Bad/incomplete data found in buffer before start byte: %s' % ','.join(protocols.toHex(buffer[0:start]))) del buffer[0:start] # If no end byte, try again later when the rest of the packet has arrived if END_BYTE not in buffer: # Quick check to make sure there isn't another packet banked up after # an incomplete one if not buffer.index(START_BYTE, 1): return start = buffer.index(START_BYTE, 1) logger.debug('Bad/incomplete packet found in buffer before a legitimate packet: %s' % ','.join(protocols.toHex(buffer[0:start]))) del buffer[0:start] return # Begin state control machine :-) state = STATE_NOT_PACKET index = 0 packet = [] bad_bytes = [] complete = False # Loop through buffer_copy, delete from buffer buffer_copy = copy.copy(buffer) for byte in buffer_copy: # If have not started a packet yet check for start_byte if state == STATE_NOT_PACKET: # If not a start byte, we should never have got here if byte != START_BYTE: raise Exception, 'Should never have got here, expecting a start byte' # Otherwise, start packet else: state = STATE_IN_PACKET packet.append(START_BYTE) # We are in a packet, save byte unless we find an end byte elif state == STATE_IN_PACKET: if byte == END_BYTE: state = STATE_NOT_PACKET packet.append(END_BYTE) elif byte == ESCAPE_BYTE: state = STATE_ESCAPE_BYTE else: packet.append(byte) # If we are in escape mode (previous byte was an escape), escape byte elif state == STATE_ESCAPE_BYTE: esc_byte = byte ^ 0xFF # Check it is a legitimately escaped byte if esc_byte in (START_BYTE, ESCAPE_BYTE, END_BYTE): packet.append(esc_byte) else: logger.error('Wrongly escaped byte found in buffer: %X' % byte) # Remove this byte from buffer as it has been processed del buffer[0] index += 1 # Check if we have a complete packet if len(packet) and state == STATE_NOT_PACKET: complete = self.processIncomingPacket(packet) break # Process bad_bytes buffer if len(bad_bytes): logger.debug('Bad/incomplete data found in buffer before start byte: %s' % ','.join(protocols.toHex(bad_bytes))) return complete