Browse Source

blocking read is not blocking

tth 1 year ago
parent
commit
bb10042aae
6 changed files with 115 additions and 26 deletions
  1. 5
    0
      README.md
  2. 6
    0
      serial/README.md
  3. 30
    15
      serial/serial.c
  4. 1
    1
      serial/serial.h
  5. 18
    10
      serial/t.c
  6. 55
    0
      simulator/send-random/send-random.ino

+ 5
- 0
README.md View File

@@ -12,6 +12,11 @@ Pour en savoir plus sur ce passionnant projet, il y a le canal IRC
12 12
 `#tetalab` sur le réseau Freenode et/ou les rencontres du mercredi
13 13
 soir au DD2, à Mixart-Myrys.
14 14
 
15
+# WTF status
16
+
17
+Le `read` bloquant ne bloque pas. Ça craint grave.
18
+
19
+
15 20
 
16 21
 
17 22
 

+ 6
- 0
serial/README.md View File

@@ -22,6 +22,12 @@ par le professeur Cispeo.
22 22
 
23 23
 Un petit exemple ?
24 24
 
25
+Oui, voilà. À ce jour (20 déc. 2018), on va dire que ça ne marche pas.
26
+Il faut dire que les `serial devices` ont toujours étés un peu le
27
+domaine de la magie noire. Mais quand même, coincer sur un `read` qui
28
+ne bloque pas, c'est un peu ironique.
29
+ 
30
+
25 31
 
26 32
 
27 33
 

+ 30
- 15
serial/serial.c View File

@@ -1,7 +1,9 @@
1 1
 
2 2
 #include <stdio.h>
3 3
 #include  <stdlib.h>
4
+#include  <errno.h>
4 5
 #include  <sys/select.h>
6
+#include  <string.h>
5 7
 #include <unistd.h>			//Used for UART
6 8
 #include <fcntl.h>			//Used for UART
7 9
 #include <termios.h>		//Used for UART
@@ -60,12 +62,14 @@ fprintf(stderr, "%s ( %s %d )\n", __func__, port, baudrate);
60 62
 // shall not cause the terminal device to become the controlling terminal
61 63
 // for the process.
62 64
 
63
-uart0 = open(port, O_RDWR | O_NOCTTY);
64
-if (uart0== -1)
65
+uart0 = open(port, O_RDONLY | O_NOCTTY);
66
+if (uart0 < 0)
65 67
 	{
66 68
 	perror("unable to open uart ");
67 69
 	return -1;
68 70
 	}
71
+
72
+return uart0;		/* WTF ??? */
69 73
 	
70 74
 // CONFIGURE THE UART
71 75
 // The flags defined in /usr/include/termios.h -
@@ -87,6 +91,9 @@ baudbits = baudrate2const(baudrate);
87 91
 #if DEBUG_LEVEL
88 92
 fprintf(stderr, "%d -> 0x%04x\n", baudrate, baudbits);
89 93
 #endif
94
+
95
+memset(&options, 0, sizeof(options));
96
+
90 97
 tcgetattr(uart0, &options);
91 98
 options.c_cflag = baudbits | CS8 | CLOCAL | CREAD;	//<Set baud rate
92 99
 options.c_iflag = IGNPAR;
@@ -100,16 +107,20 @@ return uart0;
100 107
 /* -------------------------------------------------------------------- */
101 108
 /*
102 109
  *	this function have NO timeout !
110
+ *	blocking read is not blocking, wtf ?
103 111
  */
104 112
 int getbyte(int fd)
105 113
 {
106 114
 unsigned char   byte;
107 115
 int             foo;
108 116
 
117
+byte = 0;
118
+
109 119
 foo = read(fd, &byte, 1);
110 120
 if (1 != foo)
111 121
         {
112
-        perror("read a byte");
122
+	fprintf(stderr, "byte %d rd %d errno %d\n",
123
+					byte, foo, errno);
113 124
         return -1;
114 125
         }
115 126
 return (int)byte;
@@ -126,9 +137,8 @@ int		retval;
126 137
 
127 138
 timeout.tv_sec  = to_ms / 1000;
128 139
 timeout.tv_usec = (to_ms % 1000) * 1000;
129
-
130 140
 #if DEBUG_LEVEL
131
-fprintf(stderr, "timeout %6d is %4ld %6ld\n", to_ms,
141
+fprintf(stderr, "timeout %6d is %4ld.%6ld\n", to_ms,
132 142
 				timeout.tv_sec, timeout.tv_usec);
133 143
 #endif
134 144
 
@@ -136,38 +146,37 @@ FD_ZERO   (&rfds);
136 146
 FD_SET    (fd, &rfds);
137 147
 
138 148
 retval = select(1, &rfds, NULL, NULL, &timeout);
139
-
140 149
 #if DEBUG_LEVEL
141 150
 fprintf(stderr, "%s : select -> %d\n", __func__, retval);
142 151
 #endif
143 152
 
144
-retval = retval < 0 ? -1 : retval;
145
-
146 153
 switch (retval) {
147 154
 
148 155
 	case -1:
149 156
 		fprintf(stderr, "omg ?\n");
150
-		byte = -1;
157
+		retval = -1;
151 158
 		break;
152 159
 
153 160
 	case 0:
154 161
 		fprintf(stderr, "timeout\n");
155
-		byte = -99;
162
+		retval = -99;
156 163
 		break;
157 164
 
158 165
 	default:
166
+		fprintf(stderr, "%s default -> %d\n", __func__, retval);
159 167
 		if (retval==fd) {
160 168
 			read(fd, &byte, 1);
161 169
 			fprintf(stderr, "got 0x%02x\n", byte);
170
+			retval = byte;
162 171
 			}
163 172
 		else	{
164 173
 			fprintf(stderr, "%d bad fd ?\n", retval);
165
-			byte = -3;
174
+			retval = -3;
166 175
 			}
167 176
 		break;
168 177
 	}
169 178
 
170
-return byte;
179
+return retval;
171 180
 }
172 181
 /* -------------------------------------------------------------------- */
173 182
 /* timeout is in milliseconds */
@@ -195,15 +204,21 @@ for(;;)	{
195 204
 		retval = curpos;
196 205
 		}
197 206
 
198
-	
199
-
207
+	if (curpos < szm) {			/* ya de la place */
208
+		where[curpos] = byte;
209
+		curpos++;
210
+		}
211
+	else	{				/* oups overflow */
212
+		retval = -6;
213
+		break;
214
+		}
200 215
 
201 216
 	}
202 217
 
203 218
 fprintf(stderr, "%s -> '%s'\n", __func__, where);
204 219
 
205 220
 
206
-return -666;
221
+return retval;
207 222
 }
208 223
 /* -------------------------------------------------------------------- */
209 224
 

+ 1
- 1
serial/serial.h View File

@@ -6,7 +6,7 @@
6 6
 
7 7
 int prepare_UART(char *port, int bauds);
8 8
 
9
-int getbyte(int fd);
9
+int getbyte(int fd);			/* really brotched func */
10 10
 
11 11
 
12 12
 /* timeout is exprimed in milliseconds. */

+ 18
- 10
serial/t.c View File

@@ -1,28 +1,36 @@
1 1
 
2 2
 
3 3
 #include  <stdio.h>
4
+#include  <stdlib.h>
4 5
 #include  <time.h>
5 6
 
6 7
 #include  "serial.h"
7 8
 
9
+int	verbosity;
10
+
8 11
 int main (int argc, char *argv[])
9 12
 {
10
-int	serial_in;
11
-int	byte, foo, to;
13
+int		serial_in;
14
+int		byte;
12 15
 
13
-serial_in = prepare_UART("/dev/ttyS0", 9600);
16
+serial_in = prepare_UART("/dev/ttyACM0", 9600);
14 17
 fprintf(stderr, "prepare uart -> %d\n", serial_in);
15 18
 
16
-for (foo=0; foo<20; foo++) {
17
-	to = (foo+1) * 666;
18
-	byte = getbyte_to(serial_in, to);
19
+if (serial_in < 0) {
20
+	exit(1);
21
+	}
22
+
23
+for (;;) {
24
+
25
+	byte = getbyte(serial_in);
26
+
19 27
 	if (byte < 0) {
20 28
 		fprintf(stderr, "get byte : err is %d\n", byte);
21 29
 		}
22
-	// XXX else	{
23
-		printf("%9ld   %6d   %6d    %02x\n",
24
-				time(NULL), foo, to, byte);
25
-	// XXX	}
30
+	else	{
31
+		printf("%9ld  %02x/%d\n",
32
+			time(NULL), byte, byte);
33
+		}
26 34
 	}
27 35
 
28 36
 return 0;

+ 55
- 0
simulator/send-random/send-random.ino View File

@@ -0,0 +1,55 @@
1
+/*
2
+ *  simulateur de telemesure automate
3
+ */
4
+
5
+/* -------------------------------------------------- */
6
+
7
+#define NBVAL   4
8
+
9
+int   values[NBVAL];
10
+
11
+/* -------------------------------------------------- */
12
+void setup() {
13
+  int   foo;
14
+  Serial.begin(9600);
15
+  pinMode(LED_BUILTIN, OUTPUT);
16
+  for (foo=0; foo<NBVAL; foo++) {
17
+    values[foo] = 0;
18
+  }
19
+}
20
+/* -------------------------------------------------- */
21
+void updatevalues(void)
22
+{
23
+  int foo;
24
+  for (foo=0; foo<NBVAL; foo++) {
25
+    if (rand()%100<33) {
26
+      values[foo] += (foo + 1);
27
+      }
28
+    if (values[foo] > 1023) {
29
+      values[foo] = rand()%15;
30
+      }
31
+  }
32
+}
33
+/* -------------------------------------------------- */
34
+
35
+void sendvalues(void)
36
+{
37
+  int foo;
38
+
39
+  Serial.print("X ");
40
+  for (foo=0; foo<NBVAL; foo++) {
41
+    Serial.print(" ");
42
+    Serial.print(values[foo]);
43
+    }
44
+  Serial.print("\n");
45
+}
46
+/* -------------------------------------------------- */
47
+
48
+void loop() {
49
+  updatevalues();
50
+  sendvalues();
51
+  delay(5000);
52
+}
53
+
54
+/* -------------------------------------------------- */
55
+

Loading…
Cancel
Save