Browse Source

select in 'get_byte_to' don't work

tth 1 year ago
parent
commit
560edb14e8
2 changed files with 22 additions and 43 deletions
  1. 9
    5
      serial/serial.c
  2. 13
    38
      serial/t.c

+ 9
- 5
serial/serial.c View File

@@ -86,7 +86,7 @@ if (uart0 < 0)
86 86
 
87 87
 
88 88
 baudbits = baudrate2const(baudrate);
89
-#if DEBUG_LEVEL
89
+#if DEBUG_LEVEL > 1
90 90
 fprintf(stderr, "%d -> 0x%04x\n", baudrate, baudbits);
91 91
 #endif
92 92
 
@@ -97,6 +97,9 @@ options.c_cflag = baudbits | CS8 | CLOCAL | CREAD;
97 97
 options.c_iflag = IGNPAR;
98 98
 options.c_oflag = 0;
99 99
 options.c_lflag = 0;
100
+
101
+options.c_cc[VMIN] = 1;			/* ask for blocking read */
102
+
100 103
 tcflush(uart0, TCIFLUSH);
101 104
 tcsetattr(uart0, TCSANOW, &options);
102 105
 
@@ -133,9 +136,9 @@ struct timeval	timeout;
133 136
 fd_set 		rfds;
134 137
 int		retval;
135 138
 
136
-timeout.tv_sec  = to_ms / 1000;
139
+timeout.tv_sec  =  to_ms / 1000;
137 140
 timeout.tv_usec = (to_ms % 1000) * 1000;
138
-#if DEBUG_LEVEL
141
+#if DEBUG_LEVEL > 1
139 142
 fprintf(stderr, "timeout %6d is %4ld.%6ld\n", to_ms,
140 143
 				timeout.tv_sec, timeout.tv_usec);
141 144
 #endif
@@ -145,7 +148,7 @@ FD_SET    (fd, &rfds);
145 148
 
146 149
 retval = select(1, &rfds, NULL, NULL, &timeout);
147 150
 #if DEBUG_LEVEL
148
-fprintf(stderr, "%s : select -> %d\n", __func__, retval);
151
+fprintf(stderr, "%s : select on %d -> %d\n", __func__, fd, retval);
149 152
 #endif
150 153
 
151 154
 switch (retval) {
@@ -156,7 +159,8 @@ switch (retval) {
156 159
 		break;
157 160
 
158 161
 	case 0:
159
-		fprintf(stderr, "timeout\n");
162
+		fprintf(stderr, "timeout %ld.%ld\n",
163
+				 timeout.tv_sec, timeout.tv_usec);
160 164
 		retval = -99;
161 165
 		break;
162 166
 

+ 13
- 38
serial/t.c View File

@@ -1,4 +1,7 @@
1
-
1
+/*
2
+ *	Experiments with the serial input 
3
+ *	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
+ */
2 5
 
3 6
 #include  <stdio.h>
4 7
 #include  <stdlib.h>
@@ -14,63 +17,35 @@
14 17
 
15 18
 int	verbosity;
16 19
 
17
-/* ----------------------------------------------------- */
18
-int openserial(char *dev)
19
-{
20
-int	uart;
21
-
22
-fprintf(stderr, "%s ( %s )\n", __func__, dev);
23
-uart = open(dev, O_RDONLY | O_NOCTTY);
24
-if (uart < 0)
25
-	{
26
-	perror("unable to open uart");
27
-	exit(1);
28
-	}
29
-return uart;
30
-}
31
-/* ----------------------------------------------------- */
32
-int configserial(int fd)
33
-{
34
-struct termios	tios;
35
-bzero(&tios, sizeof(tios));
36
-tios.c_cflag = B9600 | CRTSCTS | CS8 | CLOCAL | CREAD;
37
-tios.c_lflag = ICANON;
38
-tios.c_cc[VMIN] = 1;			/* read bloquant ??? */
39
-tcsetattr(fd,TCSANOW,&tios);
40
-return 0;
41
-}
42
-/* ----------------------------------------------------- */
20
+/* ---------------------------------------------------------------- */
43 21
 int main (int argc, char *argv[])
44 22
 {
45 23
 int		serial_in, foo;
46
-unsigned char	byte;
47 24
 
48 25
 if (2 != argc) {
49 26
 	fprintf(stderr, "give me a device name, please.\n");
50 27
 	return 2;
51 28
 	}
52 29
 
53
-serial_in = openserial(argv[1]);
54
-fprintf(stderr, "openserial -> %d\n", serial_in);
55
-
56
-(void)configserial(serial_in);
30
+serial_in = prepare_UART(argv[1], 9600);
57 31
 
32
+fprintf(stderr, "going to listen on %d\n", serial_in);
58 33
 
59 34
 for (;;) {
60
-	foo = read(serial_in, &byte, 1);
61
-	if (1 != foo) {
35
+	foo = getbyte_to(serial_in, 50000);
36
+	if (foo < 0) {
62 37
 		fprintf(stderr, "get byte : got %d, err is %d\n",
63 38
 						foo, errno);
64 39
 		}
65 40
 	else	{
66
-		printf("%9ld    $%02x  ", time(NULL), byte);
67
-		if (isprint(byte))	putchar(byte);
41
+		printf("%9ld    $%02x  ", time(NULL), foo);
42
+		if (isprint(foo))	putchar(foo);
68 43
 		puts("");
69
-		if ('\n'==byte) 	puts("");
44
+		if ('\n'==foo) 	puts("");
70 45
 		}
71 46
 	}
72 47
 
73 48
 return 0;
74 49
 }
75 50
 
76
-/* ----------------------------------------------------- */
51
+/* ---------------------------------------------------------------- */

Loading…
Cancel
Save