|
@@ -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()
|
|
|
|