#include <sys/fcntl.h> 
#include <sys/file.h>
#include <unistd.h>
#include <stdio.h>
#include <termio.h>
#include <termios.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>

int main(){
  
  //fd is the file descriptor.  It is not a pointer, but rather a number
  //that unix assigns
  int fd, written, bytes, status, returned;
  char ispeed,ospeed;
  char phex=0x68;
  char *amessage;
  char rmessage;
  char tmessage[1]="t";
  char smessage[1]="s";
  char word;
  struct termios options;
  *amessage='s';
  //pmessage is a pointer that is initialized to point to a string constant
  //  int open(char *name, int flags, int perms);
  //fd=open("/home/lances/readme.txt",O_RDWR,0);
  //Open the Serial Port to be used
  fd=open("/dev/ttyUSB0",O_RDWR | O_NONBLOCK | O_NOCTTY | O_NDELAY);
  printf("Opening File /dev/ttyUSB0\n");
  perror("");
  fcntl(fd,F_SETFL,0);
  //!!!!!!!!!!!!!!!!!!!!!!!!!!THIS IS THE KEY.  THIS IS THE CALL THAT
  //MAKES THINGS WORK!!
  //O_NONBLOCK=Prevents the process from blocking while opening the file,
  //	       and possibly in subsequent IO operations
  //O_NOCTTY=  If the file is a terminal deice, this flag prevents the file from  //	       becoming the contrling terminal of the process
	      
  //Do some setting business
  //Force a zero delay time in reading the serial port
  //fcntl(fd,F_SETFL,FNDELAY);
  //Get the current options for the port
  tcgetattr(fd, &options);
  printf("Getting Serial Port attributes\n");
  perror(""); 
  //Set the baud rates to 9600
  printf("Setting Baud Rates\n");
  cfsetispeed(&options, B9600);  //This command Works!!
  perror("");
  cfsetospeed(&options, B9600);  //This command Works~~
  perror("");
  
  options.c_cflag &= ~CSIZE;	/*Mask the Character size bits*/
  options.c_cflag |= CS8;	/*Select 8 data bits */
  perror("");
  //Enable the receiver and set local mode
  options.c_cflag |= (CLOCAL | CREAD | CRTSCTS);
  
  //Disable software flow control
  options.c_cflag &= ~(IXON | IXOFF | IXANY );
  options.c_iflag &= ~(IXON | IXOFF | IXANY );
  options.c_oflag &= ~(IXON | IXOFF | IXANY ); 

  perror("");
  
  //Do Raw
  options.c_lflag &=~(ICANON | ECHO | ECHOE |ISIG);

  options.c_cflag |= (CLOCAL | CREAD);
  //options.c_iflag= IGNBRK | IGNPAR;
  options.c_oflag &= ~OPOST;
  options.c_cc[VMIN] = 0;
  options.c_cc[VTIME] = 5;
  //Set the options on the serial port TCSANOW=Make changes now without waiting
  //for data to complete
  tcsetattr(fd, TCSANOW, &options);
  ispeed=cfgetispeed(&options);
  ospeed=cfgetospeed(&options);
  
  perror("");
  
  printf("Baud Rate=%X\n",ispeed);
  printf("Baud Rate=%X\n",ospeed);
  printf("Options=%d\n",options.c_cflag);
  tcgetattr(fd,&options);
  perror("");
  printf("Baud Rate=%d\n",options.c_iflag);
  printf("Baud Rate=%d\n",options.c_ospeed);
  printf("Options=%d\n",options.c_cflag);

  written=write(fd,"s",1);
  //ioctl(fd,FIONREAD,&bytes);
  sleep(.5);
  returned=read(fd,&rmessage,1);
  printf("Error In Reading:\n"); 
  perror("");
  
  //ioctl(fd,TIOCMGET, &status);
  printf("status before= %ld\n",status);
  //status &= ~TIOCM_DSR; 
  printf("status after= %ld\n",status); 
  //ioctl(fd,TIOCMSET,&status);

  
  printf("handle= %ld\n",fd);
  printf("number of bytes written=%ld\n",written);
  printf("number of bytes returned=%ld\n",bytes);
  printf("value returned=%ld\n",returned);
  printf("value returned=%ld\n",rmessage);
  close(fd);
  return 0;
}

