Skip to content

Commit 83138a9

Browse files
authored
Create main.cpp
1 parent a3986db commit 83138a9

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

main.cpp

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#include <iostream>
2+
#include "snap7.h"
3+
#include "s7.h"
4+
#include <unistd.h>
5+
#include <cstdint>
6+
#include <cstring>
7+
#include <sys/socket.h>
8+
#include <netinet/in.h>
9+
#include <stdio.h>
10+
#include <ctime>
11+
12+
#define PORT 2000
13+
14+
using namespace std;
15+
16+
TS7Client* Client;
17+
int Rack = 0;
18+
int Slot = 1;
19+
int DB_NUMBER = 6;
20+
int timerOutput = 3000;
21+
const char* address = "192.168.0.150";
22+
uint8_t myDB6[10];
23+
24+
void plc_connect(){
25+
Client = new TS7Client;
26+
Client->ConnectTo(address, Rack,Slot);
27+
}
28+
29+
void plc_disconnect(){
30+
Client->Disconnect();
31+
delete Client;
32+
}
33+
34+
void leerdatoDB()
35+
{
36+
Client->DBRead(DB_NUMBER, 0, 2, &myDB6);
37+
int tres = S7_GetIntAt(myDB6, 0);
38+
cout << "Alarma Anterior: " << tres << endl;
39+
}
40+
41+
void escribirDB(char a)
42+
{
43+
S7_SetIntAt(myDB6, 0, a);
44+
Client->DBWrite(DB_NUMBER, 0, 2, &myDB6);
45+
cout << "Alarma al PLC: " << a << endl;
46+
cout << endl;
47+
}
48+
49+
char encuentra_mensaje(char *b)
50+
{
51+
std::string texto = b;
52+
size_t inicio = texto.find("<UserString>");
53+
size_t fin = texto.find("</UserString>");
54+
const char* m;
55+
56+
if (inicio != std::string::npos && fin != std::string::npos)
57+
m = texto.substr(inicio, fin - inicio).c_str();
58+
else
59+
std::cout << "No se encontró la etiqueta <UserString>" << std::endl;
60+
61+
return m[12];
62+
}
63+
64+
char recibeTCP()
65+
{
66+
int serverSocket;
67+
int clientSocket;
68+
char men;
69+
sockaddr_in serverAddr;
70+
serverAddr.sin_family = AF_INET;
71+
serverAddr.sin_addr.s_addr = INADDR_ANY;
72+
serverAddr.sin_port = htons(PORT);
73+
74+
serverSocket = socket(AF_INET, SOCK_STREAM, 0);
75+
bind(serverSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
76+
listen(serverSocket, 5);
77+
std::cout << "Esperando Alarmas en el puerto " << PORT << std::endl;
78+
while(1)
79+
{
80+
clientSocket = accept(serverSocket, nullptr, nullptr);
81+
char buffer[1024] = {0};
82+
recv(clientSocket, buffer, sizeof(buffer) - 1, 0);
83+
men = encuentra_mensaje(buffer);
84+
85+
std::cout << "Alarma Symphony: " << men << " ";
86+
std::time_t tiempoActual = std::time(nullptr);
87+
std::tm* tiempoLocal = std::localtime(&tiempoActual);
88+
std::cout << " : " << std::asctime(tiempoLocal);
89+
90+
plc_connect();
91+
escribirDB(men);
92+
plc_disconnect();
93+
close(clientSocket);
94+
}
95+
close(serverSocket);
96+
97+
return men;
98+
}

0 commit comments

Comments
 (0)