DD2-monitor/serial/t.c

178 lines
3.5 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-04-27 21:44:48 +02:00
/* ---------------------------------------------------------------- */
int read_temps(char *buff, int k)
{
float datas[4];
int raw[4], foo;
long temps;
foo = parse4_Ivalues(buff, 'T', raw);
//
if (foo >= 0) {
temps = time(NULL);
values2temperature(raw, 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;
}
/* ---------------------------------------------------------------- */
2019-04-27 21:44:48 +02:00
int read_light(char *buff, int k)
{
int val;
if (1 == sscanf(buff+1, "%d", &val)) {
printf("--------- %04X ----------\n", val);
return 0;
}
return -3;
}
/* ---------------------------------------------------------------- */
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;
2019-02-22 17:25:24 +01:00
char ligne[200], buff[200];
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++) {
2019-04-27 21:44:48 +02:00
//
2019-01-05 11:47:51 +01:00
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);
2019-02-22 17:25:24 +01:00
(void)strftime(buff, 19, "%H:%M:%S", p_tms);
fprintf(stderr, "%s %6d / %d\n", buff, count, iters);
2019-02-22 17:08:16 +01:00
}
2019-04-27 21:44:48 +02:00
foo = 0;
switch (ligne[0]) {
case 'L':
foo = read_light(ligne, 0);
break;
case 'T':
foo = read_temps(ligne, 0);
break;
default:
fprintf(stderr, "*** WTF '%s' ?\n", ligne);
foo = -1;
break;
2019-01-05 11:47:51 +01:00
}
2019-04-27 21:44:48 +02:00
if (foo) {
fprintf(stderr, "%s : error %d after switch\n",
__func__, foo);
2019-01-05 11:47:51 +01:00
}
2019-04-27 21:44:48 +02:00
if (verbosity) puts("");
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.");
2019-05-16 12:28:47 +02:00
puts("\t-K\tset the K parameter.");
2019-02-22 15:55:50 +01:00
puts("\t-n\tnumber of records to grab.");
2019-05-16 12:28:47 +02:00
puts("\t-t\tselect the function");
2019-02-22 15:55:50 +01:00
puts("\t-v\tincrease verbosity.");
}
/* ---------------------------------------------------------------- */
2019-01-05 11:47:51 +01:00
int main (int argc, char *argv[])
{
int serial_in;
2019-05-16 12:28:47 +02:00
char *device = "/dev/ttyS0";
int nbre, speed, opt, K=0;
int param, retv;
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;
2019-05-16 12:28:47 +02:00
param = 1;
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) {
2019-05-16 12:28:47 +02:00
case 'p': param = atoi(optarg); break;
2019-01-05 11:47:51 +01:00
case 'v': verbosity++; break;
2019-05-16 12:28:47 +02:00
case 'K': K = atoi(optarg); break;
2019-01-05 11:47:51 +01:00
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);
2019-05-16 12:28:47 +02:00
switch (param) {
case 0:
retv = loop(serial_in, nbre);
break;
case 1:
2019-05-18 18:01:05 +02:00
retv = -1;
2019-05-16 12:28:47 +02:00
break;
default:
2019-05-18 18:01:05 +02:00
retv = -2;
2019-05-16 12:28:47 +02:00
break;
}
fprintf(stderr, "Returned value is %d\n", retv);
2019-01-05 11:47:51 +01:00
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
/* ---------------------------------------------------------------- */