Étape 3: Le programme hôte
J’ai branché le câble USB dans un Pi de framboise, mais en principe tout * nix ordinateur avec le pilote USB-to-serial approprié n’opérera.
Le programme d’accueil est très simple. C’est simplement un conduit du port série directement vers syslog.
syslog est un excellent choix, car elle sera timestamp les lignes et conservez-les dans des fichiers journaux pivoté. Vous suffit de comparer les lignes de sortie dans /var/log/syslog et agréger la sortie comme vous le souhaitez.
#include <stdio.h> #include <string.h> #include <syslog.h> #include <termios.h> #include <unistd.h> int main(int argc, char **argv) { daemon(0, 0); openlog(argv[0], LOG_PERROR, LOG_USER); FILE *port = fopen("/dev/ttyUSB0", "r+"); if (port == NULL) { perror("Error opening port"); return 1; } struct termios t; if (tcgetattr(fileno(port), &t)) { perror("Error getting termios"); return 1; } cfsetspeed(&t, B9600); cfmakeraw(&t); if (tcsetattr(fileno(port), 0, &t)) { perror("Error setting speed"); return 1; } while(1) { char buf[1024]; if (fgets(buf, sizeof(buf), port) == NULL) break; while (buf[strlen(buf) - 1] == '\015' || buf[strlen(buf) - 1] == '\012') buf[strlen(buf) - 1] = 0; syslog(LOG_INFO, "Line monitor reports %s", buf); } }