#include "stdio.h"
#include "stdlib.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "unistd.h"
#include "fcntl.h"
#include "errno.h"
#include "termios.h"

int main(argc,argv)
int argc;
char **argv;
{
   char buffer[8];
   int c,i,fd,status;
   struct termios opts;

   /* Attempt to open the serial port */
   if ((fd = open("/dev/ttym2",O_RDWR | O_NOCTTY | O_NDELAY)) == -1) {
      printf("Unable to open serial device, error %d\n",fd);
      exit(0);
   }

   /* Configure various things */
   tcgetattr(fd,&opts);
   opts.c_lflag &= ~(ICANON | ECHO | ISIG);
   opts.c_cflag |= (CLOCAL | CREAD);
   cfsetispeed(&opts,B38400);
   opts.c_cflag &= ~CNEW_RTSCTS;
   opts.c_iflag |= (INPCK | ISTRIP);
   opts.c_iflag |= (IXON | IXOFF | IXANY);
   opts.c_cflag &= ~CSIZE;
   opts.c_cflag |= CS8;
   opts.c_oflag &= ~OPOST;
   opts.c_cc[VMIN] = 1;
   opts.c_cc[VTIME] = 0;
   tcsetattr(fd,TCSANOW,&opts);

   /*
      Attempt to write to the port
      Initialise the modem to its factory settings for example
   */
   if ((status = write(fd,"AT&F\r",5)) != 5) {
      fprintf(stderr,"Unable to write to the port, error %d\n",status);
      close(fd);
      exit(0);
   }

   /*
      Wait around until we receive stuff
      Pretty nasty as you'll have to kill this process now!
      All we really want to see is the following string
         AT&F...OK..
   */
   for (;;) {
      if (read(fd,buffer,1) > 0) {
         c = buffer[0];
         if (c == '\n' || c == '\r')
            c = '.';
         printf("%c",c);
         fflush(stdout);
      }
   }

   /* Finished, close the port */
   close(fd);
}


