DD2-monitor/serial/t.c

194 lines
4.1 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
2019-07-31 06:20:33 +11:00
/**********************************************************************
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
**********************************************************************/
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-04-28 06:44:48 +11: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-28 06:44:48 +11: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 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-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++) {
2019-04-28 06:44:48 +11:00
//
2019-01-05 21:47:51 +11:00
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-04-28 06:44:48 +11: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 21:47:51 +11:00
}
2019-04-28 06:44:48 +11:00
if (foo) {
fprintf(stderr, "%s : error %d after switch\n",
__func__, foo);
2019-01-05 21:47:51 +11:00
}
2019-04-28 06:44:48 +11:00
if (verbosity) puts("");
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.");
2019-05-16 21:28:47 +11:00
puts("\t-K\tset the K parameter.");
2019-02-23 01:55:50 +11:00
puts("\t-n\tnumber of records to grab.");
2019-05-16 21:28:47 +11:00
puts("\t-t\tselect the function");
2019-02-23 01:55:50 +11:00
puts("\t-v\tincrease verbosity.");
}
/* ---------------------------------------------------------------- */
2019-01-05 21:47:51 +11:00
int main (int argc, char *argv[])
{
int serial_in;
2019-05-16 21:28:47 +11:00
char *device = "/dev/ttyS0";
int nbre, speed, opt, K=0;
int param, retv;
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;
2019-05-16 21:28:47 +11:00
param = 1;
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) {
2019-05-16 21:28:47 +11:00
case 'p': param = atoi(optarg); break;
2019-01-05 21:47:51 +11:00
case 'v': verbosity++; break;
2019-05-16 21:28:47 +11:00
case 'K': K = atoi(optarg); break;
2019-01-05 21:47:51 +11:00
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);
2019-05-16 21:28:47 +11:00
switch (param) {
case 0:
retv = loop(serial_in, nbre);
break;
case 1:
2019-05-19 03:01:05 +11:00
retv = -1;
2019-05-16 21:28:47 +11:00
break;
default:
2019-05-19 03:01:05 +11:00
retv = -2;
2019-05-16 21:28:47 +11:00
break;
}
fprintf(stderr, "Returned value is %d\n", retv);
2019-01-05 21:47:51 +11:00
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
/* ---------------------------------------------------------------- */