From 535e92ac673ffa98e612425f721f229dc18a4aa1 Mon Sep 17 00:00:00 2001
From: "Robin.Mueller" <robin.mueller.m@gmail.com>
Date: Wed, 19 Aug 2020 21:40:59 +0200
Subject: [PATCH] binary uploader continued

---
 comIF/obsw_com_config.py        |  3 ++-
 comIF/obsw_serial_com_if.py     | 18 +++++++++++++++++-
 utility/obsw_args_parser.py     |  3 ---
 utility/obsw_binary_uploader.py | 19 +++++++++++++++++--
 4 files changed, 36 insertions(+), 7 deletions(-)

diff --git a/comIF/obsw_com_config.py b/comIF/obsw_com_config.py
index 1b59adb..4810be9 100644
--- a/comIF/obsw_com_config.py
+++ b/comIF/obsw_com_config.py
@@ -2,6 +2,7 @@
 Set-up function. Initiates the communication interface.
 """
 import sys
+from typing import Union
 
 from comIF.obsw_com_interface import CommunicationInterface
 from comIF.obsw_dummy_com_if import DummyComIF
@@ -17,7 +18,7 @@ import config.obsw_config as g
 LOGGER = get_logger()
 
 
-def set_communication_interface(tmtc_printer: TmTcPrinter) -> CommunicationInterface:
+def set_communication_interface(tmtc_printer: TmTcPrinter) -> Union[CommunicationInterface, None]:
     """
     Return the desired communication interface object
     :param tmtc_printer: TmTcPrinter object.
diff --git a/comIF/obsw_serial_com_if.py b/comIF/obsw_serial_com_if.py
index 83c2ca7..ccecaa0 100644
--- a/comIF/obsw_serial_com_if.py
+++ b/comIF/obsw_serial_com_if.py
@@ -10,6 +10,8 @@ from typing import Tuple
 from enum import Enum
 
 import serial
+import serial.tools.list_ports
+
 from comIF.obsw_com_interface import CommunicationInterface
 from utility.obsw_tmtc_printer import TmTcPrinter
 from tm.obsw_pus_tm_factory import PusTelemetryFactory, PusTmListT
@@ -47,12 +49,15 @@ class SerialComIF(CommunicationInterface):
         :param ser_com_type: Specify how to handle serial reception
         """
         super().__init__(tmtc_printer)
+        if com_port is None:
+            com_port = self.prompt_com_port()
+        self.valid = False
         try:
             self.serial = serial.Serial(port=com_port, baudrate=baud_rate, timeout=serial_timeout)
             self.valid = True
         except serial.SerialException:
             LOGGER.error("Serial Port opening failure!")
-        self.valid = False
+
         self.data = bytearray()
         self.ser_com_type = ser_com_type
         if self.ser_com_type == SerialCommunicationType.FIXED_FRAME_BASED:
@@ -64,6 +69,17 @@ class SerialComIF(CommunicationInterface):
             #       threads use the deque
             pass
 
+    def prompt_com_port(self):
+        com_port = input(
+            "Serial Commuinication specified without COM port. Please enter COM Port"
+            "(enter h to display list of COM ports): ")
+        if com_port == 'h':
+            ports = serial.tools.list_ports.comports()
+            for port, desc, hwid in sorted(ports):
+                print("{}: {} [{}]".format(port, desc, hwid))
+            com_port = self.prompt_com_port()
+        return com_port
+
     def close(self):
         try:
             self.serial.close()
diff --git a/utility/obsw_args_parser.py b/utility/obsw_args_parser.py
index 01bc892..67a6fc0 100644
--- a/utility/obsw_args_parser.py
+++ b/utility/obsw_args_parser.py
@@ -86,9 +86,6 @@ def handle_unspecified_args(args) -> None:
     """
     if args.com_if == 1 and args.tm_timeout is None:
         args.tm_timeout = 6.0
-    if args.com_if == 1 and args.com_port is None:
-        args.com_port = input("Serial Commuinication specified without COM port. "
-                              "Please enter COM Port: ")
     if args.mode is None:
         print("No mode specified with -m Parameter.")
         print("Possible Modes: ")
diff --git a/utility/obsw_binary_uploader.py b/utility/obsw_binary_uploader.py
index 979ee2f..4b5b552 100644
--- a/utility/obsw_binary_uploader.py
+++ b/utility/obsw_binary_uploader.py
@@ -13,6 +13,21 @@ from tkinter import filedialog
 def perform_binary_upload():
     root = tk.Tk()
     root.withdraw()
+    root.wm_attributes('-topmost', 1)
+    print("Please select file to upload")
+    file_path = filedialog.askopenfilename(parent=root)
+    print("File select: " + str(file_path))
+    calc_hamming_code = input("Calculate and send hamming code? [y/n]")
+    if calc_hamming_code in ['y', 'yes', 1]:
+        calc_hamming_code = True
+        print("Hamming code will be calculated and sent in tail packet")
+    else:
+        calc_hamming_code = False
+        print("Hamming code will not be calculated")
+
+    if calc_hamming_code:
+        # now we calculate the hamming code
+        pass
+
+    # We have to split the binary here first
 
-    file_path = filedialog.askopenfilename()
-    print(file_path)
-- 
GitLab