serial/ structuration du code

This commit is contained in:
tth 2019-01-05 11:47:51 +01:00
parent 704a51d3b2
commit dd359c9f82
4 changed files with 73 additions and 17 deletions

10
serial/essai.sh Executable file
View File

@ -0,0 +1,10 @@
#!/bin/bash
DEVICE="/dev/ttyACM0"
for p in {0..15}
do
echo pass $p
./t ${DEVICE} | tee -a foo.dat
sleep 8
done

View File

@ -34,3 +34,10 @@ for (foo=0; foo<4; foo++) {
return value; return value;
} }
/* ---------------------------------------------------------------- */ /* ---------------------------------------------------------------- */
int parse4values(char *line, char cflag, float array[4])
{
return -777;
}
/* ---------------------------------------------------------------- */

View File

@ -17,5 +17,6 @@ int getline_to(int fd, char *where, int szm, int to_ms);
/* auxiliary and test functions */ /* auxiliary and test functions */
int parseXvalue(char *asciidatas, char id); int parseXvalue(char *asciidatas, char id);
int parse4values(char *line, char cflag, float array[4]);

View File

@ -12,38 +12,76 @@
#include <fcntl.h> //Used for UART #include <fcntl.h> //Used for UART
#include <errno.h> #include <errno.h>
#include <termios.h> //Used for UART #include <termios.h> //Used for UART
#include <getopt.h>
#include "serial.h" #include "serial.h"
int verbosity; int verbosity;
/* ---------------------------------------------------------------- */ /* ---------------------------------------------------------------- */
int loop(int sfd, int iters)
{
int count, foo;
long temps;
char ligne[200];
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);
}
foo = parseXvalue(ligne, 'X');
//
if (foo >= 0) {
temps = time(NULL);
printf("%ld %d\n", temps, foo);
fflush(stdout);
}
else {
fprintf(stderr, "%s: parse -> %d\n", __func__, foo);
}
}
return 0;
}
/* ---------------------------------------------------------------- */ /* ---------------------------------------------------------------- */
int main (int argc, char *argv[]) int main (int argc, char *argv[])
{ {
int serial_in, foo, count; int serial_in;
char ligne[200]; char *device;
int nbre, speed, opt;
if (2 != argc) { /* set some default values */
fprintf(stderr, "give me a device name, please.\n"); verbosity = 0;
return 2; nbre = 25;
speed = 9600;
device = "/dev/ttyACM0";
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;
} }
serial_in = prepare_UART(argv[1], 9600); }
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); fprintf(stderr, "going to listen on %d\n", serial_in);
for (count=0; count<100000; count++) { (void)loop(serial_in, nbre);
foo = getline_to(serial_in, ligne, 100, 0);
fprintf(stderr, "getline #%d -> %d\n", count, foo);
fprintf(stderr, "%s\n", ligne);
foo = parseXvalue(ligne, 'X');
// fprintf(stderr, "parse -> %d\n", foo);
if (foo>= 0) {
printf("%d %d\n", count, foo);
fflush(stdout);
}
}
return 0; return 0;
} }