/* includes for serial communication */ 
       #include <sys/types.h> 
       #include <sys/stat.h>
       #include <fcntl.h>
       #include <termios.h>
       #include <stdio.h>
       
/* define properties of the serial port */ 
       #define BAUDRATE B300
       #define MODEMDEVICE "/dev/ttyS1"
       /* ttyS1 is lower port on phoebe.cse, ttyS0 is upper 11/1/99 R. Chapman  */
       #define _POSIX_SOURCE 1 
       #define FALSE 0
       #define TRUE 1

static   int fd;
static   struct termios oldtio,newtio;
static   char buffer[255]; 


int openIR() { 
        fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
        if (fd <0) {perror(MODEMDEVICE); exit(-1); }

        tcgetattr(fd,&oldtio); /* save current port settings */

        bzero(&newtio, sizeof(newtio));
        newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
        newtio.c_iflag = IGNPAR;
        newtio.c_oflag = 0;

        /* set input mode (non-canonical, no echo,...) */
        newtio.c_lflag = 0;

        newtio.c_cc[VTIME]    = 0;   /* inter-character timer unused */
        newtio.c_cc[VMIN]     = 1;   /* blocking read until 5 chars received */

        tcflush(fd, TCIFLUSH);
        tcsetattr(fd,TCSANOW,&newtio);
        return(0); 
        
} 

       
int main() {
       int res=0; /* used to store return codes from read(); */  
       int i,j;  /* index vars */ 
       unsigned char buf[255],ident[255]; 
       unsigned int chksum = 0; 

       

        openIR(); 
	fprintf(stderr,"Looking for a badge"); 
        while (1) {       /* loop for input till a return gets you out */
          res = read(fd,buf,255);   /* read 1 char */ 
          i = 0;  
          chksum = 0; 
          fprintf(stderr,"%c",buf[0]); 
          /* badge starts out with some '{{{{' chars, at least 5 */ 
	  if (buf[0]  == '{') { 
	    while (buf[0] == '{') {  
                     res=read(fd,buf,255);
                    i++; 
                    /* printf("header mode: %s\n",buf);   */ 
            } 
            if (i < 5) { 
	      fprintf(stderr,"\nBad badge header\n"); 
              continue; /* bad string */  
            }
            /* now we've read the first data character, put it in ident */ 
            i=0; 
            /* keep reading data until you hit a '}' */ 
            while (buf[0] != '}' & i < 30 ) { 
                    ident[i]=buf[0];
                    fprintf(stderr,"digit %d\n",(int) buf[0]); 
                    chksum = (chksum + (int) buf[0]) % 256; 
                    i++; 
                    res = read(fd,buf,255); 
            }   
            j = 0;  
            while (buf[0] == '}') {  
                    res=read(fd,buf,255);
                    j++; 
            }  
            if (j < 2) { 
                 fprintf(stderr,"\nBad badge trailer\n"); 
                 continue; /* bad string */          
            } 
            ident[i] = 0; /* terminate the string */  
            fprintf(stderr,
             "\nBadge Text:%s\n computed checksum=%d, trasnsmitted checksum = %d\n",  
             ident,chksum,buf[0]);  
          }
        }
       closeIR(); 
       }


closeIR() { 
        /* restore the old port settings */ 
	tcsetattr(fd,TCSANOW,&oldtio); 
}






