def ExBusPacket(self, packet_ID): self.exbus_packet = bytearray() # toggles True and False self.get_new_sensor ^= True # send data and text messages for each sensor alternating if self.get_new_sensor: # get next sensor to send its data self.current_sensor = next(self.next_sensor) current_EX_type = 'data' else: current_EX_type = 'text' # get the EX packet for the current sensor ex_packet = self.jetiex.ExPacket(self.current_sensor, current_EX_type) # EX bus header self.exbus_packet.extend(b'3B01') # EX bus packet length in bytes including the header and CRC exbus_packet_length = 8 + len(ex_packet) self.exbus_packet.extend('{:02x}'.format(exbus_packet_length)) # packet ID (answer with same ID as by the request) # FIXME # FIXME check how this works with data and 2x text from EX values??? # FIXME int_ID = int(str(packet_ID), 16) bin_ID = '{:02x}'.format(int_ID) self.exbus_packet.extend(bin_ID) # telemetry identifier self.exbus_packet.extend(b'3A') # packet length in bytes of EX packet ex_packet_length = len(ex_packet) self.exbus_packet.extend('{:02x}'.format(ex_packet_length)) # add EX packet self.exbus_packet.extend(ex_packet) # calculate the crc for the packet crc = CRC16.crc16_ccitt(self.exbus_packet) # compile final telemetry packet self.exbus_packet.extend(crc[2:4]) self.exbus_packet.extend(crc[0:2]) # print('Ex Bus Packet (JetiExBus.py)', self.jetiex.bytes2hex(self.exbus_packet)) # print('Ex Bus Packet (JetiExBus.py)', self.exbus_packet) return self.exbus_packet
def checkCRC(self, packet, crc_check): '''Do a CRC check using CRC16-CCITT Args: packet (bytearray): packet of Jeti Ex Bus including the checksum The last two bytes of the packet are LSB and MSB of the checksum. Returns: bool: True if the crc check is OK, False if NOT ''' crc = CRC16.crc16_ccitt(packet) return crc == crc_check
def checkSpeed(self): '''Check the connection speed via CRC. This needs to be done by the sensor (slave). The speed is either 125000 (default) or 250000 - if CRC ok, then speed is ok - if CRC is not ok, then speed has to be set to the respective 'other' speed Args: packet (bytes): one complete packet received from the Jeti receiver (master) Returns: bool: information if speed check was ok or failed ''' # do the same as in run_forever while True: self.exbus = self.serial.read(8) self.checkTelemetryRequest() # EX bus CRC starts with first byte of the packet offset = 0 # packet to check is message without last 2 bytes packet = bytearray(self.telemetryRequest[:-2]) # the last 2 bytes of the message makeup the crc value for the packet packet_crc = self.telemetryRequest[-2:] # calculate the crc16-ccitt value of the packet crc = CRC16.crc16_ccitt(packet) if crc == packet_crc: speed_changed = False else: # change speed if CRC check fails speed_changed = True if self.baudrate == 125000: self.baudrate = 250000 elif self.baudrate == 250000: self.baudrate = 125000 return speed_changed