#include #include #include #include #include #include #include #include #include #define BAUD B115200 int fd0; int fd1; dirswitch (int d) { static int direction = -1; static int cnt = 0; if ((direction == d) && (cnt != 17)) { cnt++; return; } printf ("\n"); fflush (stdout); switch (direction = d) { case 0: printf (">"); break; case 1: printf ("<"); break; } cnt = 1; } static int mainloop () { fd_set rfds; struct timeval tv; unsigned char c; int n = (fd0 > fd1) ? (fd0 + 1) : (fd1 + 1); while (1) { tv.tv_sec = 0; tv.tv_usec = 500000; FD_ZERO (&rfds); FD_SET (fd0, &rfds); FD_SET (fd1, &rfds); if (!select (n, &rfds, NULL, NULL, &tv)) { dirswitch (-1); printf (" ."); fflush (stdout); } else { if (FD_ISSET (fd0, &rfds)) { dirswitch (0); read (fd0, &c, 1); write (fd1, &c, 1); printf (" %02X", c); } if (FD_ISSET (fd1, &rfds)) { dirswitch (1); read (fd1, &c, 1); write (fd0, &c, 1); printf (" %02X", c); } } } } static int init_comms (int fd) { struct termios tios; unsigned char buf[0x100]; int length; tcgetattr (fd, &tios); tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL | CRTSCTS); tios.c_cflag |= CLOCAL; tios.c_cflag |= CS8; tios.c_cflag |= CREAD | HUPCL; tios.c_iflag = IGNBRK | IGNPAR; tios.c_oflag = 0; tios.c_lflag = 0; tios.c_cc[VMIN] = 1; tios.c_cc[VTIME] = 0; cfsetospeed (&tios, BAUD); cfsetispeed (&tios, BAUD); if (tcsetattr (fd, TCSAFLUSH, &tios) < 0) return 0; return 0; } int open_port (char *port) { int fd; fd = open (port, O_RDWR); if (fd < 0) { return -1; } if (init_comms (fd)) { close (fd); return -1; } return fd; } main () { fd0 = open_port ("/dev/ttyS0"); fd1 = open_port ("/dev/ttyS1"); mainloop (); }