From f484d50b8b2674671a4ab3862450d58192e2e91e Mon Sep 17 00:00:00 2001
From: "Robin.Mueller" <robin.mueller.m@gmail.com>
Date: Tue, 14 Jan 2020 00:50:00 +0100
Subject: [PATCH] Service 3 testing, bugfixes

---
 .../OBSW_UdpClient_Service_3_Serial_.xml      |  4 +-
 .../OBSW_UdpClient_Single_Command_Serial.xml  |  4 +-
 OBSW_TmTcClient.py                            |  4 +-
 comIF/OBSW_Serial_ComIF.py                    |  6 ++-
 tc/OBSW_TcService3.py                         | 38 ++++++-------
 tm/OBSW_TmPacket.py                           | 44 +--------------
 tm/OBSW_TmService1.py                         |  8 +--
 tm/OBSW_TmService3.py                         | 18 ++++++-
 tm/OBSW_TmService5.py                         | 53 +++++++++++++++++++
 utility/OBSW_TmTcPrinter.py                   |  2 +-
 10 files changed, 106 insertions(+), 75 deletions(-)
 create mode 100644 tm/OBSW_TmService5.py

diff --git a/.idea/runConfigurations/OBSW_UdpClient_Service_3_Serial_.xml b/.idea/runConfigurations/OBSW_UdpClient_Service_3_Serial_.xml
index 7a1d8a7..ded0da2 100644
--- a/.idea/runConfigurations/OBSW_UdpClient_Service_3_Serial_.xml
+++ b/.idea/runConfigurations/OBSW_UdpClient_Service_3_Serial_.xml
@@ -12,8 +12,8 @@
     <option name="ADD_CONTENT_ROOTS" value="true" />
     <option name="ADD_SOURCE_ROOTS" value="true" />
     <EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
-    <option name="SCRIPT_NAME" value="C:\Users\Robin\NoSyncDokumente\sourceobsw\tmtc\OBSW_TmTcClient.py" />
-    <option name="PARAMETERS" value="-m 3 -s 3 -p -c 1 -t 4" />
+    <option name="SCRIPT_NAME" value="$PROJECT_DIR$/OBSW_TmTcClient.py" />
+    <option name="PARAMETERS" value="-m 3 -s 3 -p -c 1 -t 4 --hk" />
     <option name="SHOW_COMMAND_LINE" value="false" />
     <option name="EMULATE_TERMINAL" value="false" />
     <option name="MODULE_MODE" value="false" />
diff --git a/.idea/runConfigurations/OBSW_UdpClient_Single_Command_Serial.xml b/.idea/runConfigurations/OBSW_UdpClient_Single_Command_Serial.xml
index b2b8e99..8b5bad4 100644
--- a/.idea/runConfigurations/OBSW_UdpClient_Single_Command_Serial.xml
+++ b/.idea/runConfigurations/OBSW_UdpClient_Single_Command_Serial.xml
@@ -12,8 +12,8 @@
     <option name="ADD_CONTENT_ROOTS" value="true" />
     <option name="ADD_SOURCE_ROOTS" value="true" />
     <EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
-    <option name="SCRIPT_NAME" value="C:\Users\Robin\NoSyncDokumente\sourceobsw\tmtc\OBSW_TmTcClient.py" />
-    <option name="PARAMETERS" value="-m 2 -c 1" />
+    <option name="SCRIPT_NAME" value="$PROJECT_DIR$/OBSW_TmTcClient.py" />
+    <option name="PARAMETERS" value="-m 2 -c 1 --COM COM9" />
     <option name="SHOW_COMMAND_LINE" value="false" />
     <option name="EMULATE_TERMINAL" value="false" />
     <option name="MODULE_MODE" value="false" />
diff --git a/OBSW_TmTcClient.py b/OBSW_TmTcClient.py
index 8994970..0cbaa95 100644
--- a/OBSW_TmTcClient.py
+++ b/OBSW_TmTcClient.py
@@ -74,7 +74,6 @@ from comIF.OBSW_Serial_ComIF import SerialComIF
 def main():
     args = parseInputArguments()
     setGlobals(args)
-    setUpSocket()
     atexit.register(g.keyboardInterruptHandler)
     tmtcPrinter = TmtcPrinter(g.displayMode, g.printToFile, True)
     communicationInterface = setCommunicationInterface(tmtcPrinter)
@@ -121,7 +120,7 @@ def main():
 def commandPreparation():
     # Direct command which triggers an additional step reply and one completion reply
     # Single Command Testing
-    # command = PUSTelecommand(service=17, subservice=1, SSC=21)
+    #command = PUSTelecommand(service=17, subservice=1, SSC=21)
     file = bytearray([1, 2, 3, 4, 5])
     command = PUSTelecommand(service=23, subservice=1, SSC=21, data=file)
     command.packCommandTuple()
@@ -143,6 +142,7 @@ def setUpSocket():
 
 def setCommunicationInterface(tmtcPrinter):
     if g.comIF == 0:
+        setUpSocket()
         communicationInterface = EthernetComIF(tmtcPrinter, g.tmTimeout, g.tcSendTimeoutFactor,
                                                g.sockSend, g.sockReceive, g.sendAddress)
     else:
diff --git a/comIF/OBSW_Serial_ComIF.py b/comIF/OBSW_Serial_ComIF.py
index f612dd3..b428aff 100644
--- a/comIF/OBSW_Serial_ComIF.py
+++ b/comIF/OBSW_Serial_ComIF.py
@@ -10,7 +10,11 @@ class SerialComIF(CommunicationInterface):
     def __init__(self, tmtcPrinter, comPort, baudRate, serialTimeout):
         super().__init__(tmtcPrinter)
         self.tmtcPrinter = tmtcPrinter
-        self.serial = serial.Serial(port=comPort, baudrate=baudRate, timeout=serialTimeout)
+        try:
+            self.serial = serial.Serial(port=comPort, baudrate=baudRate, timeout=serialTimeout)
+        except serial.SerialException:
+            print("Serial Port can not be opened")
+            exit()
         self.data = bytearray()
         self.numberOfPackets = 0
         # self.serial.open()
diff --git a/tc/OBSW_TcService3.py b/tc/OBSW_TcService3.py
index 777d288..19d18e3 100644
--- a/tc/OBSW_TcService3.py
+++ b/tc/OBSW_TcService3.py
@@ -38,28 +38,30 @@ def packService3TestInto(tcQueue):
     tcQueue.put(("print", "\r\nTesting Service 3: Enable custom definition"))
     command = PUSTelecommand(service=3, subservice=5, SSC=3030, data=sid1)
     tcQueue.put(command.packCommandTuple())
-    # enable custom diag definition
-    tcQueue.put(("print", "\r\nTesting Service 3: Enable custom diagnostics definition"))
-    command = PUSTelecommand(service=3, subservice=7, SSC=3030, data=sid1)
-    tcQueue.put(command.packCommandTuple())
-    # enable gps0
-    tcQueue.put(("print", "\r\nTesting Service 3: Enable GPS definition"))
-    command = PUSTelecommand(service=3, subservice=5, SSC=3030, data=sidGps)
-    tcQueue.put(command.packCommandTuple())
+    # # enable custom diag definition
+    # tcQueue.put(("print", "\r\nTesting Service 3: Enable custom diagnostics definition"))
+    # command = PUSTelecommand(service=3, subservice=7, SSC=3030, data=sid1)
+    # tcQueue.put(command.packCommandTuple())
+    # # enable gps0
+    # tcQueue.put(("print", "\r\nTesting Service 3: Enable GPS definition"))
+    # command = PUSTelecommand(service=3, subservice=5, SSC=3030, data=sidGps)
+    # tcQueue.put(command.packCommandTuple())
 
     # maybe wait a bit to receive at least 2 packets..
+    tcQueue.put(("wait", 8))
+
     # Disable custom hk definition
-    tcQueue.put(("print", "\r\nTesting Service 3: Disable custom definition"))
-    command = PUSTelecommand(service=3, subservice=6, SSC=3030, data=sid1)
-    tcQueue.put(command.packCommandTuple())
+    # tcQueue.put(("print", "\r\nTesting Service 3: Disable custom definition"))
+    # command = PUSTelecommand(service=3, subservice=6, SSC=3030, data=sid1)
+    # tcQueue.put(command.packCommandTuple())
     # Disable custom diag definition
-    tcQueue.put(("print", "\r\nTesting Service 3: Disable custom diagnostics definition"))
-    command = PUSTelecommand(service=3, subservice=8, SSC=3030, data=sid1)
-    tcQueue.put(command.packCommandTuple())
-    # disable gps0
-    tcQueue.put(("print", "\r\nTesting Service 3: Disable GPS definition"))
-    command = PUSTelecommand(service=3, subservice=6, SSC=3030, data=sidGps)
-    tcQueue.put(command.packCommandTuple())
+    # tcQueue.put(("print", "\r\nTesting Service 3: Disable custom diagnostics definition"))
+    # command = PUSTelecommand(service=3, subservice=8, SSC=3030, data=sid1)
+    # tcQueue.put(command.packCommandTuple())
+    # # disable gps0
+    # tcQueue.put(("print", "\r\nTesting Service 3: Disable GPS definition"))
+    # command = PUSTelecommand(service=3, subservice=6, SSC=3030, data=sidGps)
+    # tcQueue.put(command.packCommandTuple())
 
     # report custom hk definition
     # report custom diag definition
diff --git a/tm/OBSW_TmPacket.py b/tm/OBSW_TmPacket.py
index 2806196..d37864d 100644
--- a/tm/OBSW_TmPacket.py
+++ b/tm/OBSW_TmPacket.py
@@ -9,6 +9,7 @@ Author: R.Mueller, S. Gaisser
 from tm.OBSW_PusTm import PUSTelemetry
 from tm.OBSW_TmService1 import Service1TM
 from tm.OBSW_TmService3 import Service3TM
+from tm.OBSW_TmService5 import Service5TM
 import struct
 # TM Packets use the generic space packet structure provided by OBSWPusPacket to generate individual
 # telemetry packets for all services
@@ -50,49 +51,6 @@ class Service2TM(PUSTelemetry):
         return
 
 
-class Service5TM(PUSTelemetry):
-    def __init__(self, byteArray):
-        super().__init__(byteArray)
-        self.printPacketInfo("Event")
-        if self.getSubservice() == 1:
-            self.appendPacketInfo(" Info")
-        elif self.getSubservice() == 2:
-            self.appendPacketInfo(" Error Low Severity")
-        elif self.getSubservice() == 3:
-            self.appendPacketInfo(" Error Med Severity")
-        elif self.getSubservice() == 4:
-            self.appendPacketInfo(" Error High Severity")
-        self.eventId = struct.unpack('>H', self.byteArrayData[0:2])[0]
-        self.objectId = struct.unpack('>I', self.byteArrayData[2:6])[0]
-        self.param1 = struct.unpack('>I', self.byteArrayData[6:10])[0]
-        self.param2 = struct.unpack('>I', self.byteArrayData[10:14])[0]
-        
-    def printTelemetryHeader(self, array):
-        super().printTelemetryHeader(array)
-        array.append(str(self.eventId))
-        array.append(hex(self.objectId))
-        array.append(str(self.param1))
-        array.append(str(self.param2))
-        
-    def printTelemetryColumnHeaders(self, array):
-        super().printTelemetryColumnHeaders(array)
-        array.append("Event ID")
-        array.append("Reporter ID")
-        array.append("Parameter 1")
-        array.append("Parameter 2")
-
-    def packTmInformation(self):
-        tmInformation = super().packTmInformation()
-        addInformation = {
-            "RID": self.objectId,
-            "EventID": self.eventId,
-            "Param1": self.param1,
-            "Param2": self.param2
-        }
-        tmInformation.update(addInformation)
-        return tmInformation
-
-
 class Service8TM(PUSTelemetry):
     def __init__(self, byteArray):
         super().__init__(byteArray)
diff --git a/tm/OBSW_TmService1.py b/tm/OBSW_TmService1.py
index 88119e3..969aaa1 100644
--- a/tm/OBSW_TmService1.py
+++ b/tm/OBSW_TmService1.py
@@ -30,7 +30,7 @@ class Service1TM(PUSTelemetry):
                 self.errorParam1 = struct.unpack('>I', self.byteArrayData[7:11])[0]
                 self.errorParam2 = struct.unpack('>I', self.byteArrayData[11:15])[0]
             else:
-                # self.printData()
+                self.printData()
                 self.ErrCode = struct.unpack('>H', self.byteArrayData[4:6])[0]
                 self.errorParam1 = struct.unpack('>I', self.byteArrayData[6:10])[0]
                 self.errorParam2 = struct.unpack('>I', self.byteArrayData[10:14])[0]
@@ -48,8 +48,8 @@ class Service1TM(PUSTelemetry):
             if self.isStep:
                 array.append(str(self.stepNumber))
             array.append(str(hex(self.ErrCode)))
-            array.append(str(hex(self.errorParam1)))
-            array.append(str(hex(self.errorParam2)))
+            array.append(str(hex(self.errorParam1)) + ", " + str(self.errorParam1))
+            array.append(str(hex(self.errorParam2)) + ", " + str(self.errorParam2))
         elif self.isStep:
             array.append(str(self.stepNumber))
 
@@ -77,4 +77,4 @@ class Service1TM(PUSTelemetry):
             tmInformation.update({"errCode": self.ErrCode})
         if self.isStep:
             tmInformation.update({"stepNumber": self.ErrCode})
-        return tmInformation
\ No newline at end of file
+        return tmInformation
diff --git a/tm/OBSW_TmService3.py b/tm/OBSW_TmService3.py
index c40b1af..a3c9073 100644
--- a/tm/OBSW_TmService3.py
+++ b/tm/OBSW_TmService3.py
@@ -20,7 +20,7 @@ class Service3TM(PUSTelemetry):
         if self.getSubservice() == 25:
             self.paramLength = len(self.byteArrayData) - 4
         # TODO: This can be automated by using the MIB parser pool names and pool datatypes
-        if self.sid == 0x1f00 or 0x2f00:
+        if self.sid == 0x1f00 or self.sid == 0x2f00:
             self.hkHeader = ["Fix Mode", "SV in Fix", "GNSS Week", "Time of Week", "Latitude", "Longitude",
                              "Mean Sea Altitude", "Position X", "Position Y", "Position Z",
                              "Velocity X", "Velocity Y", "Velocity Z"]
@@ -50,6 +50,20 @@ class Service3TM(PUSTelemetry):
             self.hkContent.append(self.vx)
             self.hkContent.append(self.vy)
             self.hkContent.append(self.vz)
+        elif self.sid == 0x4300:
+            self.hkHeader = ["Bool", "UINT8", "UINT16", "UINT32", "FLOAT1", "FLOAT2"]
+            self.testBool = self.byteArrayData[4]
+            self.hkContent.append(self.testBool)
+            self.testUint8 = self.byteArrayData[5]
+            self.hkContent.append(self.testUint8)
+            self.testUint16 = (self.byteArrayData[6] << 8) | self.byteArrayData[7]
+            self.hkContent.append(self.testUint16)
+            self.testUint32 = struct.unpack('>I', self.byteArrayData[7:11])[0]
+            self.hkContent.append(self.testUint32)
+            self.floatVector1 = struct.unpack('>f', self.byteArrayData[11:15])[0]
+            self.hkContent.append(self.floatVector1)
+            self.floatVector2 = struct.unpack('>f', self.byteArrayData[15:19])[0]
+            self.hkContent.append(self.floatVector2)
 
     def printTelemetryHeader(self, array):
         super().printTelemetryHeader(array)
@@ -60,5 +74,5 @@ class Service3TM(PUSTelemetry):
     def printTelemetryColumnHeaders(self, array):
         super().printTelemetryColumnHeaders(array)
         array.append("SID")
-        array.append("Number of Parameters")
+        array.append("HK Data Size")
         return
diff --git a/tm/OBSW_TmService5.py b/tm/OBSW_TmService5.py
new file mode 100644
index 0000000..8dd25e2
--- /dev/null
+++ b/tm/OBSW_TmService5.py
@@ -0,0 +1,53 @@
+# -*- coding: utf-8 -*-
+"""
+Program: OBSW_TmService5.py
+Date: 30.12.2019
+Description: Deserialize PUS Event Report
+Author: R. Mueller
+"""
+
+from tm.OBSW_PusTm import PUSTelemetry
+import struct
+
+
+class Service5TM(PUSTelemetry):
+    def __init__(self, byteArray):
+        super().__init__(byteArray)
+        self.printPacketInfo("Event")
+        if self.getSubservice() == 1:
+            self.appendPacketInfo(" Info")
+        elif self.getSubservice() == 2:
+            self.appendPacketInfo(" Error Low Severity")
+        elif self.getSubservice() == 3:
+            self.appendPacketInfo(" Error Med Severity")
+        elif self.getSubservice() == 4:
+            self.appendPacketInfo(" Error High Severity")
+        self.eventId = struct.unpack('>H', self.byteArrayData[0:2])[0]
+        self.objectId = struct.unpack('>I', self.byteArrayData[2:6])[0]
+        self.param1 = struct.unpack('>I', self.byteArrayData[6:10])[0]
+        self.param2 = struct.unpack('>I', self.byteArrayData[10:14])[0]
+
+    def printTelemetryHeader(self, array):
+        super().printTelemetryHeader(array)
+        array.append(str(self.eventId))
+        array.append(hex(self.objectId))
+        array.append(str(hex(self.param1)) + ", " + str(self.param1))
+        array.append(str(hex(self.param2)) + ", " + str(self.param2))
+
+    def printTelemetryColumnHeaders(self, array):
+        super().printTelemetryColumnHeaders(array)
+        array.append("Event ID")
+        array.append("Reporter ID")
+        array.append("Parameter 1")
+        array.append("Parameter 2")
+
+    def packTmInformation(self):
+        tmInformation = super().packTmInformation()
+        addInformation = {
+            "RID": self.objectId,
+            "EventID": self.eventId,
+            "Param1": self.param1,
+            "Param2": self.param2
+        }
+        tmInformation.update(addInformation)
+        return tmInformation
diff --git a/utility/OBSW_TmTcPrinter.py b/utility/OBSW_TmTcPrinter.py
index 88e0f8d..297c6c9 100644
--- a/utility/OBSW_TmTcPrinter.py
+++ b/utility/OBSW_TmTcPrinter.py
@@ -28,7 +28,7 @@ class TmtcPrinter:
             self.handleLongPrint(packet)
         self.handleWiretappingPacket(packet)
         self.handleDataReplyPacket(packet)
-        if packet.getService == 3 and packet.getSubservice == 25 or 26:
+        if packet.getService == 3 and (packet.getSubservice == 25 or packet.getSubservice == 26):
             self.handleHkPrint(packet)
         if g.printRawTmData:
             self.printBuffer = "TM Data:" + "\n" + self.returnDataString(packet.data)
-- 
GitLab