def build(self): self._logger.debug("building message") buffer = [] data_length = len(self.__datas) opt_length = len(self.__optDatas) # sync byte buffer.append(self.__syncByte) # adding sync byte # header buffer.append((data_length >> 8) & 0xFF) # first byte length data buffer.append(data_length & 0xFF) # second byte length data buffer.append(opt_length & 0xFF) # optionnal data length buffer.append(self.__type & 0xFF) # packet type byte # CRC Header buffer.append(CRC8Utils.calc(buffer[1:5])) # data buffer += self.__datas buffer += self.__optDatas # CRC Data buffer.append(CRC8Utils.calc(buffer[6:])) self._logger.debug("buffer : {0}".format(buffer)) return buffer
def build(self): self._logger.debug('building message') buffer = [] data_length = len(self.__datas) opt_length = len(self.__optDatas) #sync byte buffer.append(self.__syncByte) # adding sync byte #header buffer.append((data_length >> 8) & 0xFF) #first byte length data buffer.append(data_length & 0xFF) #second byte length data buffer.append(opt_length & 0xFF) #optionnal data length buffer.append(self.__type & 0xFF) #packet type byte #CRC Header buffer.append(CRC8Utils.calc(buffer[1:5])) #data buffer += self.__datas buffer += self.__optDatas #CRC Data buffer.append(CRC8Utils.calc(buffer[6:])) self._logger.debug('buffer : {0}'.format(buffer)) return buffer
def _getSerialData(self): self._logger.debug('searching for sync byte') s = 0 while s != b'\x55': if self.__connection.inWaiting() != 0: s = self.__connection.read(1) self._logger.debug('sync byte found') while self.__connection.inWaiting() < 5: () header = self.__connection.read(4) #read header fields headerCRC = self.__connection.read(1)[0] #read header crc field self._logger.debug('header reading : {0} and crc : {1}'.format(header, headerCRC)) if (CRC8Utils.calc(header) == headerCRC): self._logger.debug('header CRC OK') data_length, opt_length, msgType = struct.unpack("!HBB", header) self._logger.debug('data_length {0}; opt_length {1}; msg_type {2}'.format( data_length, opt_length, msgType )) totalDataLength = data_length + opt_length while self.__connection.inWaiting() < totalDataLength+1: () datas = self.__connection.read(data_length) opts = self.__connection.read(opt_length) dataCRC = self.__connection.read(1) self._logger.debug('datas {0}; opts {1}; dataCRC {2}'.format( datas, opts, dataCRC )) if(self._logger.isEnabledFor(logging.DEBUG)): msg = header msg += bytes({headerCRC}) msg += datas msg += opts msg += dataCRC self._logger.debug(msg) if (CRC8Utils.calc(datas+opts) == dataCRC[0]): return EnOceanMessage(msgType, datas, opts) return "Data CRC Failed" return "Header CRC Failed"