Feature #777 » maintenance.c
| 1 |
#include <iostream>
|
|---|---|
| 2 |
#include <fcntl.h>
|
| 3 |
#include <unistd.h>
|
| 4 |
#include <termios.h>
|
| 5 |
#include <cstring>
|
| 6 |
|
| 7 |
int main() { |
| 8 |
const char* device = "/dev/ttyUSB0"; |
| 9 |
|
| 10 |
// UART nur zum Schreiben öffnen
|
| 11 |
int fd = open(device, O_WRONLY | O_NOCTTY); |
| 12 |
if (fd < 0) { |
| 13 |
std::cerr << "Fehler beim Öffnen von " << device << ": " |
| 14 |
<< strerror(errno) << "\n"; |
| 15 |
return 1; |
| 16 |
}
|
| 17 |
|
| 18 |
// Terminal-Attribute holen
|
| 19 |
termios tty{}; |
| 20 |
if (tcgetattr(fd, &tty) != 0) { |
| 21 |
std::cerr << "tcgetattr fehlgeschlagen: " << strerror(errno) << "\n"; |
| 22 |
close(fd); |
| 23 |
return 1; |
| 24 |
}
|
| 25 |
|
| 26 |
// Terminal konfigurieren (RAW-Modus)
|
| 27 |
cfmakeraw(&tty); |
| 28 |
|
| 29 |
// Baudrate setzen (115200 Beispiel)
|
| 30 |
cfsetospeed(&tty, B115200); |
| 31 |
cfsetispeed(&tty, B115200); |
| 32 |
|
| 33 |
// Einstellungen übernehmen
|
| 34 |
if (tcsetattr(fd, TCSANOW, &tty) != 0) { |
| 35 |
std::cerr << "tcsetattr fehlgeschlagen: " << strerror(errno) << "\n"; |
| 36 |
close(fd); |
| 37 |
return 1; |
| 38 |
}
|
| 39 |
|
| 40 |
std::cout << "UART geöffnet. Sende Daten...\n"; |
| 41 |
|
| 42 |
// Beispiel: alle 2 Sekunden einen Befehl senden
|
| 43 |
while (true) { |
| 44 |
const char* cmd = "STATUS?\r\n"; |
| 45 |
ssize_t written = write(fd, cmd, strlen(cmd)); |
| 46 |
|
| 47 |
if (written < 0) { |
| 48 |
std::cerr << "Fehler beim Schreiben: " << strerror(errno) << "\n"; |
| 49 |
} else { |
| 50 |
std::cout << "Gesendet: " << cmd; |
| 51 |
}
|
| 52 |
|
| 53 |
usleep(2'000'000); // 2 Sekunden warten |
| 54 |
}
|
| 55 |
|
| 56 |
close(fd); |
| 57 |
return 0; |
| 58 |
}
|