#include <errno.h>		/* Include Error Definitions*/	
#include <sys/fcntl.h>		/* File control definitions */	
#include <sys/file.h>		/* Not sure */
#include <unistd.h>		/* File control definitions */
#include <stdio.h>		/* Standard Input Output */
#include <termio.h>		/* File control definitions */
#include <termios.h>

int main(int argc, char *argv[]){
  printf("%d\n",argc); 
  printf("%s\n",argv[1]);
  printf("%s\n",argv[2]);
  int fd, written, bytes, *flags, status, returned;
  char ispeed,ospeed;
 
  int phex=0x70;	/*Hexidecimal Value for Pressure*/
  int shex=0x73;	/*Hexidecimal Value for Start over*/
  int thex=0x74;	/*Hexidecimal Value for Temperature*/
  int hhex=0x68;	/*Hexidecimal Value for Humidity*/

  int *pphex,*pshex,*pthex,*phhex; 
  
  char rmessage[60];
  struct termios options;
  
  pphex=&phex;		/*pphex points to phex */
  pshex=&shex;		/*pshex points to shex */
  pthex=&thex;		/*pthex points to thex */
  phhex=&hhex;

  /*Open the Serial Port to be used with only the NONBLOCK setting		*/
  /*O_NONBLOCK=Prevents the process from blocking while opening the file	*/
  /*           	 and possibly in subsequent IO operations			*/
 /*DON'T USE THE O_NDELAY OPTION */
  printf("%X\n",O_NONBLOCK);
  printf("%X\n",O_NOCTTY);
  printf("%X\n",O_RDWR);
 // printf("%X\n",O_IGNORE_CTTY);
  fd=open("/dev/ttyUSB0", O_RDWR | O_NONBLOCK);
  //fd=open("/dev/ttyUSB0",O_RDWR);  
  /*This command is a gem.  It apparaently resets the serial port to default */
  /*If it is not set and the mode it O_NONBLOCK, we will get a -1 return from 
  /*read() and the error will be DEVICE TEMPORARILY UNAVAILABLE */

  /*This command has a man page.  The F_SETFL=Set file status flags */
  fcntl(fd,F_GETFL,flags);
  printf("%ld",flags);

  fcntl(fd,F_SETFL,0);
  // ioctl(fd,TCFLSH); 
   perror("Error In reseting Port:");

  /*Get the Properties currently assigned and fill the structure options with them*/
  tcgetattr(fd, &options);
  perror("Getting Serial Port attributes:"); 
  bzero(&options,sizeof(options));
 
  /*Set Baud Rate to 9600*/
  cfsetispeed(&options, B9600);  //This command Works!!
  cfsetospeed(&options, B9600);  //This command Works~~

  //CONTROL FLAGS 
  options.c_cflag &= ~PARENB;
  options.c_cflag &= ~CSTOPB;
  options.c_cflag &= ~CSIZE;
  options.c_cflag |= CS8;
  options.c_cflag &= ~PARODD;  		/*Disable Odd Parity*/
  options.c_cflag &= ~CRTSCTS;		/*Disable RTS/CTS Handshaking*/
  options.c_cflag &= ~HUPCL;
  //options.c_cflag &= ~CLOCAL;
   perror("Set Baud and Character Size:");
  //Enable the receiver and set local mode
  //options.c_cflag |= (CLOCAL | CREAD);
  perror("Enabled Receiver and Local Control:");
  options.c_cflag |=CLOCAL;
  options.c_cflag |=CREAD;
  //INPUT FLAGS
  //Disable software flow control
  options.c_iflag &= ~(IXON | IXOFF | IXANY );
  options.c_iflag &= ~(IGNBRK | IGNPAR);
  options.c_iflag &= ~(IUCLC);
  options.c_iflag &= ~(IMAXBEL);
// options.c_cflag &= ~(IXON | IXOFF | IXANY );
  //OUTPUT FLAGS
  //Disable software flow control
  options.c_oflag &= ~(IXON | IXOFF | IXANY );
   
  //LOCAL MODE FLAGS.  Raw Input
  options.c_lflag &=~(ICANON | ECHO | ECHOE |ISIG);
    
  //options.c_cc[VMIN] =0 ;
  options.c_cc[VTIME] = 0;

  //NOW SET THE ATTRIBUTES
  //tcsetattr(fd, TCSANOW, &options);
  tcflush(fd,TCIFLUSH);
  tcsetattr(fd,TCSADRAIN,&options);

  written=write(fd,argv[1],1);
  sleep(1);
  
  ioctl(fd,FIONREAD,&bytes);
  //sleep(.5);

  returned=read(fd,&rmessage[0],10);
 
  //written=write(fd,argv[2],1);
  //sleep(1);
  //returned=read(fd,&rmessage[1],1);
 
  perror(""); 
  printf("handle= %ld\n",fd);
  printf("number of bytes written=%ld\n",written);
  printf("number of bytes returned=%ld\n",returned);
  printf("value returned=%X\n",rmessage[0]);
  printf("number of bytes received=%ld\n",bytes);
  close(fd);
  perror("Closing file:");
  return 0;

}

