/* sout.c - simple serial output code */
/* based on code at: http://www.easysw.com/~mike/serial/ */

#include <stdio.h>   /* Standard input/output definitions */
#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */

int open_port (void);

main ()
    {
    int sfd; /*serial port file descriptor*/
    int n =0;
    int c;
    char buf[80];

    sfd = open_port(); /*open serial port*/
  
    while ((c=getchar())!='Z')
        {
        buf[n++]=c;
        if (c=='\n')
            {
            buf[n]='\0';
            if (write(sfd,buf,n)<0)
                fprintf(stderr,"write() of %d bytes failed.\n",n);
            n=0;
            }
        }
  
   /* tell sin to stop with a Z*/
   buf[0]='Z';
   buf[1]='\n';
   buf[2]='\0';
   if (write(sfd, buf, 2)<0)
        puts("ending write failed");

    close (sfd);
    }

int open_port(void)

    {
    int fd; /*file descriptor*/
    
   fd = open("/dev/ttyS2", O_RDWR | O_NOCTTY | O_NDELAY);
   if (fd == -1)
     fprintf(stderr, "open_port: Unable to open /dev/ttyS2 - %s\n",
             strerror(errno));

   return (fd);
 }
