Index: DialityCoreCanProtocol.py =================================================================== diff -u -r1a9060a8d3ae75f0d65accd7af3705c860373a1e -rdfcbdcb3369e33c0ad90ce17097dd7fddc767dd6 --- DialityCoreCanProtocol.py (.../DialityCoreCanProtocol.py) (revision 1a9060a8d3ae75f0d65accd7af3705c860373a1e) +++ DialityCoreCanProtocol.py (.../DialityCoreCanProtocol.py) (revision dfcbdcb3369e33c0ad90ce17097dd7fddc767dd6) @@ -32,11 +32,13 @@ PAYLOAD_LENGTH_INDEX = 3 PAYLOAD_START_INDEX = 4 - PAYLOAD_LENGTH_FIRST_PACKET = 4 + PAYLOAD_LENGTH_FIRST_PACKET = 3 HEADER_LENGTH = 4 PACKET_LENGTH = 8 + MAX_MSG_ID_NUMBER = 65535 + @staticmethod def buildBasicMessage(channel_id=0, message=None): """ @@ -65,14 +67,15 @@ payload = [] message = [DenaliCanMessenger.START_BYTE] - if 0 <= message_id <= (2 ** 16 - 1): + if 0 <= message_id <= DenaliMessage.MAX_MSG_ID_NUMBER: # Make sure an unsigned int was passed message_id_in_bytes = message_id.to_bytes(2, byteorder=DenaliMessage.BYTE_ORDER) message += [message_id_in_bytes[0]] message += [message_id_in_bytes[1]] else: + return [] # Check payload length @@ -119,6 +122,7 @@ :param message: dictionary with channel_id and message keys :return: integer with channel id + """ return message['channel_id'] @@ -131,6 +135,7 @@ :param message: complete Diality Packet :return: integer with request ID + """ msg_id_array = message['message'][DenaliMessage.MSG_ID_INDEX: DenaliMessage.PAYLOAD_LENGTH_INDEX] @@ -236,6 +241,7 @@ self.__message = can_message self.__number_of_can_packets_needed = DenaliMessage.getTotalPackets(can_message) self.__number_of_can_packets_up_to_now = 1 + else: self.__message += can_message self.__number_of_can_packets_up_to_now += 1 @@ -244,6 +250,7 @@ return_message = self.__message self.__message = None return return_message + else: return None @@ -280,6 +287,7 @@ if self.__bus is not None: self.__serialListenerThread = threading.Thread(target=self.__listener, daemon=True) + else: self.__serialListenerThread = None print("Can connection is not valid") @@ -317,43 +325,35 @@ message = self.__listener_buffer.get_message(0.0) if message is not None and message.dlc == DenaliMessage.PACKET_LENGTH: - # We have received a legit can message of 8 bytes - can_data = [b for b in message.data] channel_id = message.arbitration_id message_length = can_data[3] # if we are building a long message, then proceed to push it to the channel dictionary if channel_id in self.__longMsgChannelIDSet: - self.__dialinMessage = self.__longMessageBuilders[channel_id].push(can_data) elif can_data[0] == self.START_BYTE and \ - message_length <= DenaliMessage.PAYLOAD_LENGTH_FIRST_PACKET: # This is a short packet + message_length <= DenaliMessage.PAYLOAD_LENGTH_FIRST_PACKET: # This is a short packet # This is the first time that we are building a message - self.__dialinMessage = can_data # deliver the packet elif can_data[0] == self.START_BYTE and \ - message_length > DenaliMessage.PAYLOAD_LENGTH_FIRST_PACKET: # Long packet start - + message_length > DenaliMessage.PAYLOAD_LENGTH_FIRST_PACKET: # Long packet start # We are starting to build a long message, include it in the lonMsgChannelIDSet self.__longMsgChannelIDSet.add(channel_id) if channel_id not in self.__longMessageBuilders.keys(): # if we don't have a builder. Create it! - self.__longMessageBuilders[channel_id] = LongDenaliMessageBuilder(can_data) self.__dialinMessage = None else: # if we do have a builder. This is the first time - self.__dialinMessage = self.__longMessageBuilders[channel_id].push(can_data, first_packet=True) # At this point we have a complete (long or short) Diality Packet if self.__dialinMessage is not None: - completeDialinMessage = DenaliMessage.buildBasicMessage(channel_id=channel_id, message=self.__dialinMessage) dialin_msg_id = DenaliMessage.getMessageID(completeDialinMessage) @@ -364,22 +364,19 @@ self.__longMsgChannelIDSet.remove(dialin_ch_id) # We first check if this is a response to a send request that is pending - if dialin_msg_id == self.__sendPacketRequestID: self.__dialinCommandResponseMessage = completeDialinMessage self.__sendEvent.set() self.__sendPacketRequestID = -1 # If it is not, this is a publication message and we need to call it's register function - elif dialin_ch_id in self.__sync_response_dictionary.keys() and \ dialin_msg_id in self.__sync_response_dictionary[channel_id].keys(): self.__sync_response_dictionary[dialin_ch_id][dialin_msg_id](completeDialinMessage) # Done with this message, let's get the next one - self.__dialinMessage = None else: @@ -399,12 +396,10 @@ # if the channel_id exist, we just update the dictionary for the channel_id if channel_id in self.__sync_response_dictionary.keys(): - self.__sync_response_dictionary[channel_id].update({message_id: function}) # otherwise, we need to create the dictionary for the channel_id else: - self.__sync_response_dictionary[channel_id] = {message_id: function} def send(self, built_message, time_out=1): @@ -460,7 +455,6 @@ def test_print_received_packet(message, sync=False): channel_id = message[0] message[0] = DenaliMessage.START_BYTE - introduction = "Received: " if sync: