/* Programm zum Lesen einer seriellen Schnittstelle */
/* see also: Serial HOWTO */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#define BAUDRATE B19200
#define MODEMDEVICE "/dev/ttyS1"
#define _POSIX_SOURCE 1 /* POSIX compliant source */
#define FALSE 0
#define TRUE 1

volatile int STOP=FALSE;

main(int argc, char **argv)
{
   int fd,c, res, k;
   struct termios oldtio,newtio;
   char buf[255];

   if (argc > 1)
      fd = open(argv[1], O_RDWR | O_NOCTTY );
   else
      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 |  CS8 | CLOCAL | CREAD;
   newtio.c_iflag = IGNPAR | IXON | IXOFF;
   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]     = 5;   /* blocking read until 5 chars received */

   tcflush(fd, TCIFLUSH);
   tcsetattr(fd,TCSANOW,&newtio);
   write(fd,"\004",1);
   setvbuf(stdout, (char *)NULL, _IONBF, 0);
   while (STOP==FALSE) {       /* loop for input */
      res = read(fd,buf,255);   /* returns after 5 chars have been input */
      buf[res]=0;               /* so we can printf... */
      for (k=0; k<res;k++) {
        putchar(buf[k]);
      }
      if (buf[0]=='z') STOP=TRUE;
   }
   tcsetattr(fd,TCSANOW,&oldtio);
}
