/* serial.c * This file contains the function sgetch () which reads a * character from stdin while listening to the serial port. */ /* Copyright (C) 1999 Jonathan duSaint * * This file is part of Tap, an interactive program for reading and * writing data to the serial port to facilitate the reverse * engineering of communication protocols. * * Tap is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * Tap is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public * License for more details. * * The GNU General Public License is generally kept in a file called * COPYING or LICENSE. If you do not have a copy of the license, write * to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "tap.h" /* This function reads a fixed number of chars from the serial port * and then writes it to wherever it needs to go. */ void read_serial_data (void) { int data_read; data_read = read (serial_port_fd, serial_buffer, serial_buffer_size); write_to_output_window (data_read); write_to_capture_register (data_read); write_to_capture_file (data_read); } /* This function reads a character from stdin while listening to * the serial port (if one is open). */ int sgetch (void) { fd_set readfs; int ret, highest_port = 1, fds; for (;;) { /* set focus to input_window */ wrefresh (input_window); FD_ZERO (&readfs); FD_SET (0, &readfs); /* always listen to stdin */ if (port != NULL) { /* if a serial port is open */ FD_SET (serial_port_fd, &readfs); highest_port = serial_port_fd + 1; } fds = select (highest_port, &readfs, NULL, NULL, NULL); if (fds < 0) { /* error */ /* for now just ignore it */ } else if (fds > 0) { if (FD_ISSET (0, &readfs)) { ret = wgetch (input_window); return (ret); } else if (FD_ISSET (serial_port_fd, &readfs)) { read_serial_data (); /* and reloop */ } } } return (ret); }