Reading from the /dev/ttyUSB1

Hi Guys,

I am working on a school project right now and I am working with an arduino board mega.

I set up my Arduino board to sense when there the connection is closed on the board. When the connection is closed, my Arduino board executes the command “Serial.println(sensorValue, DEC);” and prints a 1 to the serial monitor.

From what I understand, when this command is executed the arduino board is writing to my virtual serial port. A “1” shows up on my serial monitor, but when I try to read from it in C code i always receive a 0.

My code is posted below. Any insight as to how to properly read output from the arduino board?

This is my C code:

   #include <stdio.h>   /* Standard input/output definitions */
    #include <string.h>  /* String function definitions */
    #include <unistd.h>  /* UNIX standard function definitions */
    #include <fcntl.h>   /* File control definitions */
    #include <errno.h>   /* Error number definitions */
    #include <termios.h> /* POSIX terminal control definitions */


    int main(void)
    {
      int fd; /* File descriptor for the port */
      int get;
      get = 5;

      fd = open("/dev/ttyUSB1", O_RDWR | O_NOCTTY | O_NDELAY);
      if (fd == -1)
      {
       //Could not open the port.
      perror("open_port: Unable to open /dev/ttyS0 - ");
      }
      else
      {

        struct termios options;

       //Get the current options for the port...
       tcgetattr(fd, &options);

       //Set the baud rates to 19200...
       cfsetispeed(&options, B9600);
       cfsetospeed(&options, B9600);

       //Enable the receiver and set local mode...
       options.c_cflag |= (CLOCAL | CREAD);

       //Set the new options for the port...
       tcsetattr(fd, TCSANOW, &options);

        while(1){
        delay(500);  
      get = fcntl(fd, F_SETFL, 0);
        //get = fcntl(fd, F_SETFL, FNDELAY);
        printf("read: %d\n",get);
        }
    
      }
    
        return (fd);
    }

try USB0

Why are you calling fcntl()? Surely you want to read the fd, not just change its flags?