/*PURPOSE*/
/*	This file will open the serial port at "/dev/ttyUSB0", which */
/*	corresponds to a mouser USB to serial converter.  	     */
/*	It initializes the serial converte 			     */

#include <errno.h>		/* Include Error Definitions*/	
#include <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>
#include <sys/ioctl.h>
#include <string.h>
#include <sys/fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

int main(int argc, char *argv[]){
  
  printf("%X\n",argv[1]);
  printf("%X\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; 
  
  unsigned 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;

  #define TTY_SPEED B9600
  #define TTY_IFLAG IGNBRK
  #define TTY_OFLAG 0
  #define TTY_CFLAG (CS8|CREAD|CLOCAL)
  #define TTY_LFLAG 0
  #define TTY_CC_VMIN  1
  #define TTY_CC_VTIME 0
  
  if ((fd=open("/dev/ttyUSB0", O_RDWR | O_NONBLOCK)) == -1){
 	printf("Failed to Open Port");
  	return (-1);
  }
  if (lockf(fd,F_TEST, 0) == -1) {
        printf("Serial Port is locked");
   	return(-1);
  }
 if(tcgetattr(fd, &options) != 0) {
      printf("tcgetattr failed");
      close(fd);
      return (-1);
   }
 
  int olflags=fcntl(fd,F_GETFL);
  printf("%X\n",olflags);


  perror("Getting Serial Port attributes:"); 
  //cfmakeraw(&options);
  cfsetispeed(&options, B9600);  //This command Works!!
  cfsetospeed(&options, B9600);  //This command Works~~
  
  if(cfsetospeed(&options, TTY_SPEED) == -1) {
      printf("cfsetospeed failed");
      close(fd);
      return (-1);
   }
   if(cfsetispeed(&options, TTY_SPEED) == -1) {
      printf("cfsetispeed failed");
      close(fd);
      return (-1);

  }

  options.c_iflag = TTY_IFLAG;
  options.c_oflag = TTY_OFLAG;
  options.c_cflag = TTY_CFLAG;
  options.c_lflag = TTY_LFLAG;
  options.c_cc[VMIN] = TTY_CC_VMIN;
  options.c_cc[VTIME] = TTY_CC_VTIME;

  if(cfsetospeed(&options, B0) == -1) {
      printf("cfsetospeed failed");
      close(fd);
      return (-1);
   }
   if(cfsetispeed(&options, B0) == -1) {
      printf("cfsetispeed failed");
      close(fd);
      return (-1);

  }

  if(tcflush(fd, TCIOFLUSH) == -1) {
      printf("tcflush failed");
      close(fd);
      return (-1);
   }
   if(tcsetattr(fd, TCSANOW, &options) == -1) {
      printf("tcsetattr failed");
      close(fd);
      return (-1);
   }

  while (1){
 
            written=write(fd,pshex,1);
            sleep(2);
  
             ioctl(fd,FIONREAD,&bytes);

            returned=read(fd,&rmessage[0],1);
	     printf("Reset Successful=%ld\n",rmessage[0]);    
 	    //if(tcflush(fd, TCIOFLUSH) == -1) {
	    //  printf("tcflush failed");
 	    // close(fd);
      	    // return (-1);
  		// }
	 
            
 	    written=write(fd,phhex,1);
            sleep(1);

             ioctl(fd,FIONREAD,&bytes);

            returned=read(fd,&rmessage[0],1);
	    printf("Current Pressure=%ld\n",rmessage[0]);
	    sleep(1);
	
	 	//if(tcflush(fd, TCIOFLUSH) == -1) {
	     // printf("tcflush failed");
      //		close(fd);
   	//   return (-1);
   	//}

  
  
  }
  return 0;

}

