#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

#define BAUDRATE B9600
#define MODEMDEVICE "/dev/ttyS0"
#define QUIT "quit"

main()
{
  int fd,res,n;
  struct termios oldtio, newtio;
  char buf[255];

  fd = open(MODEMDEVICE, O_RDWR);
  if (fd < 0) {printf("open error"); exit(-1);}

  tcgetattr(fd,&oldtio);
  bzero(&newtio, sizeof(newtio));

  newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD ;
  newtio.c_iflag = IGNPAR ;
  newtio.c_oflag = 0;
  newtio.c_lflag = ICANON;

  tcflush(fd, TCIFLUSH);
  tcsetattr(fd, TCSANOW, &newtio);
  while(1){
	res = read(0,buf,255);
	n = write(fd,buf,res);
	printf("number of bytes written %d\n",n);
	buf[res-1] = 0;
	if(strcmp(buf,QUIT) == 0) break;
  }
  tcsetattr(fd, TCSANOW, &oldtio);
}
