Index: DialityCoreSerialProtocol.py =================================================================== diff -u -r5f23b5ca87b6211702df8aa805fa767026862b77 -r085c19600947145ec02a4baccba379bdbf6844af --- DialityCoreSerialProtocol.py (.../DialityCoreSerialProtocol.py) (revision 5f23b5ca87b6211702df8aa805fa767026862b77) +++ DialityCoreSerialProtocol.py (.../DialityCoreSerialProtocol.py) (revision 085c19600947145ec02a4baccba379bdbf6844af) @@ -55,9 +55,11 @@ self.__dialityPacketLength = 0 self.__sendRID = -1 self.__sendEvent = threading.Event() - self.responseFunction = None self.__run = False + self.__async_response_dictionary = {} + self.__sync_response_dictionary = {} + if self.__serialConnection is not None: self.__serialListenerThread = threading.Thread(target=self.__listener) else: @@ -156,27 +158,35 @@ #print("Packet CRC: " + str(crc) + ", Calculated CRC:" + str(self.__denaliPacketCRC())) - if True: # crc == self.__denaliPacketCRC(): + # Get the Denali Response Request ID + denaliResponseRequestID = self.__getRequestID(self.__dialityPacket) - denaliResponseRequestID = self.__getRequestID(self.__dialityPacket) - #print("send: "+str(self.__sendRID)+" rec: "+str(denaliResponseRequestID)) - if denaliResponseRequestID == self.__sendRID: - self.__dialityResponsePacket = self.__dialityPacket - self.__sendEvent.set() # Set the "sendEvent", so the "send" command can continue - self.__sendRID = -1 - else: - self.__dialityResponsePacket = bytearray() - self.__dialityPacket.clear() # We didn't get what we were expecting - # We are going to let the command timeout + #if mot crc == self.__denaliPacketCRC(): + # TODO: we need to trash the packet if crc is not correct + #self.__dialityResponsePacket = bytearray() + #self.__dialityPacket.clear() # We didn't get what we were expecting - # print("Final Denali Message: " + str(self.__dialityPacket)) + if denaliResponseRequestID in self.__sync_response_dictionary.keys(): - else: + # if message is a sync message + self.__sync_response_dictionary[denaliResponseRequestID](self.__dialityPacket) - # CRC is corrupted. Drop the packet - self.__dialityPacket.clear() + elif denaliResponseRequestID in self.__async_response_dictionary.keys(): + # if message is a async message + self.__async_response_dictionary[denaliResponseRequestID](self.__dialityPacket) + + elif 0 <= self.__sendRID == denaliResponseRequestID: + + # if message is a response to a command + self.__dialityResponsePacket = self.__dialityPacket + self.__sendEvent.set() # Set the "sendEvent", so the "send" command can continue + self.__sendRID = -1 + + + # we are waiting for a message... + self.__dialitySerialByteBuffer = self.__dialitySerialByteBuffer[self.__dialityPacketLength:] # If there is more to process @@ -199,9 +209,27 @@ return sum(self.__dialityPacket[0:self.__dialityPacketLength - 1]) % 256 - def registerResponseFunction(self, function): - self.responseFunction = function + def registerAsyncFunction(self, request_id, function): + """ + assign a function with message parameter to an async request id, e.g., + def thefunction(msg). + :param request_id: integer + :param function: function reference + + """ + self.__async_response_dictionary[request_id] = function + + def registerSyncFunction(self, request_id, function): + """ + assign a function with message parameter to an sync request id, e.g., + def thefunction(msg). + + :param request_id: integer + :param function: function reference + """ + self.__sync_response_dictionary[request_id] = function + def send(self, message, time_out=1): # Check message ID