Changeset 12 for trunk/software/tty/readtty_timer.c
- Timestamp:
- Jul 31, 2019, 5:44:55 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/software/tty/readtty_timer.c
r1 r12 3 3 #include <unistd.h> 4 4 #include <errno.h> 5 #include <err.h> 5 6 #include <fcntl.h> 6 7 #include <signal.h> 8 #include <string.h> 7 9 #include <termios.h> 8 10 #include <sys/ioctl.h> 11 #include <sys/select.h> 9 12 10 13 volatile int quit; … … 14 17 { 15 18 quit = 1; 19 } 20 21 static void 22 readline(int d, char *buf, int l) 23 { 24 int i; 25 int bytes; 26 do { 27 for (i = 0; i < l; i++) { 28 do { 29 bytes = read(d, &buf[i], 1); 30 if (quit) 31 return; 32 } while ((bytes < 0 && errno == EAGAIN) || buf[i] == '\r'); 33 if (bytes != 1) { 34 fprintf(stderr, "readline: read %d\n", bytes); 35 err(1, "read line"); 36 } 37 if (buf[i] == '\n') 38 break; 39 } 40 } while (i == 0); 41 if (buf[i] != '\n') { 42 errx(1, "realine: end of line not found"); 43 } 44 buf[i] = '\0'; 16 45 } 17 46 … … 25 54 int i; 26 55 fd_set rfds; 27 char gpio[ 2], current[4], tension[4];56 char gpio[3], current[4], tension[4]; 28 57 char *endp; 29 int currenti, tensioni; 30 int val; 58 int gpioi, currenti, tensioni; 59 int gpioval, currentval, tensionval; 60 int exitst = 0; 31 61 32 62 enum { … … 41 71 42 72 state = INIT; 43 currenti = tensioni = 0;73 gpioi = currenti = tensioni = 0; 44 74 45 75 if (argc != 3) { … … 87 117 } 88 118 fprintf(stderr, "tty ready\n"); 119 write(d, "M0\n", 3); 120 write(d, "M0\n", 3); 121 do { 122 readline(d, buf, sizeof(buf)); 123 if (quit) 124 goto quit; 125 } while (memcmp(buf, "OK", 2) != 0); 126 fprintf(stderr, "starting\n"); 127 write(d, "M1\n", 3); 128 89 129 alarm(atoi(argv[2])); 90 130 FD_ZERO(&rfds); … … 94 134 if (nfds > 0 && FD_ISSET(d, &rfds)) { 95 135 bytes = read(d, buf, sizeof(buf)); 136 if (bytes > sizeof(buf)) { 137 errx(1, "read %d/0x%x bytes", bytes, bytes); 138 } 96 139 if (bytes < 0 && errno != EAGAIN) { 97 140 warn("read"); 141 continue; 98 142 } 99 143 for (i = 0; i < bytes; i++) { 144 if (i > sizeof(buf)) 145 abort(); 146 if (bytes > sizeof(buf)) 147 abort(); 148 if (gpioi > sizeof(buf)) 149 abort(); 150 if (tensioni > sizeof(buf)) 151 abort(); 152 if (currenti > sizeof(buf)) 153 abort(); 100 154 switch(state) { 101 155 case INIT: 102 156 /* look for an X mark */ 103 if (buf[i] == 'X') 157 if (buf[i] == 'X') { 104 158 state = GPIO; 159 gpioi = 0; 160 } 105 161 break; 106 162 case GPIO: 107 gpio[0] = buf[i]; 108 gpio[1] = '\0'; 109 val = strtol(gpio, &endp, 16); 163 gpio[gpioi] = buf[i]; 164 gpioi++; 165 if (gpioi < 2) 166 break; 167 gpio[gpioi] = '\0'; 168 gpioval = strtol(gpio, &endp, 16); 110 169 if (*endp != '\0') { 111 170 fprintf(stderr, 112 171 "gpio error: %c\n", 113 172 *endp); 114 state = INIT;115 } else {116 state = CURRENT;117 currenti = 0;118 printf("%2d ", val);173 exitst = 1; 174 state = INIT; 175 } else { 176 state = TENSION; 177 tensioni = 0; 119 178 } 120 179 break; … … 125 184 break; 126 185 current[currenti] = '\0'; 127 val = strtol(current, &endp, 16);186 currentval = strtol(current, &endp, 16); 128 187 if (*endp != '\0') { 129 188 fprintf(stderr, 130 189 "current error: %c\n", 131 190 *endp); 132 state = INIT; 133 } else { 134 state = TENSION; 191 exitst = 1; 192 state = INIT; 193 } else { 194 state = MARK; 135 195 tensioni = 0; 136 printf("%4d ", val);137 196 } 138 197 break; … … 143 202 break; 144 203 tension[tensioni] = '\0'; 145 val = strtol(tension, &endp, 16);204 tensionval = strtol(tension, &endp, 16); 146 205 if (*endp != '\0') { 147 206 fprintf(stderr, 148 207 "tension error: %c\n", 149 208 *endp); 150 state = INIT; 151 } else { 152 state = MARK; 153 printf("%4d\n", val); 209 exitst = 1; 210 state = INIT; 211 } else { 212 state = CURRENT; 213 currenti = 0; 154 214 } 155 215 break; … … 159 219 "mark error: %c\n", 160 220 buf[i]); 161 state = INIT; 162 } else { 221 exitst = 1; 222 state = INIT; 223 } else { 224 printf("%3d ", gpioval); 225 printf("%4d ", tensionval); 226 printf("%4d\n", currentval); 163 227 state = GPIO; 228 gpioi = 0; 164 229 } 165 230 break; … … 170 235 } 171 236 } 237 quit: 238 /* stop measures */ 239 write(d, "M0\n", 3); 240 if (fflush(stdout) == EOF) { 241 warn("fflush"); 242 } 243 172 244 if (tcsetattr(d, TCSANOW, &ot) < 0) { 173 245 err(1, "restore tcsetattr"); 174 246 } 175 exit( 0);247 exit(exitst); 176 248 } 177 249
Note: See TracChangeset
for help on using the changeset viewer.