def parseSerialMsg(self, msgBytes, msgStart): """Searches raw serial data for HDLC messages and then validates message integrity by comparing a computed CRC to the CRC found in the message. Valid messages are then stored for processing. Args: msgBytes: Raw serial data to be parsed. msgStart: Start location to begin looking for serial message in msgBytes data array. """ if len(msgBytes) > 0: # Process serial message self.msg.decodeMsg(msgBytes, msgStart) if self.msg.msgFound == True: # Message start found if self.msg.msgEnd != -1: # entire msg found # Check msg CRC crc = self.crc(self.msg.msg[:-self.crcLength]) if self.msg.msg[-self.crcLength:] == packData( crc, self.crcLength): # CRC matches - valid message #print("CRC matches") self.parsedMsgs.append(self.msg.msg[:-self.crcLength]) return self.msg.msgEnd else: # partial msg found pass else: # No message found pass return len( msgBytes) # all bytes parsed so return length of input message else: return 0
def encodeMsg(self, inputMsg): """Encodes provided serial data into an HDLC message. Args: inputMsg: Serial bytes to be encoded into HDLC message. """ if not inputMsg: # Check for empty msg return # Create crc crc = self.crc(inputMsg) inputMsg = inputMsg + packData(crc, self.crcLength) outMsg = bytearray() outMsg += HDLC_END # start message for i in range(len(inputMsg)): byte = inputMsg[i:i + 1] if (byte == HDLC_END): # Replace END character outMsg += HDLC_ESC outMsg += HDLC_END_SHIFTED elif (byte == HDLC_ESC): # Replace ESC character outMsg += HDLC_ESC outMsg += HDLC_ESC_SHIFTED elif (byte == HDLC_END_TDMA): # Replace TDMA END character outMsg += HDLC_ESC outMsg += HDLC_END_TDMA_SHIFTED else: # Insert raw message byte outMsg += byte outMsg += HDLC_END # end message self.encoded = outMsg
def encodeMsg(self, byteList): """Encodes provided serial data into a SLIP message. Args: byteList: Serial bytes to be encoded into SLIP message. """ if not byteList: # Check for empty msg return # Create crc crc = self.crc(byteList) byteList = byteList + packData(crc, self.crcLength) self.encoded = b'' self.encoded += SLIP_END for i in range(len(byteList)): byte = byteList[i:i + 1] if byte == SLIP_END: # Replace END character self.encoded += SLIP_ESC + SLIP_ESC_END elif byte == SLIP_ESC: # Replace ESC character self.encoded += SLIP_ESC + SLIP_ESC_ESC #elif byte == SLIP_END_TDMA: # Replace TDMA END character # self.encoded += SLIP_ESC + SLIP_ESC_END_TDMA else: # Insert raw byte into message self.encoded += byte self.encoded += SLIP_END
def encodeMsg(self, msgBytes): if msgBytes: # Add CRC crc = self.crc(msgBytes) msgBytes = msgBytes + packData(crc, self.crcLength) self.msg.encodeMsg(msgBytes) return self.msg.hdlc return msgBytes
def parseMsg(self, msgBytes, msgStart): if len(msgBytes) > 0: # Process serial message self.decodeMsg(msgBytes, msgStart) if self.msgFound == True: # Message start found if self.msgEnd != -1: # entire msg found # Check msg CRC crc = self.crc(self.msg[:-self.crcLength]) if self.msg[-self.crcLength:] == packData( crc, self.crcLength): # CRC matches - valid message #print("CRC matches") return self.msg[:-self.crcLength] return [] # no message found
def setup_method(self, method): self.hdlcMsg = HDLCMsg(256) self.truthCRC = packData(self.hdlcMsg.crc(testMsg), self.hdlcMsg.crcLength) pass
def setup_method(self, method): self.slipMsg = SLIPMsg(256) self.truthCRC = packData(self.slipMsg.crc(testMsg), self.slipMsg.crcLength) pass