#include <iostream>
#include <cstdio>
#include <unistd.h>
#include <stdlib.h>
#include <string>
#include <termios.h>
#include <fcntl.h>
#include <sys/signal.h>
#define BAUDRATE B9600
#define DEVICE "/dev/ttyO4"
#define _POSIX_SOURCE 1
#define FALSE 0
#define TRUE 1
volatile int STOP=FALSE;
int wait_flag=FALSE;
using namespace std;
void signal_handler_IO (int status){
cout << "received SIGIO signal "<< endl;
wait_flag = FALSE;
}
int main()
{
int fd, c, res;
struct termios oldtio, newtio;
struct sigaction saio;
char buffer [255];
system ("echo BB-UART4 > /sys/devices/bone_capemgr.8/slots");
fd = open (DEVICE, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd <0) {perror(DEVICE); exit (-1);}
/*install the siganl handler before making the device ansynhronous */
saio.sa_handler = signal_handler_IO;
saio.sa_mask.__val[0] = 0;
saio.sa_mask.__val[1] = 0;
saio.sa_mask.__val[2] = 0;
saio.sa_mask.__val[3] = 0;
saio.sa_flags = 0;
saio.sa_restorer = NULL;
sigaction(SIGIO, &saio, NULL);
/* Allow the process to receive SIGIO */
fcntl(fd, F_SETOWN, getpid());
/* Make the file descriptor ansynchronous */
fcntl(fd, F_SETFL, FASYNC);
tcgetattr(fd,&oldtio);
newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR | ICRNL;
newtio.c_oflag = 0;
newtio.c_lflag = ICANON;
newtio.c_cc[VMIN]=1;
newtio.c_cc[VTIME]=0;
/* Clean the modem line and activate the settings for the port */
tcflush(fd,TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
//------------ WRITE
//--------------READ
while(STOP==FALSE) {
usleep(1000000);
putchar ('*'); fflush(stdout);
if(wait_flag==FALSE) {
buffer[res]=NULL;
res = read (fd,buffer,sizeof(buffer));
printf(":%s :%d\n", buffer, res);
if (res==1) STOP=TRUE;
wait_flag = TRUE;
}
}
//restore old port settings
tcsetattr(fd,TCSANOW, &oldtio);
close (fd);
return 0;
}