#define _DEFAULT_SOURCE #include #include #include #include #include #include #include #include #include #include #define SER_SPEED B19200 #define SER_BITS CS7 #define SER_PARITY (PARENB | PARODD) int quit_flag = 0; void handle_sigint(int a) { quit_flag = 1; } int sercat(char *dev) { int fd; struct termios tty, orig_tty; int tio, orig_tio; int r; char buffer[14]; signal(SIGINT, handle_sigint); fd = open(dev, O_RDWR | O_NOCTTY); if (fd < 0) { perror("open():"); return fd; } memset(&orig_tty, 0, sizeof(orig_tty)); r = tcgetattr(fd, &orig_tty); if (r < 0) { close(fd); perror("tcgetattr():"); return r; } memcpy(&tty, &orig_tty, sizeof(orig_tty)); cfsetospeed(&tty, SER_SPEED); cfsetispeed(&tty, SER_SPEED); tty.c_cflag = (tty.c_cflag & ~CSIZE) | SER_BITS; tty.c_cflag = (tty.c_cflag & ~(PARENB | PARODD)) | SER_PARITY; tty.c_cflag &= ~CSTOPB; tty.c_cflag &= ~CRTSCTS; tty.c_iflag &= ~ICRNL; r = tcsetattr(fd, TCSANOW, &tty); if (r < 0) { perror("tcsetattr():"); close(fd); return r; } ioctl(fd, TIOCMGET, &orig_tio); // To power the adapter we need DTR high and RTS low tio = (orig_tio & ~TIOCM_RTS) | TIOCM_DTR; ioctl(fd, TIOCMSET, &tio); while ((r = read(fd, buffer, sizeof(buffer))) > 0) { write(STDOUT_FILENO, buffer, r); if (quit_flag) break; } close(STDOUT_FILENO); if (r < 0) { perror("read():"); } r = tcsetattr(fd, TCSANOW, &orig_tty); if (r < 0) { perror("tcsetattr(restore):"); } ioctl(fd, TIOCMSET, &orig_tio); close(fd); return 0; } void usage(char *bin) { fprintf(stderr, "Symtax: %s SERIAL_PORT\n", bin); } int main(int argc, char *argp[]) { if (argc < 2) usage(argp[0]); else { if (sercat(argp[1]) < 0) return EXIT_FAILURE; } return EXIT_SUCCESS; }