//------------------------------------------------------------------- // kb2cable.cpp // // This Linux program invokes services provided by our 'uart.c' // device-driver to transmit keyboard-input to a remote machine // via a serial null-modem cable, and to display any characters // that are received from that machine. (In particular, it may // be useful for debugging a keyboard-driver on the remote PC.) // A user terminates this program with or -C. // // compile using: $ g++ kb2cable.cpp -o kb2cable // execute using: $ ./kb2cable // // NOTE: Our 'uart.c' Linux module has to already be installed. // // programmer: ALLAN CRUSE // written on: 26 OCT 2008 //------------------------------------------------------------------- #include // for printf(), perror() #include // for open() #include // for read() #include // for exit() #include // for signal() #include // for tcsetattr(), tcgetattr() char devname[] = "/dev/uart"; struct termios tty_orig; int kybd = STDIN_FILENO; void my_action( int ) { tcsetattr( kybd, TCSAFLUSH, &tty_orig ); printf( "\n" ); exit(0); } int main( int argc, char **argv ) { // open the '/dev/uart' device-file int uart = open( devname, O_RDWR ); if ( uart < 0 ) { perror( devname ); exit(1); } // install signal-handler for CONTROL-C tcgetattr( 0, &tty_orig ); signal( SIGINT, my_action ); // setup keyboard for non-canonical input struct termios tty_work = tty_orig; tty_work.c_lflag = ~( ECHO | ICANON ); tty_work.c_cc[ VMIN ] = 1; tty_work.c_cc[ VTIME ] = 0; tcsetattr( 0, TCSAFLUSH, &tty_work ); // main program-loop to process inputs from keyboard and/or uart int done = 0; while ( !done ) { fd_set readset; FD_ZERO( &readset ); FD_SET( kybd, &readset ); FD_SET( uart, &readset ); if ( select( 1+uart, &readset, 0, 0, 0 ) < 0 ) break; if ( FD_ISSET( kybd, &readset ) ) { int inch = 0; int r = read( kybd, &inch, sizeof( int ) ); if (( r == 1 )&&( inch == 0x1B )) done |= 1; if ( r == 1 ) write( uart, &inch, 1 ); } if ( FD_ISSET( uart, &readset ) ) { int inch = 0; int r = read( uart, &inch, sizeof( inch ) ); if (( r == 1 )&&( inch == 0x1B )) done |= 1; if ( r == 1 ) write( 1, &inch, 1 ); } } tcsetattr( 0, TCSAFLUSH, &tty_orig ); printf( "\n" ); }