12 #include <sys/types.h> 14 #include <sys/select.h> 23 struct termios oldt, newt;
27 tcgetattr(STDIN_FILENO, &oldt);
29 newt.c_lflag &= ~(ICANON | ECHO);
30 tcsetattr(STDIN_FILENO, TCSANOW, &newt);
31 oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
32 fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
36 tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
37 fcntl(STDIN_FILENO, F_SETFL, oldf);
48 #define cfsetspeed(term, baudrate)\ 49 cfsetispeed(term, baudrate);\ 50 cfsetospeed(term, baudrate); 54 int origflags = fcntl(fd, F_GETFL, 0);
55 fcntl(fd, F_SETFL, origflags & ~O_NONBLOCK);
56 ssize_t len = strlen(msg);
57 ssize_t retval = write(fd, msg, len);
58 int fputserrno = errno;
59 fcntl(fd, F_SETFL, origflags | O_NONBLOCK);
65 printf(
"fputs fialed %d: %s", errno, strerror(errno));
77 unsigned short data[6];
80 int clk, clkb, clkb2, clk0;
90 printf(
"Enter COM port > ");
93 sprintf(devname_out,
"/dev/serusb%d", comNo);
94 printf(
"Open %s for output\n", devname_out);
95 fd = open(devname_out, O_RDWR | O_NOCTTY | O_NONBLOCK);
102 printf(
"Enter sampling time (ms) > ");
105 printf(
"Sampling time = %d ms\n", tw);
107 printf(
"Enter File name > ");
115 slogf(0, _SLOG_INFO,
"Started input: %s fd = %d\n", devname_in, fd);
116 slogf(0, _SLOG_INFO,
" output: %s fd = %d\n", devname_out, fd);
121 printf(
"=== record data ===\n");
122 clk0 = clock() / (CLOCKS_PER_SEC / 1000);
128 struct timeval wr0, wr1, tcd0, tcd1;
130 n = write(fd,
"R", 1);
131 gettimeofday(&wr1, NULL);
132 gettimeofday(&tcd0, NULL);
134 gettimeofday(&tcd1, NULL);
135 #define DELTA_SEC(start, end) (end.tv_sec - start.tv_sec + (end.tv_usec - start.tv_usec)/1e6) 136 printf(
"write took %7.3f, tcdrain took %7.3f [msec]\n",
DELTA_SEC(wr0, wr1) * 1000,
DELTA_SEC(tcd0, tcd1) * 1000);
137 printf(
"write data (ret %d)\n", n);
142 clk = clock() / (CLOCKS_PER_SEC / 1000) - clk0;
143 if (clk >= clkb + tw) {
144 clkb = clk / tw * tw;
150 gettimeofday(&wr0, NULL);
151 n = write(fd,
"R", 1);
152 gettimeofday(&wr1, NULL);
153 printf(
"write (in loop) took %7.3f [msec]\n",
DELTA_SEC(tcd0, tcd1) * 1000);
154 gettimeofday(&tcd0, NULL);
156 gettimeofday(&tcd1, NULL);
157 printf(
"(In loop) write took %7.3f, tcdrain took %7.3f [msec]\n",
DELTA_SEC(wr0, wr1) * 1000,
DELTA_SEC(tcd0, tcd1) * 1000);
158 printf(
"write data (ret %d)\n", n);
161 #define DATA_LENGTH 27 166 printf(
"read data (ret %d, %d bytes read))\n", n, len + n);
170 char *pmesg = strerror(errno);
171 fprintf(
stderr,
"failed to read data (%d): %s (%d)\n", n, pmesg,
181 fprintf(
stderr,
"%02x:", 0x0000ff & str[i]);
185 sscanf(str,
"%1d%4hx%4hx%4hx%4hx%4hx%4hx", &tick, &data[0], &data[1],
186 &data[2], &data[3], &data[4], &data[5]);
188 sprintf(str,
"%05d,%d,%05d,%05d,%05d,%05d,%05d,%05d\n", clk / tw * tw,
189 tick, data[0], data[1], data[2], data[3], data[4], data[5]);
195 skip:
if (clk >= 10000)
199 if (clk >= clkb2 + 1000) {
201 if (
kbhit() && getchar() ==
'.')
203 clkb2 = clk / 1000 * 1000;
212 printf(
"=== num = %d ===\n", num);
214 printf(
"exit (y / n) ? > ");
227 int oldf = fcntl(fd, F_GETFL, 0);
228 fcntl(fd, F_SETFL, oldf | O_NONBLOCK);
230 while (read(fd, &c, 1) != EOF)
232 fcntl(fd, F_SETFL, oldf);
236 #define B921600 0010007 240 struct termios newtio;
241 tcgetattr(fd, &newtio);
242 memset(&newtio.c_cc, 0,
sizeof(newtio.c_cc));
243 newtio.c_cflag |= CS8 | CLOCAL | CREAD;
245 newtio.c_cflag &= ~(IHFLOW | OHFLOW);
246 newtio.c_iflag = IGNPAR;
252 newtio.c_cc[VMIN] = 1;
253 newtio.c_cc[VTIME] = 2;
254 newtio.c_cc[VEOF] = 4;
265 char *pmesg = strerror(errno);
266 fprintf (
stderr,
"failed to cfsetspeed(): %s\n", pmesg);
271 tcflush(fd, TCIFLUSH);
272 if (tcsetattr(fd, TCSANOW, &newtio) < 0) {
273 printf(
"can not set attr");
279 int retval = tcflush(fd, TCIOFLUSH);
281 printf(
"tcflush failed\n");
291 n = tcgetattr(fdc, &term);
293 char *pmesg = strerror(errno);
294 fprintf(
stderr,
"failed to tcgetattr(): %s\n", pmesg);
298 bzero(&term,
sizeof(term));
301 term.c_cflag = CS8 | CLOCAL | CREAD;
312 char *pmesg = strerror(errno);
313 fprintf(
stderr,
"failed to cfsetspeed(): %s\n", pmesg);
317 term.c_iflag = IGNPAR;
321 term.c_lflag |= IEXTEN;
323 term.c_cc[VINTR] = 0;
324 term.c_cc[VQUIT] = 0;
325 term.c_cc[VERASE] = 0;
326 term.c_cc[VKILL] = 0;
328 term.c_cc[VTIME] = 0;
331 term.c_cc[VSTART] = 0;
332 term.c_cc[VSTOP] = 0;
333 term.c_cc[VSUSP] = 0;
335 term.c_cc[VREPRINT] = 0;
336 term.c_cc[VDISCARD] = 0;
337 term.c_cc[VWERASE] = 0;
338 term.c_cc[VLNEXT] = 0;
339 term.c_cc[VEOL2] = 0;
342 term.c_cflag &= ~PARENB;
343 term.c_cflag &= ~CSTOPB;
344 term.c_cflag &= ~(IHFLOW | OHFLOW);
347 term.c_cc[VTIME] = 0;
351 n = tcsetattr(fdc, TCSANOW, &term);
354 char *pmesg = strerror(errno);
355 fprintf(
stderr,
"failed to tcsetattr(): %s\n", pmesg);
367 char *pmesg = strerror(errno);
368 fprintf(
stderr,
"failed to open : %s\n", pmesg);
373 int res = tcgetattr(fdc, &term);
375 char *pmesg = strerror(errno);
376 fprintf(
stderr,
"failed to tcgetattr(): %s\n", pmesg);
385 char *pmesg = strerror(errno);
386 fprintf(
stderr,
"failed to cfsetspeed(): %s\n", pmesg);
391 term.c_iflag |= IGNPAR;
392 term.c_cflag |= (CLOCAL | CREAD);
393 term.c_cflag &= ~PARENB;
395 term.c_cflag &= ~CSTOPB;
398 term.c_lflag &= ~(ECHO | ECHOCTL | ECHONL);
401 term.c_iflag &= ~INPCK;
404 term.c_cc[VINTR] = 0;
405 term.c_cc[VQUIT] = 0;
406 term.c_cc[VERASE] = 0;
407 term.c_cc[VKILL] = 0;
409 term.c_cc[VTIME] = 0;
412 term.c_cc[VSTART] = 0;
413 term.c_cc[VSTOP] = 0;
414 term.c_cc[VSUSP] = 0;
416 term.c_cc[VREPRINT] = 0;
417 term.c_cc[VDISCARD] = 0;
418 term.c_cc[VWERASE] = 0;
419 term.c_cc[VLNEXT] = 0;
420 term.c_cc[VEOL2] = 0;
422 term.c_cflag &= ~(IHFLOW | OHFLOW);
427 term.c_cc[VTIME] = 2;
429 res = tcsetattr(fdc, TCSANOW, &term);
431 char *pmesg = strerror(errno);
432 fprintf(
stderr,
"failed to tcsetattr(): %s\n", pmesg);
void clear_packet(int fd)
unsigned int sleep(unsigned int seconds)
#define DELTA_SEC(start, end)
int gettimeofday(struct timeval *tv, struct timezone *tz)
std::string sprintf(char const *__restrict fmt,...)
int com_write(int fd, const char *msg)
#define cfsetspeed(term, baudrate)
int usleep(useconds_t usec)