From a83804c9cdd9602fb5653ccfb3908d6d8ff42623 Mon Sep 17 00:00:00 2001
From: aschwinn <al.schwinn@gsi.de>
Date: Thu, 14 Sep 2017 14:48:42 +0200
Subject: [PATCH] added missing demo-files

---
 .../src/demo/api_demo_modbus.cpp              | 70 ++++++++++++++++
 .../src/demo/api_demo_snap7.cpp               | 82 +++++++++++++++++++
 2 files changed, 152 insertions(+)
 create mode 100644 silecs-communication-cpp/src/demo/api_demo_modbus.cpp
 create mode 100644 silecs-communication-cpp/src/demo/api_demo_snap7.cpp

diff --git a/silecs-communication-cpp/src/demo/api_demo_modbus.cpp b/silecs-communication-cpp/src/demo/api_demo_modbus.cpp
new file mode 100644
index 0000000..146c119
--- /dev/null
+++ b/silecs-communication-cpp/src/demo/api_demo_modbus.cpp
@@ -0,0 +1,70 @@
+#include <silecs-communication/interface/utility/SilecsLog.h>
+#include <silecs-communication/interface/utility/StringUtilities.h>
+#include <silecs-communication/interface/communication/SilecsConnection.h>
+#include <silecs-communication/interface/equipment/SilecsPLC.h>
+#include <silecs-communication/interface/communication/MBConnection.h>
+#include <silecs-communication/interface/utility/SilecsException.h>
+#include <silecs-communication/protocol/modbus/iemdb.h>
+
+#include <iostream>
+
+int main(void)
+{
+    Silecs::Service* pService = NULL;
+    Silecs::Cluster* pCluster  = NULL;
+    modbus_t *readCtx;
+    modbus_t *writeCtx;
+    uint16_t reg[64];
+    int rc, i;
+
+    memset(reg, 0, sizeof(reg));
+    try
+    {
+        // Instantiate the singleton of the SILECS Service
+
+        pService = Silecs::Service::getInstance();
+        pCluster = pService->getCluster("BK9Design", "1.0.0");
+
+        // Retrieve the PLC related to the current FESA device
+
+        // (from 'plcHostName' FESA field defined on that purpose).
+
+        Silecs::PLC* pPLC = pCluster->getPLC("cfp-774-ctwincat-mb-03");
+
+        //pPLC->connect(/*synchroMode=*/Silecs::NO_SYNCHRO, /*connectNow=*/false);
+
+        readCtx = modbus_new_tcp((char *) pPLC->getIPAddress().c_str(), MODBUS_TCP_DEFAULT_PORT /*=502*/);
+        writeCtx = modbus_new_tcp((char *) pPLC->getIPAddress().c_str(), MODBUS_TCP_DEFAULT_PORT /*=502*/);
+        modbus_connect(readCtx);
+        modbus_connect(writeCtx);
+
+
+        rc = modbus_read_input_registers(readCtx, 24, 1, reg);
+
+        std::cout << "Reading input from address 0" << std::endl;
+
+        if (rc == -1) {
+            fprintf(stderr, "%s\n", modbus_strerror(errno));
+            return -1;
+        }
+
+
+        for (i=0; i < rc; i++) {
+            printf("reg[%d]=%d (0x%X)\n", i, reg[i], reg[i]);
+        }
+
+        modbus_close(readCtx);
+        modbus_close(writeCtx);
+        modbus_free(readCtx);
+        modbus_free(writeCtx);
+
+        pService->deleteInstance();
+   }
+
+   catch (const Silecs::SilecsException& ex)
+   {
+       throw Silecs::SilecsException(__FILE__, __LINE__, ex.getMessage());
+   }
+
+   pService->deleteInstance();
+}
diff --git a/silecs-communication-cpp/src/demo/api_demo_snap7.cpp b/silecs-communication-cpp/src/demo/api_demo_snap7.cpp
new file mode 100644
index 0000000..39d92bb
--- /dev/null
+++ b/silecs-communication-cpp/src/demo/api_demo_snap7.cpp
@@ -0,0 +1,82 @@
+#include <silecs-communication/interface/core/SilecsService.h>
+#include <silecs-communication/interface/equipment/SilecsCluster.h>
+#include <silecs-communication/interface/utility/SilecsLog.h>
+#include <silecs-communication/interface/utility/StringUtilities.h>
+#include <silecs-communication/interface/communication/SilecsConnection.h>
+#include <silecs-communication/interface/equipment/SilecsPLC.h>
+#include <silecs-communication/interface/communication/SNAP7Connection.h>
+#include <silecs-communication/interface/utility/SilecsException.h>
+#include <silecs-communication/protocol/core/silecs.h>
+
+#include <iostream>
+#include <numeric>
+#include <vector>
+#include <string>
+
+int main(int argc, char **argv)
+
+{
+    Silecs::Service* pService = NULL;
+    Silecs::Cluster* pCluster  = NULL;
+    int rackNb = 0;
+    int slotNb = 2;
+    uint8_t EBbuffer[10];
+    uint8_t ABbuffer[10];
+
+    memset(EBbuffer, 0, sizeof(EBbuffer));
+
+    try
+
+    {
+        // Instantiate the singleton of the SILECS Service
+
+        pService = Silecs::Service::getInstance();
+
+        pService->setArguments(string(argv[1]));
+        pCluster = pService->getCluster("SilecsValid", "1.1.0");
+
+        cout << string(argv[1]) << endl;
+
+        // Retrieve the PLC related to the current FESA device
+
+        // (from 'plcHostName' FESA field defined on that purpose).
+
+        Silecs::PLC* pPLC = pCluster->getPLC("cfp-774-csimatic-s7-01");
+
+        //pPLC->connect(/*synchroMode=*/Silecs::NO_SYNCHRO, /*connectNow=*/false);
+
+       // S7Object recvClient = Cli_Create();
+       // Cli_ConnectTo(recvClient, pPLC->getIPAddress().c_str(), rackNb, slotNb);
+
+
+       // Cli_EBRead(recvClient, 72, 1, EBbuffer);
+
+       // std::cout << std::hex << EBbuffer[0] << std::endl;
+
+       // Cli_ABRead(recvClient, 72, 1, EBbuffer);
+
+       // std::cout << std::hex << EBbuffer[0] << std::endl;
+
+       // ABbuffer[0] = EBbuffer[0] + 1;
+
+       // Cli_ABWrite(recvClient, 72, 1, ABbuffer);
+
+       // std::cout << std::hex << ABbuffer[0] << std::endl;
+
+       // Cli_EBRead(recvClient, 72, 1, EBbuffer);
+
+       // std::cout << std::hex << EBbuffer[0] << std::endl;
+
+       // Cli_Disconnect(recvClient);
+	   // Cli_Destroy(&recvClient);
+
+        pService->deleteInstance();
+   }
+
+   catch (const Silecs::SilecsException& ex)
+   {
+       throw Silecs::SilecsException(__FILE__, __LINE__, ex.getMessage());
+   }
+
+   pService->deleteInstance();
+}
-- 
GitLab