Bladeren bron

add method to check if a response is complete

subDesTagesMitExtraKaese 1 maand geleden
bovenliggende
commit
339592f7fd
1 gewijzigde bestanden met toevoegingen van 12 en 3 verwijderingen
  1. 12 3
      src/protocol.py

+ 12 - 3
src/protocol.py

@@ -129,11 +129,20 @@ class LumiaxClient:
         result = header + bytes(data)
         return result + crc16(result)
 
-
-
-    def parse(self, start_address: int, buffer: bytes) -> list[(Variable, Value)]:
+    def is_complete(self, buffer: bytes) -> bool:
+        if len(buffer) < 4:
+            return False
         device_id = buffer[0]
         function_code = FunctionCodes(buffer[1])
+        if function_code in [FunctionCodes.READ_MEMORY, FunctionCodes.READ_PARAMETER, FunctionCodes.READ_STATUS_REGISTER]:
+            data_length = buffer[2]
+            return len(buffer) >= data_length + 5
+        else:
+            return len(buffer) >= 8
+
+    def parse(self, start_address: int, buffer: bytes) -> list[Result]:
+        self.device_id = buffer[0]
+        function_code = FunctionCodes(buffer[1])
         if function_code in [FunctionCodes.READ_MEMORY, FunctionCodes.READ_PARAMETER, FunctionCodes.READ_STATUS_REGISTER]:
             data_length = buffer[2]
             received_crc = buffer[3+data_length:3+data_length+2]