Browse Source

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

tth 1 year ago
parent
commit
6aa2a26bec
7 changed files with 65 additions and 18 deletions
  1. 3
    0
      .gitignore
  2. 5
    2
      serial/Makefile
  3. 26
    0
      serial/funcs.c
  4. 13
    2
      serial/serial.c
  5. 4
    0
      serial/serial.h
  6. 12
    12
      serial/t.c
  7. 2
    2
      simulator/send-random/send-random.ino

+ 3
- 0
.gitignore View File

@@ -12,4 +12,7 @@ doc/*.idx
12 12
 doc/*.ilg
13 13
 doc/*.ind
14 14
 
15
+*/foo.dat
16
+
17
+
15 18
 

+ 5
- 2
serial/Makefile View File

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

+ 26
- 0
serial/funcs.c View File

@@ -0,0 +1,26 @@
1
+
2
+#include  <stdio.h>
3
+
4
+#include  "serial.h"
5
+
6
+extern int	verbosity;
7
+
8
+/* ---------------------------------------------------------------- */
9
+int parseXvalue(char *line, char cflag)
10
+{
11
+int	value, foo;
12
+int	vrd[4];
13
+
14
+value=0;
15
+
16
+foo = sscanf(data+1, "%d %d %d %d", vrd, vrd+1, vrd+2, vrd+3);
17
+fprintf(stderr, "sscanf -> %d\n", foo);
18
+if (4 != foo) {
19
+	return -666;
20
+	}
21
+for (foo=0; foo<4; foo++) {
22
+	value += vrd[foo];
23
+	}
24
+return value;
25
+}
26
+/* ---------------------------------------------------------------- */

+ 13
- 2
serial/serial.c View File

@@ -148,7 +148,7 @@ FD_SET    (fd, &rfds);
148 148
 
149 149
 retval = select(1, &rfds, NULL, NULL, &timeout);
150 150
 #if DEBUG_LEVEL
151
-fprintf(stderr, "%s : select on %d -> %d\n", __func__, fd, retval);
151
+fprintf(stderr, "%s : select on fd %d -> %d\n", __func__, fd, retval);
152 152
 #endif
153 153
 
154 154
 switch (retval) {
@@ -187,12 +187,22 @@ int getline_to(int fd, char *where, int szm, int to_ms)
187 187
 {
188 188
 int	curpos, byte, retval;
189 189
 
190
+#if DEBUG_LEVEL
191
+fprintf(stderr, ">>> %s ( %d %p %d %d )\n", __func__,
192
+				fd, where, szm, to_ms);
193
+#endif
194
+
190 195
 curpos = 0;
191 196
 retval = -7;					/* magic number powa */
192 197
 where[0] = '\0';				/* erase all the bs  */
193 198
 
194 199
 for(;;)	{
195
-	byte = getbyte_to (fd, to_ms);
200
+	if (to_ms) {
201
+		byte = getbyte_to (fd, to_ms);
202
+		}
203
+	else	{
204
+		byte = getbyte(fd);
205
+		}
196 206
 
197 207
 	if (byte < 0) {
198 208
 		fprintf(stderr, "%s : somthing is wrong %d\n", 
@@ -204,6 +214,7 @@ for(;;)	{
204 214
 	if ('\n' == byte) {			/* got an EOL ? */
205 215
 		where[curpos] = '\0';
206 216
 		retval = curpos;
217
+		break;
207 218
 		}
208 219
 
209 220
 	if (curpos < szm) {			/* ya de la place */

+ 4
- 0
serial/serial.h View File

@@ -14,4 +14,8 @@ int getbyte_to (int fd, int to_ms);
14 14
 
15 15
 int getline_to(int fd, char *where, int szm, int to_ms);
16 16
 
17
+/* auxiliary and test functions */
18
+
19
+int parseXvalue(char *asciidatas, char id);
20
+
17 21
 

+ 12
- 12
serial/t.c View File

@@ -17,10 +17,12 @@
17 17
 
18 18
 int	verbosity;
19 19
 
20
+/* ---------------------------------------------------------------- */
20 21
 /* ---------------------------------------------------------------- */
21 22
 int main (int argc, char *argv[])
22 23
 {
23
-int		serial_in, foo;
24
+int		serial_in, foo, count;
25
+char		ligne[200];
24 26
 
25 27
 if (2 != argc) {
26 28
 	fprintf(stderr, "give me a device name, please.\n");
@@ -31,17 +33,15 @@ serial_in = prepare_UART(argv[1], 9600);
31 33
 
32 34
 fprintf(stderr, "going to listen on %d\n", serial_in);
33 35
 
34
-for (;;) {
35
-	foo = getbyte_to(serial_in, 50000);
36
-	if (foo < 0) {
37
-		fprintf(stderr, "get byte : got %d, err is %d\n",
38
-						foo, errno);
39
-		}
40
-	else	{
41
-		printf("%9ld    $%02x  ", time(NULL), foo);
42
-		if (isprint(foo))	putchar(foo);
43
-		puts("");
44
-		if ('\n'==foo) 	puts("");
36
+for (count=0; count<10000; count++) {
37
+	foo = getline_to(serial_in, ligne, 100, 0);
38
+	fprintf(stderr, "getline #%d -> %d\n", count, foo);
39
+	fprintf(stderr, "%s\n", ligne);
40
+	foo = parseXvalue(ligne);
41
+	fprintf(stderr, "parse -> %d\n", foo);
42
+	if (foo>= 0) {
43
+		printf("%d %d\n", count, foo);
44
+		fflush(stdout);
45 45
 		}
46 46
 	}
47 47
 

+ 2
- 2
simulator/send-random/send-random.ino View File

@@ -26,7 +26,7 @@ void updatevalues(void)
26 26
       values[foo] += (foo + 1);
27 27
       }
28 28
     if (values[foo] > 1023) {
29
-      values[foo] = rand()%15;
29
+      values[foo] = rand()%25;
30 30
       }
31 31
   }
32 32
 }
@@ -48,7 +48,7 @@ void sendvalues(void)
48 48
 void loop() {
49 49
   updatevalues();
50 50
   sendvalues();
51
-  delay(2500);
51
+  delay(800);
52 52
 }
53 53
 
54 54
 /* -------------------------------------------------- */

Loading…
Cancel
Save