1 | #include <stdio.h>
|
2 | #include <string>
|
3 | #include <unistd.h>
|
4 | #include <fcntl.h>
|
5 | #include <errno.h>
|
6 | #include <stdlib.h>
|
7 | #include <termios.h>
|
8 | #include <unistd.h>
|
9 | #include <sys/ioctl.h>
|
10 | #include <sys/time.h>
|
11 | #include <sys/types.h>
|
12 | #include <iostream>
|
13 | #include <fstream>
|
14 | #include <sys/select.h>
|
15 |
|
16 | using namespace std;
|
17 |
|
18 | int open_input_source(int);
|
19 | int read_input_source(int, int);
|
20 |
|
21 | int main() {
|
22 | int fd; // Var für Port
|
23 | fd_set readfs; // file desciptor für select-Subroutine
|
24 | int loop=1;
|
25 | int res, errorNpb = 0;
|
26 | struct timeval Timeout; // Konstrukt für Time-Out der select-Subroutine
|
27 | int port=0; // Port-Auswahl --> hier /dev/ttyS0
|
28 |
|
29 | ofstream npbLogFile("FILE_OUT.LOG",ios_base::trunc);
|
30 | if (npbLogFile.fail()) {
|
31 | cout << "\nFehler! Log-Datei konnte nicht erzeugt werden!\n" <<
|
32 | "Programmabruch!\n";
|
33 | exit(1);
|
34 | }
|
35 | npbLogFile.close();
|
36 |
|
37 | ofstream npbOutFile("FILE_OUT.001",ios_base::trunc);
|
38 | if (npbLogFile.fail()) {
|
39 |
|
40 | cout << "Programmabruch!\n";
|
41 |
|
42 | ofstream npbLogFile("FILE_OUT.LOG",ios_base::app);
|
43 | npbLogFile << "time" << " Programmabruch!\n";
|
44 | npbLogFile.close();
|
45 |
|
46 | exit(1);
|
47 | }
|
48 | npbOutFile.close();
|
49 |
|
50 |
|
51 | /* Serielle Schnittstelle öffnen & konfigurieren */
|
52 | fd = open_input_source(port);
|
53 |
|
54 | /* Schleife zum einlesen von Daten an der Schnittstelle */
|
55 | while (loop) {
|
56 |
|
57 |
|
58 | Timeout.tv_usec = 0; // Millisekunden
|
59 | Timeout.tv_sec = 3; // Sekungen
|
60 |
|
61 | /* Zeitstempel (YYYYMMDDHHMMSS) für LOG-Datei-Werte erzeugen und in Sting 'strTimestamp' ablegen */
|
62 | time_t Zeitstempel;
|
63 | struct tm *ltime;
|
64 | char strTimestamp[15];
|
65 | Zeitstempel = time(0);
|
66 | ltime = localtime(&Zeitstempel);
|
67 | strftime(strTimestamp, 15, "%Y%m%d%H%M%S", ltime);
|
68 |
|
69 | FD_ZERO(&readfs); // geht
|
70 | FD_SET(fd, &readfs); // <-- Segmentation fault
|
71 |
|
72 | res = select(fd+1, &readfs, NULL, NULL, &Timeout);
|
73 |
|
74 | if (res == 0) {
|
75 | cout << "\nZeitüberschreitung! Keine Input-Daten an seriellen Port /dev/ttyS" << port <<
|
76 | " vorhanden!\n";
|
77 |
|
78 | ofstream npbLogFile("FILE_OUT.LOG",ios_base::app);
|
79 | npbLogFile << strTimestamp << " Zeitüberschreitung! Keine Input-Daten an seriellen Port /dev/ttyS"
|
80 | << port << " vorhanden!\n";
|
81 | npbLogFile.close();
|
82 | }
|
83 |
|
84 | if (FD_ISSET(fd, &readfs)) {
|
85 | errorNpb = read_input_source(fd, errorNpb);
|
86 |
|
87 | if (errorNpb = 0)
|
88 | sleep(10);
|
89 | else if (errorNpb > 3) {
|
90 | cout << "\nFehler beim einlesenen!\n";
|
91 |
|
92 | ofstream npbLogFile("FILE_OUT.LOG",ios_base::app);
|
93 | npbLogFile << strTimestamp << " Fehler beim einlesenen!\n";
|
94 | npbLogFile.close();
|
95 | }
|
96 | }
|
97 | }
|
98 |
|
99 | close(fd); // serielle Schnittstelle schließen
|
100 | return 0;
|
101 | }
|
102 |
|
103 | /***********************************************************************************/
|
104 | /* Funktion zum einlesen der Daten hab ich raus genommen, soweit kommt er ja nicht */
|
105 | /***********************************************************************************/
|
106 |
|
107 |
|
108 | int open_input_source(int port) {
|
109 | int fd;
|
110 | struct termios options;
|
111 |
|
112 | /* Zeitstempel (YYYYMMDDHHMMSS) für LOG-Datei-Werte erzeugen und in Sting 'strTimestamp' ablegen */
|
113 | time_t Zeitstempel;
|
114 | struct tm *ltime;
|
115 | char strTimestamp[15];
|
116 | Zeitstempel = time(0);
|
117 | ltime = localtime(&Zeitstempel);
|
118 | strftime(strTimestamp, 15, "%Y%m%d%H%M%S", ltime);
|
119 |
|
120 | /* seriellen Port öffnen */
|
121 | switch (port) {
|
122 | case 0: fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY); break;
|
123 | case 1: fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NDELAY); break;
|
124 | case 2: fd = open("/dev/ttyS2", O_RDWR | O_NOCTTY | O_NDELAY); break;
|
125 | case 3: fd = open("/dev/ttyS3", O_RDWR | O_NOCTTY | O_NDELAY); break;
|
126 | }
|
127 |
|
128 | /* wenn serieller Port nicht geöffnet werden konnte, dann Fehlermeldung, Log-Datei-Ausgabe und Programmabruch! */
|
129 | if (fd == -1) {
|
130 | cout << "\nFehler! Programmabruch!\n";
|
131 |
|
132 | ofstream npbLogFile("FILE_OUT.LOG",ios_base::app);
|
133 | npbLogFile << strTimestamp << " Fehler! Programmabruch!\n";
|
134 | npbLogFile.close();
|
135 | exit(1);
|
136 | }
|
137 |
|
138 | /* wenn serieller Port geöffnet, dann Port konfigurieren */
|
139 | else if (fd >= 0) {
|
140 | fcntl(fd, F_SETFL, 0);
|
141 |
|
142 |
|
143 | if (tcgetattr(fd, &options) != 0) {
|
144 | cout << "\nFehler! Programmabruch!\n";
|
145 |
|
146 | ofstream npbLogFile("FILE_OUT.LOG",ios_base::app);
|
147 | npbLogFile << strTimestamp << " Fehler! Programmabruch!\n";
|
148 | npbLogFile.close();
|
149 |
|
150 | exit(1);
|
151 | }
|
152 |
|
153 | cfsetspeed(&options, B9600); // Geschwindigkeit: 9600 Baud
|
154 | options.c_cflag &= ~PARENB; // kein Paritybit
|
155 | options.c_cflag &= ~CSTOPB; // 1 Stopbit
|
156 | options.c_cflag &= ~CSIZE;
|
157 | options.c_cflag |= CS8; // 8 Datenbits
|
158 | options.c_cflag |= (CLOCAL | CREAD); // CD-Signal ignorieren
|
159 | options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // nicht Zeilenbasiert, kein Echo, keine Steuerzeichen,
|
160 | // keine Interrupts
|
161 | options.c_oflag &= ~OPOST; // Setze RAW-Output
|
162 | options.c_cc[VMIN] = 0; // Warten auf min. 0 Zeichen beim lesen
|
163 | options.c_cc[VTIME] = 0; // Timeout beim lesen "read()" nicht setzen
|
164 |
|
165 |
|
166 | tcflush(fd, TCIOFLUSH);
|
167 |
|
168 | if (tcsetattr(fd, TCSAFLUSH, &options) != 0) {
|
169 | cout << "\nFehler! Programmabruch!\n";
|
170 |
|
171 | ofstream npbLogFile("FILE_OUT.LOG",ios_base::app);
|
172 | npbLogFile << strTimestamp <<
|
173 | " Fehler! Programmabruch!\n";
|
174 | npbLogFile.close();
|
175 |
|
176 | exit(1);
|
177 | }
|
178 |
|
179 | fcntl(fd, F_SETFL, FNDELAY);
|
180 | fcntl(fd, F_SETFL, 0);
|
181 |
|
182 | return fd;
|
183 | }
|
184 | }
|