/* * This program cycles DTR. * Do not use stdin because we must open with O_NONBLOCK. */ #include #include #include #include #include #include /* Provides TIOCMBIC/TIOCMBIS */ #include #include #include /* This comes from , which cannot be included */ #define TIOCM_DTR 0x002 #define TIOCM_RTS 0x004 #define NAME "cycle" struct params { char *devname; int sleep; /* Seconds */ }; void parse_args(struct params *p, int argc, char **argv); void Usage(void); int main(int argc, char *argv[]) { struct params par; int fd; unsigned int b; parse_args(&par, argc, argv); /* * Standard nonblocking open for Linux. */ if ((fd = open(par.devname, O_RDWR|O_NONBLOCK)) < 0) { fprintf(stderr, NAME ": Cannot open %s: %s\n", par.devname, strerror(errno)); exit(1); } /* (void) ioctl(fd, TCFLSH, TCIOFLUSH); */ /* make the serial port blocking again */ (void) fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) & ~O_NONBLOCK); /* * Cycle DTR */ printf("Dropping DTR...\n"); b = TIOCM_DTR; if (ioctl(fd, TIOCMBIC, &b) != 0) { fprintf(stderr, NAME ": TIOCMBIC %s: %s\n", par.devname, strerror(errno)); exit(1); } printf("Sleeping (%ds)...\n", par.sleep); sleep(par.sleep); printf("Raising DTR...\n"); b = TIOCM_DTR; if (ioctl(fd, TIOCMBIS, &b) != 0) { fprintf(stderr, NAME ": TIOCMBIS %s: %s\n", par.devname, strerror(errno)); exit(1); } printf("Sleeping (%ds)...\n", par.sleep); sleep(par.sleep); exit(0); } void parse_args(struct params *p, int argc, char **argv) { char *arg; p->devname = NULL; p->sleep = 5; argv++; while ((arg = *argv++) != NULL) { if (arg[0] == '-') { switch (arg[1]) { case 's': if ((arg = *argv) == NULL) Usage(); argv++; if (!isdigit(arg[0])) Usage(); p->sleep = atoi(arg); if (p->sleep < 0) { fprintf(stderr, NAME ": sleep value is bad (%s)\n", arg); exit(1); } break; default: Usage(); } } else { p->devname = arg; if (*argv != NULL) Usage(); } } if (p->devname == NULL) Usage(); } void Usage() { fprintf(stderr, "Usage: cycle [-s sleep] device\n"); exit(1); }