DD2-monitor/serial/t.c

93 lines
1.9 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;
char ligne[200];
2019-01-06 02:15:48 +11:00
float datas[4];
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 -> %d\n", count, foo);
fprintf(stderr, "%s\n", ligne);
}
2019-01-06 02:15:48 +11:00
foo = parse4values(ligne, 'X', datas);
2019-01-05 21:47:51 +11:00
//
if (foo >= 0) {
temps = time(NULL);
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);
}
2019-01-06 02:15:48 +11:00
if (verbosity) fprintf(stderr, "\n");
2018-12-22 03:53:17 +11:00
}
2019-01-05 21:47:51 +11:00
return 0;
}
/* ---------------------------------------------------------------- */
int main (int argc, char *argv[])
{
int serial_in;
char *device;
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;
device = "/dev/ttyACM0";
2018-12-29 19:45:22 +11:00
2019-01-05 21:47:51 +11:00
while ((opt = getopt(argc, argv, "d:n:v")) != -1) {
switch (opt) {
case 'v': verbosity++; break;
case 'n': nbre = atoi(optarg); break;
case 'd': device = optarg; break;
2018-12-29 19:45:22 +11:00
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-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
/* ---------------------------------------------------------------- */