Procházet zdrojové kódy

bugfixing in shellmatta transport python driver

stefan před 3 roky
rodič
revize
f4f7825e03

+ 2 - 1
.gitignore

@@ -1,3 +1,4 @@
-output
+**__pycache__**
 .cache
 compile_commands.json
+python_driver/shellmatta_transport.py

+ 5 - 3
python_driver/shellmatta_transport.py

@@ -125,7 +125,7 @@ class ShellmattaTransport():
             packet = self.Packet(self.Packet.SOH,
                                 self.Packet.PROTOCOL_VERSION,
                                 packet_type,
-                                len(data),
+                                min(len(data), self.Packet.MAX_PAYLOAD_LENGTH),
                                 0,
                                 destination,
                                 self.sequence_counter_h2s,
@@ -210,6 +210,7 @@ class ShellmattaTransport():
         Args:
             data (bytes): data to send to shellmatta
         """
+
         if not isinstance(data, bytes):
             raise ValueError("data must be od type bytes")
         self.__send(self.Packet.TYPE_DATA, data)
@@ -248,5 +249,6 @@ class ShellmattaTransport():
         self.received_raw_buffer = b''
         self.received_packet = None
         self.received_buffer = b''
-        # send lots of cancels
-        self.com_obj.write(b'\x03' * (self.Packet.MAX_PAYLOAD_LENGTH + 12))
+        # flush the buffer and send one cancel
+        self.com_obj.write(b'\x00' * (self.Packet.MAX_PAYLOAD_LENGTH + 12))
+        self.write(b'\x03')

+ 29 - 12
python_driver/shellmatta_transport_serial.py

@@ -1,6 +1,6 @@
 """ Wrapper arount a shellmatta with transport layer to send commands and receive the response """
 
-import sys
+import time
 import serial
 import argparse
 from shellmatta_transport import ShellmattaTransport
@@ -21,19 +21,34 @@ class ShellmattaSerial():
     def send_command(self, command):
         """ Send command and wait for the prompt in the response. """
 
-        send_string = command + "\r"
-        self.transport.write(str.encode(send_string))
-        data = b''
+        if isinstance(command, str):
+            send_data = str.encode(command)
+        else:
+            send_data = command
+
+        retries = 3
+
+        while retries:
+            send_data = send_data + b"\r"
+            self.transport.write(send_data)
+            data = b''
+
+            while True:
+                data += self.transport.read()
+
+                if data.endswith(str.encode(self.prompt)):
+                    # return received string without echo and prompt
+                    if data.startswith(send_data):
+                        data = data[len(send_data):-len(str.encode(self.prompt))]
+                    break
+
+            # todo implement proper retries / sequence checking
+            if b"crc error\r\n" not in data:
+                break
 
-        while True:
-            data += self.transport.read()
-            data_string = data.decode()
+            retries -= 1
 
-            if data_string.endswith(self.prompt):
-                # return received string without echo and prompt
-                if data_string.startswith(send_string):
-                    data_string = data_string[len(send_string):-len(self.prompt)]
-                return data_string
+        return data
 
     def send_command_only(self, command):
         """ Send command without waiting for response. """
@@ -49,6 +64,8 @@ class ShellmattaSerial():
     def reset_communication(self):
         """ Clears all internal buffers, throwing away all data. """
         self.transport.reset()
+        self.com_port.flush()
+        time.sleep(0.1)
         self.com_port.reset_input_buffer()
         self.com_port.reset_output_buffer()