Here is the program for
com2...(/dev/ttyAM1)
#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <sys/stat.h>
/*baudrate settings are defined in <asm/termbits.h>, which
is included by termios.h */
#define BAUDRATE B115200
/* change the following for the correct port */
#define MODEMDEVICE "/dev/ttyAM1"
#define _POSIX_SOURCE 1
/* POSIX complaint source */
#define FALSE 0
#define TRUE 1
volatile int STOP=FALSE;
void signal_handler_IO (int status);/* defn of signal handler
*/
int wait_flag=TRUE; /* TRUE while no signal recvd */
main()
{
int fd,c,res;
struct termios oldtio,newtio;
struct sigaction saio; /*definition of signal
action*/
char buf[255];
/* open modem device for reading and wring and not as
controlling tty to avoid
getting killed by ^c */
/* open the device as non blocking */
fd = open (MODEMDEVICE, O_RDWR | O_NOCTTY | O_NONBLOCK
);
if (fd < 0 ) { perror (MODEMDEVICE); exit (-1);}
/* install the signal handler before making the device
asynchronous */
saio.sa_handler = signal_handler_IO;
/* saio.sa_mask = NULL; */
saio.sa_flags = 0;
saio.sa_restorer = NULL;
sigaction(SIGIO, &saio, NULL);
/* allow the process to receive SIGIO */
fcntl (fd, F_SETFL, FASYNC);
tcgetattr(fd, &oldtio); /* save currnet serial port settings
*/
/* bzero(&newtio, sizeof(newtio)); /* clr struct for new
port settings */
/* BAUDRATE set bps rate you can also cfsetispeed and set
cfsetospeed
CRTSCTS hardware flow control
CS8 : 8n1
CLOCAL : local connection no modem control
CREAD : enable receiving characters
*/
newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR | ICRNL;
newtio.c_oflag = 0;
newtio.c_lflag = ICANON; /*enable canonical input*/
/* initalise control chars */
newtio.c_cc[VINTR] = 0; /* CTRL-C */
newtio.c_cc[VQUIT] = 0; /* CTRL-\ */
newtio.c_cc[VERASE] = 0; /* del */
newtio.c_cc[VKILL] = 0; /* @ */
newtio.c_cc[VEOF] = 4; /* CTRL-d */
newtio.c_cc[VTIME] = 0; /* inter char time */
newtio.c_cc[VMIN] = 1; /* blocking read until 1 char arrives
*/
newtio.c_cc[VSWTC] = 0; /* '\0' */
newtio.c_cc[VSTART] = 0; /* CTRL-q */
newtio.c_cc[VSTOP] = 0; /* CTRL-s */
newtio.c_cc[VSUSP] = 0; /* CTRL-z */
newtio.c_cc[VEOL] = 0; /* '\0' */
newtio.c_cc[VREPRINT] = 0; /* CTRL-r */
newtio.c_cc[VDISCARD] = 0; /* CTRL-u */
newtio.c_cc[VWERASE] = 0; /* CTRL-w */
newtio.c_cc[VLNEXT] = 0; /* CTRL-v */
newtio.c_cc[VEOL2] = 0; /* '\0' */
/* now clean the modem line */
tcflush (fd, TCIFLUSH);
tcsetattr (fd, TCSANOW , &newtio);
/* terminal settings done inputtinf 'z' at the beg of the line
will exit the pgm */
while (STOP==FALSE)
{
/*printf(".\n");usleep(100000);
/*after recving SIGIO wait_flag = FALSE input available for and
can be read*/
if (wait_flag==FALSE)
{
res = read (fd,buf,255);
buf[res] = 0; /* set end of string with null for printf
*/
printf(":%s:%d\r\n", buf, res);
if (res==1) STOP=TRUE;/*stop loop if only CR was input
*/
wait_flag = TRUE; /*wait for new input */
}
}
/* RESTORE OLD PORT SETTINGS */
tcsetattr (fd, TCSANOW, &oldtio);
}
/******************************************************************************
* signal handler sets wait_flag to FALSE to indicate above loop
that chrs is recvd
*******************************************************************************/
void signal_handler_IO (int status)
{
/*printf("received SIGIO signal.\r\n");*/
wait_flag = FALSE;
}