#include #include #include #include #include #include #include #include #include #define SECONDS_BETWEEN_SAMPLES 5 #define BAUDRATE B38400 #define MODEMDEVICE "/dev/ttyS1" #define _POSIX_SOURCE 1 //POSIX compliant source #define FALSE 0 #define TRUE 1 volatile int STOP = FALSE; void signal_handler_IO ( int status ); // definition of signal handler int bSerialDataAvailable = FALSE; // FALSE while no signal received char devicename [80]; long PARITYON = 0; // Disabled long PARITY = 0; // Dont care, but set it anyway // 00 = NONE, 01 = Odd, 02 = Even, 03 = Mark, 04 = Space FILE * input; FILE * output; int status; char * pszPoke; typedef int bool; int main ( int Parm_Count, char * Parms [] ) { char message [90]; int fd, tty, c, res, i, error, iOutInd, iSample, iSampleTime; char In1, Key; struct termios oldtio, newtio; //place for old and new port settings for serial port struct termios oldkey, newkey; //place tor old and new port settings for keyboard teletype struct sigaction saio; //definition of signal action char buf[255]; //buffer for where data is put time_t tNow; time_t tNextSample; struct tm * pTm; bool bWaste; char pszMsgBuf [200]; int j; // Open the console for read and write fprintf ( stderr, "Opening Console\n\r" ); tty = open ( "/dev/tty", O_RDWR | O_NOCTTY | O_NONBLOCK ); input = fdopen ( tty, "r"); //open the terminal keyboard output = fdopen ( tty, "w"); //open the terminal screen if ( !input || !output ) { fprintf ( stderr, "Unable to open /dev/tty\n" ); return ( 1 ); } /* endif */ tty = open ( "/dev/tty", O_RDWR | O_NOCTTY | O_NONBLOCK ); // set the user console port up tcgetattr ( tty, &oldkey ); // save current port settings // so commands are interpreted right for this program // set new port settings for non-canonical input processing //must be NOCTTY fprintf ( stderr, "Setting new console parms\n\r" ); newkey.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD; newkey.c_iflag = IGNPAR; newkey.c_oflag = 0; newkey.c_lflag = 0; //ICANON; newkey.c_cc [VMIN ] = 1; newkey.c_cc [VTIME] = 0; tcflush ( tty, TCIFLUSH ); tcsetattr ( tty, TCSANOW, &newkey ); //open the device (com port) to be non-blocking (read will return immediately) fprintf ( stderr, "Opening com port\n\r" ); strcpy ( devicename, MODEMDEVICE ); fd = open ( devicename, O_RDWR | O_NOCTTY | O_NONBLOCK ); if ( fd < 0 ) { perror ( devicename ); return ( -1 ); } /* endif */ //install the serial handler before making the device asynchronous fprintf ( stderr, "Installing handlers\n\r" ); saio.sa_handler = signal_handler_IO; sigemptyset ( &saio.sa_mask ); //saio.sa_mask = 0; saio.sa_flags = 0; saio.sa_restorer = NULL; sigaction ( SIGIO, &saio, NULL ); // allow the process to receive SIGIO fcntl ( fd, F_SETOWN, getpid () ); // Make the file descriptor asynchronous // ( the manual page says only O_APPEND and O_NONBLOCK, will work with F_SETFL... ) fcntl ( fd, F_SETFL, FASYNC ); tcgetattr ( fd, &oldtio ); // save current port settings fprintf ( stderr, "Setup com port stuff\n\r" ); // set new port settings for canonical input processing newtio.c_cflag = B1200 | CRTSCTS | CS7 | CSTOPB | PARENB | PARODD | CLOCAL | CREAD; pszPoke = "H"; newtio.c_iflag = 0; newtio.c_iflag |= ICRNL; newtio.c_oflag = 0; newtio.c_lflag = 0; newtio.c_lflag = ICANON; tcflush ( fd, TCIFLUSH ); tcsetattr ( fd, TCSANOW, &newtio ); iSample = 0; time ( &tNextSample ); iSampleTime = SECONDS_BETWEEN_SAMPLES; // loop while waiting for input. normally we would do something useful here while ( STOP == FALSE ) { // Check for user input status = fread ( &Key, 1, 1, input ); if ( status == 1 ) { //if a key was hit switch ( Key ) { // branch to appropiate key handler case 0x1b: // Esc STOP=TRUE; // User signals toprogram to terminate break; default: break; } //end of switch key } //end if a key was hit time ( &tNow ); if ( tNow >= tNextSample ) { pTm = localtime ( &tNow ); sprintf ( pszMsgBuf, pTm->tm_hour, pTm->tm_min, pTm->tm_sec, pTm->tm_mday, pTm->tm_mon, pTm->tm_year + 1900, iSample++ ); fprintf ( stderr, pszMsgBuf ); write ( fd, pszPoke, strlen ( pszPoke ) ); // write 1 byte to serial port iOutInd = 0; tNextSample = tNow + iSampleTime; } /* endif */ // after receiving SIGIO, bSerialDataAvailable = TRUE, input is available and can be read // if serial input is available if ( ! bSerialDataAvailable ) continue; res = read ( fd, buf, 98 ); if ( res ) { j = 0; for ( i = 0; i < res; i++ ) if ( isdigit ( buf [i] ) ) buf [j++] = buf[i]; buf [j] = 0; strcat ( pszMsgBuf, buf ); printf ( pszMsgBuf ); fprintf ( stderr, buf ); } bSerialDataAvailable = FALSE; // wait for new input } //while stop==FALSE // restore old port settings tcsetattr ( fd, TCSANOW, &oldtio ); tcsetattr ( tty, TCSANOW, &oldkey ); close ( tty ); close ( fd ); //close the com port fclose ( input ); fclose ( output ); fprintf ( stderr, "\n\rThe program has closed\n\r" ); } //end of main /*************************************************************************** * signal handler. sets bSerialDataAvailable to TRUE, to indicate above loop that * * characters have been received. * ***************************************************************************/ void signal_handler_IO (int status) { bSerialDataAvailable = TRUE; }