From 082656860496cb449969c3fdc9542174d78839b2 Mon Sep 17 00:00:00 2001 From: tth Date: Sat, 5 Jan 2019 16:15:48 +0100 Subject: [PATCH] reading 4 values from arduino --- .gitignore | 2 +- serial/essai.sh | 27 +++++++++++++++++++++------ serial/funcs.c | 23 +++++++++++++++++++++-- serial/serial.c | 4 +++- serial/t.c | 7 +++++-- simulator/send-random/send-random.ino | 3 ++- 6 files changed, 53 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 4052870..d3701dc 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,6 @@ doc/*.ind */*.dat rrdb/*.png - +serial/*.png diff --git a/serial/essai.sh b/serial/essai.sh index 3056834..0222503 100755 --- a/serial/essai.sh +++ b/serial/essai.sh @@ -1,10 +1,25 @@ #!/bin/bash DEVICE="/dev/ttyACM0" +DATAFILE="foo.dat" +IMAGE="graphe.png" +NB_READ=1000 + +./t -n ${NB_READ} -d ${DEVICE} | tee -a ${DATAFILE} + +gnuplot << __EOC__ +set term png size 1024,512 +set output "${IMAGE}" +set grid +set xdata time +set timefmt "%s" +set format x "%H:%M:S" +plot "${DATAFILE}" using 1:2 title "foo" with lines, \ + "${DATAFILE}" using 1:3 title "bar" with lines, \ + "${DATAFILE}" using 1:4 title "quux" with lines, \ + "${DATAFILE}" using 1:5 title "booz" with lines +__EOC__ + +# display ${IMAGE} + -for p in {0..15} -do - echo pass $p - ./t ${DEVICE} | tee -a foo.dat - sleep 8 -done diff --git a/serial/funcs.c b/serial/funcs.c index 99c6ae1..6bbd45a 100644 --- a/serial/funcs.c +++ b/serial/funcs.c @@ -1,5 +1,6 @@ #include +#include #include "serial.h" @@ -15,7 +16,7 @@ value=0; if ( cflag != *line ) { if (verbosity) { - fprintf(stderr, "%s line[0] 0x%x bad\n", + fprintf(stderr, "%s : line[0] 0x%x bad\n", __func__, *line); } return -777; @@ -36,8 +37,26 @@ return value; /* ---------------------------------------------------------------- */ int parse4values(char *line, char cflag, float array[4]) { +float ftmp[4]; +int foo; +if ( cflag != *line ) { + if (verbosity) { + fprintf(stderr, "%s : line[0] 0x%x bad\n", + __func__, *line); + } + return -777; + } -return -777; +foo = sscanf(line+1, "%f %f %f %f", ftmp, ftmp+1, ftmp+2, ftmp+3); +if (4 != foo) { + fprintf(stderr, "%s : sscanf -> %d\n", __func__, foo); + return -666; + } +// fprintf(stderr, "\tV %f\n", ftmp[0]); + +memcpy(array, ftmp, 4*sizeof(float)); + +return 4; } /* ---------------------------------------------------------------- */ diff --git a/serial/serial.c b/serial/serial.c index f6df3f4..34c79f5 100644 --- a/serial/serial.c +++ b/serial/serial.c @@ -41,7 +41,7 @@ int baudbits; struct termios options; #if DEBUG_LEVEL -fprintf(stderr, "%s ( %s %d )\n", __func__, port, baudrate); +fprintf(stderr, ">>> %s ( %s %d )\n", __func__, port, baudrate); #endif // OPEN THE UART // The flags (defined in fcntl.h): @@ -103,6 +103,8 @@ options.c_cc[VMIN] = 1; /* ask for blocking read */ tcflush(uart0, TCIFLUSH); tcsetattr(uart0, TCSANOW, &options); +tcflush(uart0, TCIFLUSH); /* do it again, sam */ + return uart0; } /* -------------------------------------------------------------------- */ diff --git a/serial/t.c b/serial/t.c index 27cb32f..ed45298 100644 --- a/serial/t.c +++ b/serial/t.c @@ -24,6 +24,7 @@ int loop(int sfd, int iters) int count, foo; long temps; char ligne[200]; +float datas[4]; for (count=0; count %d\n", count, foo); fprintf(stderr, "%s\n", ligne); } - foo = parseXvalue(ligne, 'X'); + foo = parse4values(ligne, 'X', datas); // if (foo >= 0) { temps = time(NULL); - printf("%ld %d\n", temps, foo); + 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) fprintf(stderr, "\n"); } return 0; } diff --git a/simulator/send-random/send-random.ino b/simulator/send-random/send-random.ino index 8496b28..a937b7b 100644 --- a/simulator/send-random/send-random.ino +++ b/simulator/send-random/send-random.ino @@ -5,6 +5,7 @@ /* -------------------------------------------------- */ #define NBVAL 4 +#define DELAI 1664 int values[NBVAL]; @@ -48,7 +49,7 @@ void sendvalues(void) void loop() { updatevalues(); sendvalues(); - delay(800); + delay(DELAI); } /* -------------------------------------------------- */