/*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; 
  
  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);
   }
  ioctl(fd,TIOCMGET,&status);
  printf("Status is =%X\n",status);

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

  returned=read(fd,&rmessage[0],10);
 
 
  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;

}

