-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
96 lines (78 loc) · 2.37 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <string>
#include <csignal>
#include <cstring>
#include <cstdlib>
#include "Plc.h"
#include "TeranisTask.h"
#include "MModbusTCPServer.h"
#include "OpcuaServer.h"
#include "opcua_configuration.h"
#if defined(ENV_MRAA)
#include "PlcIoMraa.h"
#elif defined(ENV_REVPI)
#include "PlcIoRevPi.h"
#elif defined(ENV_WINDOWS)
#warning No I/O implementation in this environment yet
#else
#warning Unknown Environment
#endif
bool terminate = false;
void signalSigterm(int signum) {
terminate = true;
}
int main(int argc, char *argv[]) {
try {
signal(SIGTERM, signalSigterm);
if(std::getenv("TERANIS_PLC_STOP_ON_SIGINT") != nullptr && std::string(std::getenv("TERANIS_PLC_STOP_ON_SIGINT")) == "true") {
// quit on CTRL+C in tty
signal(SIGINT, signalSigterm);
}
TMModbusTCPServer modbusTcpServer;
OpcuaServer opcuaServer;
// TERANIS Code laden
PlcTask *task = new TeranisTask();
task->setMinCycleSetMs(1);
task->setMaxCycleSetMs(250); // sollte größer als plc.getCooldownCycleTime() sein
// PLC vorbereiten
PlcIo *io;
#if defined(ENV_MRAA)
io = new PlcIoMraa();
#elif defined(ENV_REVPI)
io = new PlcIoRevPi();
#elif defined(ENV_WINDOWS)
io = new PlcIo(); //TODO: where to map i/o here?
#else
io = new PlcIo();
#endif
Plc plc(io);
plc.setCooldownCycleTime(50);
plc.setTask(task);
// simple Argumentpruefung
if (argc == 3 && strcmp(argv[1], "-mbport") == 0) modbusTcpServer.setMbport(atoi(argv[2]));
modbusTcpServer.begin();
plc.begin();
configureOpcuaServer(&opcuaServer);
std::set<OpcuaVariable*> opcuaVariables;
configureOpcuaVariables(opcuaVariables);
opcuaServer.addVariables(opcuaVariables);
opcuaServer.begin();
// PLC-Zyklus so lange ausfuehren bis SIGTERM eintrifft, oder Exception
while (!terminate) {
plc.cycle();
modbusTcpServer.run();
opcuaServer.cycle();
}
plc.end();
modbusTcpServer.end();
opcuaServer.end();
return 0;
}
catch(const std::exception& e) {
std::cerr << e.what() << std::endl;
}
catch (...) {
std::cerr << " Es ist ein unbekannter Fehler aufgetreten." << std::endl;
return 1;
}
}