DD2-monitor/serial/t.c

114 lines
2.4 KiB
C
Raw Normal View History

2018-12-29 12:09:34 +01:00
/*
* Experiments with the serial input
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
2018-12-12 16:06:18 +01:00
#include <stdio.h>
2018-12-20 11:57:23 +01:00
#include <stdlib.h>
2018-12-29 09:45:22 +01:00
#include <strings.h>
#include <ctype.h>
2018-12-12 22:26:54 +01:00
#include <time.h>
2018-12-21 17:53:17 +01:00
#include <unistd.h> //Used for UART
#include <fcntl.h> //Used for UART
#include <errno.h>
2018-12-29 09:45:22 +01:00
#include <termios.h> //Used for UART
2019-01-05 11:47:51 +01:00
#include <getopt.h>
2018-12-12 16:06:18 +01:00
#include "serial.h"
2018-12-20 11:57:23 +01:00
int verbosity;
/* ---------------------------------------------------------------- */
2019-01-05 11:47:51 +01:00
int loop(int sfd, int iters)
2018-12-12 16:06:18 +01:00
{
2019-01-05 11:47:51 +01:00
int count, foo;
long temps;
char ligne[200];
2019-01-05 16:15:48 +01:00
float datas[4];
2019-02-22 16:02:01 +01:00
struct tm *p_tms;
2018-12-12 16:06:18 +01:00
2019-01-05 11:47:51 +01:00
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); */
2019-01-05 11:47:51 +01:00
fprintf(stderr, "%s\n", ligne);
}
2019-02-22 17:08:16 +01:00
//
if (verbosity > 1)
{
p_tms = localtime(&temps);
(void)strftime(ligne, 19, "%H:%M:%S", p_tms);
fprintf(stderr, "%6d %s\n", count, ligne);
}
foo = parse4values(ligne, 'T', datas);
2019-01-05 11:47:51 +01:00
//
if (foo >= 0) {
temps = time(NULL);
2019-01-28 14:42:33 +01:00
values2temperature(datas);
2019-01-05 16:15:48 +01:00
printf("%ld %f %f %f %f\n", temps,
datas[0], datas[1], datas[2], datas[3]);
2019-01-05 11:47:51 +01:00
fflush(stdout);
}
else {
fprintf(stderr, "%s: parse -> %d\n", __func__, foo);
}
2018-12-21 17:53:17 +01:00
}
2019-01-05 11:47:51 +01:00
return 0;
}
/* ---------------------------------------------------------------- */
2019-02-22 15:55:50 +01:00
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.");
}
/* ---------------------------------------------------------------- */
2019-01-05 11:47:51 +01:00
int main (int argc, char *argv[])
{
int serial_in;
2019-01-14 03:20:54 +01:00
char *device = "/dev/ttyACM0";
2019-01-05 11:47:51 +01:00
int nbre, speed, opt;
2018-12-21 17:53:17 +01:00
2019-01-05 11:47:51 +01:00
/* set some default values */
verbosity = 0;
nbre = 25;
speed = 9600;
2018-12-29 09:45:22 +01:00
2019-02-22 15:55:50 +01:00
while ((opt = getopt(argc, argv, "d:n:vh")) != -1) {
2019-01-05 11:47:51 +01:00
switch (opt) {
case 'v': verbosity++; break;
case 'n': nbre = atoi(optarg); break;
case 'd': device = optarg; break;
2019-02-22 15:55:50 +01:00
case 'h': help(0); exit(0);
2019-01-05 11:47:51 +01:00
default:
fprintf(stderr, "%s : uh ?", argv[0]);
exit(1);
break;
2018-12-20 11:57:23 +01:00
}
2019-01-05 11:47:51 +01:00
2018-12-12 16:06:18 +01:00
}
2019-01-28 14:42:33 +01:00
if (verbosity) {
fprintf(stderr, "Testing Serial Software - compiled " \
__DATE__ " " __TIME__ "\n");
}
2019-01-05 11:47:51 +01:00
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);
2018-12-12 16:06:18 +01:00
return 0;
}
2018-12-21 17:53:17 +01:00
2018-12-29 12:09:34 +01:00
/* ---------------------------------------------------------------- */