DD2-monitor/serial/t.c

115 lines
2.4 KiB
C

/*
* Experiments with the serial input
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <ctype.h>
#include <time.h>
#include <unistd.h> //Used for UART
#include <fcntl.h> //Used for UART
#include <errno.h>
#include <termios.h> //Used for UART
#include <getopt.h>
#include "serial.h"
int verbosity;
/* ---------------------------------------------------------------- */
int loop(int sfd, int iters)
{
int count, foo;
long temps;
char ligne[200], buff[200];
float datas[4];
struct tm *p_tms;
for (count=0; count<iters; count++) {
foo = getline_to(sfd, ligne, 100, 0);
//
if (verbosity) {
/* fprintf(stderr, "getline #%d on %d -> %d\n",
count, iters, foo); */
fprintf(stderr, "%s\n", ligne);
}
//
if (verbosity > 1)
{
p_tms = localtime(&temps);
(void)strftime(buff, 19, "%H:%M:%S", p_tms);
fprintf(stderr, "%s %6d / %d\n", buff, count, iters);
}
//
foo = parse4values(ligne, 'T', datas);
//
if (foo >= 0) {
temps = time(NULL);
values2temperature(datas);
printf("%ld %f %f %f %f\n", temps,
datas[0], datas[1], datas[2], datas[3]);
fflush(stdout);
}
else {
fprintf(stderr, "%s: parse -> %d\n", __func__, foo);
}
}
return 0;
}
/* ---------------------------------------------------------------- */
void help(int k)
{
puts("options : ");
puts("\t-d\tserial device to read.");
puts("\t-n\tnumber of records to grab.");
puts("\t-v\tincrease verbosity.");
}
/* ---------------------------------------------------------------- */
int main (int argc, char *argv[])
{
int serial_in;
char *device = "/dev/ttyACM0";
int nbre, speed, opt;
/* set some default values */
verbosity = 0;
nbre = 25;
speed = 9600;
while ((opt = getopt(argc, argv, "d:n:vh")) != -1) {
switch (opt) {
case 'v': verbosity++; break;
case 'n': nbre = atoi(optarg); break;
case 'd': device = optarg; break;
case 'h': help(0); exit(0);
default:
fprintf(stderr, "%s : uh ?", argv[0]);
exit(1);
break;
}
}
if (verbosity) {
fprintf(stderr, "Testing Serial Software - compiled " \
__DATE__ " " __TIME__ "\n");
}
serial_in = prepare_UART(device, speed);
if (serial_in < 0) {
fprintf(stderr, "%s : open device : error %d on %s\n",
argv[0], serial_in, device);
exit(1);
}
fprintf(stderr, "going to listen on %d\n", serial_in);
(void)loop(serial_in, nbre);
return 0;
}
/* ---------------------------------------------------------------- */