workaround the infamous select() bug : first milestone, demo code is coming

This commit is contained in:
tth 2018-12-29 17:07:23 +01:00
parent 560edb14e8
commit 6aa2a26bec
7 changed files with 65 additions and 18 deletions

3
.gitignore vendored
View File

@ -12,4 +12,7 @@ doc/*.idx
doc/*.ilg doc/*.ilg
doc/*.ind doc/*.ind
*/foo.dat

View File

@ -4,6 +4,9 @@ OPT = -Wall -DDEBUG_LEVEL=1
serial.o: serial.c serial.h Makefile serial.o: serial.c serial.h Makefile
gcc ${OPT} -c $< gcc ${OPT} -c $<
t: t.c serial.o Makefile funcs.o: funcs.c serial.h Makefile
gcc ${OPT} $< serial.o -o $@ gcc ${OPT} -c $<
t: t.c serial.o funcs.o Makefile
gcc ${OPT} $< serial.o funcs.o -o $@

26
serial/funcs.c Normal file
View File

@ -0,0 +1,26 @@
#include <stdio.h>
#include "serial.h"
extern int verbosity;
/* ---------------------------------------------------------------- */
int parseXvalue(char *line, char cflag)
{
int value, foo;
int vrd[4];
value=0;
foo = sscanf(data+1, "%d %d %d %d", vrd, vrd+1, vrd+2, vrd+3);
fprintf(stderr, "sscanf -> %d\n", foo);
if (4 != foo) {
return -666;
}
for (foo=0; foo<4; foo++) {
value += vrd[foo];
}
return value;
}
/* ---------------------------------------------------------------- */

View File

@ -148,7 +148,7 @@ FD_SET (fd, &rfds);
retval = select(1, &rfds, NULL, NULL, &timeout); retval = select(1, &rfds, NULL, NULL, &timeout);
#if DEBUG_LEVEL #if DEBUG_LEVEL
fprintf(stderr, "%s : select on %d -> %d\n", __func__, fd, retval); fprintf(stderr, "%s : select on fd %d -> %d\n", __func__, fd, retval);
#endif #endif
switch (retval) { switch (retval) {
@ -187,12 +187,22 @@ int getline_to(int fd, char *where, int szm, int to_ms)
{ {
int curpos, byte, retval; int curpos, byte, retval;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %d %p %d %d )\n", __func__,
fd, where, szm, to_ms);
#endif
curpos = 0; curpos = 0;
retval = -7; /* magic number powa */ retval = -7; /* magic number powa */
where[0] = '\0'; /* erase all the bs */ where[0] = '\0'; /* erase all the bs */
for(;;) { for(;;) {
if (to_ms) {
byte = getbyte_to (fd, to_ms); byte = getbyte_to (fd, to_ms);
}
else {
byte = getbyte(fd);
}
if (byte < 0) { if (byte < 0) {
fprintf(stderr, "%s : somthing is wrong %d\n", fprintf(stderr, "%s : somthing is wrong %d\n",
@ -204,6 +214,7 @@ for(;;) {
if ('\n' == byte) { /* got an EOL ? */ if ('\n' == byte) { /* got an EOL ? */
where[curpos] = '\0'; where[curpos] = '\0';
retval = curpos; retval = curpos;
break;
} }
if (curpos < szm) { /* ya de la place */ if (curpos < szm) { /* ya de la place */

View File

@ -14,4 +14,8 @@ int getbyte_to (int fd, int to_ms);
int getline_to(int fd, char *where, int szm, int to_ms); int getline_to(int fd, char *where, int szm, int to_ms);
/* auxiliary and test functions */
int parseXvalue(char *asciidatas, char id);

View File

@ -17,10 +17,12 @@
int verbosity; int verbosity;
/* ---------------------------------------------------------------- */
/* ---------------------------------------------------------------- */ /* ---------------------------------------------------------------- */
int main (int argc, char *argv[]) int main (int argc, char *argv[])
{ {
int serial_in, foo; int serial_in, foo, count;
char ligne[200];
if (2 != argc) { if (2 != argc) {
fprintf(stderr, "give me a device name, please.\n"); fprintf(stderr, "give me a device name, please.\n");
@ -31,17 +33,15 @@ serial_in = prepare_UART(argv[1], 9600);
fprintf(stderr, "going to listen on %d\n", serial_in); fprintf(stderr, "going to listen on %d\n", serial_in);
for (;;) { for (count=0; count<10000; count++) {
foo = getbyte_to(serial_in, 50000); foo = getline_to(serial_in, ligne, 100, 0);
if (foo < 0) { fprintf(stderr, "getline #%d -> %d\n", count, foo);
fprintf(stderr, "get byte : got %d, err is %d\n", fprintf(stderr, "%s\n", ligne);
foo, errno); foo = parseXvalue(ligne);
} fprintf(stderr, "parse -> %d\n", foo);
else { if (foo>= 0) {
printf("%9ld $%02x ", time(NULL), foo); printf("%d %d\n", count, foo);
if (isprint(foo)) putchar(foo); fflush(stdout);
puts("");
if ('\n'==foo) puts("");
} }
} }

View File

@ -26,7 +26,7 @@ void updatevalues(void)
values[foo] += (foo + 1); values[foo] += (foo + 1);
} }
if (values[foo] > 1023) { if (values[foo] > 1023) {
values[foo] = rand()%15; values[foo] = rand()%25;
} }
} }
} }
@ -48,7 +48,7 @@ void sendvalues(void)
void loop() { void loop() {
updatevalues(); updatevalues();
sendvalues(); sendvalues();
delay(2500); delay(800);
} }
/* -------------------------------------------------- */ /* -------------------------------------------------- */