From a19969a9629799968927860abadb7c43435e4ca5 Mon Sep 17 00:00:00 2001
From: aschwinn <al.schwinn@gsi.de>
Date: Fri, 12 May 2017 13:27:45 +0200
Subject: [PATCH] Bug 1201 - Add possibility to read out PLC run-state

---
 .../interface/communication/SNAP7Connection.cpp       | 11 +++++++++++
 .../interface/communication/SNAP7Connection.h         |  2 ++
 .../interface/communication/SilecsConnection.cpp      |  7 ++++++-
 .../interface/communication/SilecsConnection.h        |  4 ++++
 .../interface/equipment/SilecsPLC.cpp                 |  5 +++++
 .../interface/equipment/SilecsPLC.h                   |  4 +++-
 6 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/silecs-communication-cpp/src/silecs-communication/interface/communication/SNAP7Connection.cpp b/silecs-communication-cpp/src/silecs-communication/interface/communication/SNAP7Connection.cpp
index ea562aa..6f91e02 100644
--- a/silecs-communication-cpp/src/silecs-communication/interface/communication/SNAP7Connection.cpp
+++ b/silecs-communication-cpp/src/silecs-communication/interface/communication/SNAP7Connection.cpp
@@ -160,6 +160,17 @@ namespace Silecs
 		return 0;
     }
 
+    bool SNAP7Connection::isRunning(PLC* thePLC)
+    {
+		UnitStatusType statusStruct;
+		this->readUnitStatus(thePLC, statusStruct);
+		switch (statusStruct.status)
+		{
+			  case S7CpuStatusRun : return true;
+			  case S7CpuStatusStop: return false;
+			  default             : throw SilecsException(__FILE__, __LINE__, UNKNOWN_ERROR,std::string("PLC Status is: UNKNOWN"));
+		}
+    }
 
     //-------------------------------------------------------------------------------------------------------------------
 	int SNAP7Connection::readData(PLC* thePLC, unsigned long DBn, unsigned long offset, unsigned long size, unsigned char* pBuffer)
diff --git a/silecs-communication-cpp/src/silecs-communication/interface/communication/SNAP7Connection.h b/silecs-communication-cpp/src/silecs-communication/interface/communication/SNAP7Connection.h
index 740dc9b..1a86cc9 100644
--- a/silecs-communication-cpp/src/silecs-communication/interface/communication/SNAP7Connection.h
+++ b/silecs-communication-cpp/src/silecs-communication/interface/communication/SNAP7Connection.h
@@ -40,6 +40,8 @@ namespace Silecs
         int readCPUInfo(PLC* thePLC, CPUInfoType& dataStruct);
         int readCPInfo(PLC* thePLC, CPInfoType& dataStruct);
 
+        bool isRunning(PLC* thePLC);
+
  		int readData(PLC* thePLC, unsigned long DBn, unsigned long offset, unsigned long size, unsigned char* pBuffer);
  		int writeData(PLC* thePLC, unsigned long DBn, unsigned long offset, unsigned long size, unsigned char* pBuffer);
 
diff --git a/silecs-communication-cpp/src/silecs-communication/interface/communication/SilecsConnection.cpp b/silecs-communication-cpp/src/silecs-communication/interface/communication/SilecsConnection.cpp
index d89db59..1cf1842 100644
--- a/silecs-communication-cpp/src/silecs-communication/interface/communication/SilecsConnection.cpp
+++ b/silecs-communication-cpp/src/silecs-communication/interface/communication/SilecsConnection.cpp
@@ -263,7 +263,12 @@ namespace Silecs
         return -1;
     }
 
-    //PERFORM COLD RESTART
+    bool Connection::isRunning(PLC* thePLC)
+    {
+        throw SilecsException(__FILE__, __LINE__, DIAG_PLC_REPORT_NOT_SUPPORTED, thePLC->getName());
+        return -1;
+    }
+
     int Connection::coldRestart(PLC* thePLC)
         {
             throw SilecsException(__FILE__, __LINE__, DIAG_PLC_REPORT_NOT_SUPPORTED, thePLC->getName());
diff --git a/silecs-communication-cpp/src/silecs-communication/interface/communication/SilecsConnection.h b/silecs-communication-cpp/src/silecs-communication/interface/communication/SilecsConnection.h
index ed9d017..0cc27ee 100644
--- a/silecs-communication-cpp/src/silecs-communication/interface/communication/SilecsConnection.h
+++ b/silecs-communication-cpp/src/silecs-communication/interface/communication/SilecsConnection.h
@@ -89,6 +89,10 @@ namespace Silecs
         virtual int readUnitStatus(PLC* thePLC, UnitStatusType& dataStruct);
         virtual int readCPUInfo(PLC* thePLC, CPUInfoType& dataStruct);
         virtual int readCPInfo(PLC* thePLC, CPInfoType& dataStruct);
+
+        // true if the "recvUnitStatus" is RUN, false otherwise. Throws exception on failure
+        virtual bool isRunning(PLC* thePLC);
+
         //SET PLC COLD RESTART
         virtual int coldRestart(PLC* thePLC);
         virtual int plcStop(PLC* thePLC);
diff --git a/silecs-communication-cpp/src/silecs-communication/interface/equipment/SilecsPLC.cpp b/silecs-communication-cpp/src/silecs-communication/interface/equipment/SilecsPLC.cpp
index 4f92638..48e00f9 100644
--- a/silecs-communication-cpp/src/silecs-communication/interface/equipment/SilecsPLC.cpp
+++ b/silecs-communication-cpp/src/silecs-communication/interface/equipment/SilecsPLC.cpp
@@ -259,6 +259,11 @@ namespace Silecs
             return getConnection()->readUnitStatus(this, dataStruct);
         }
 
+        bool PLC::isRunning()
+        {
+            return getConnection()->isRunning(this);
+        }
+
 
         int PLC::recvCPUInfo(CPUInfoType& dataStruct)
         {
diff --git a/silecs-communication-cpp/src/silecs-communication/interface/equipment/SilecsPLC.h b/silecs-communication-cpp/src/silecs-communication/interface/equipment/SilecsPLC.h
index 43f513e..b3327be 100644
--- a/silecs-communication-cpp/src/silecs-communication/interface/equipment/SilecsPLC.h
+++ b/silecs-communication-cpp/src/silecs-communication/interface/equipment/SilecsPLC.h
@@ -270,7 +270,9 @@ namespace Silecs
         int recvCPUInfo(CPUInfoType& dataStruct);
         int recvCPInfo(CPInfoType& dataStruct);
 
-        //Cold RESTART
+        // true if the "recvUnitStatus" is RUN, false otherwise. Throws exception on failure
+        bool isRunning();
+
         int sendColdRestart();
         int sendPlcStop();
         /*!
-- 
GitLab