DD2-monitor/serial/t.c

115 lines
2.4 KiB
C
Raw Normal View History

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