98 lines
2.0 KiB
C
98 lines
2.0 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];
|
|
float datas[4];
|
|
|
|
for (count=0; count<iters; count++) {
|
|
foo = getline_to(sfd, ligne, 100, 0);
|
|
//
|
|
if (verbosity > 1) {
|
|
fprintf(stderr, "getline #%d -> %d\n", count, foo);
|
|
fprintf(stderr, "%s\n", ligne);
|
|
}
|
|
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);
|
|
}
|
|
if (verbosity > 1) fprintf(stderr, "\n");
|
|
}
|
|
return 0;
|
|
}
|
|
/* ---------------------------------------------------------------- */
|
|
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:v")) != -1) {
|
|
switch (opt) {
|
|
case 'v': verbosity++; break;
|
|
case 'n': nbre = atoi(optarg); break;
|
|
case 'd': device = optarg; break;
|
|
|
|
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;
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- */
|