title: Reading from a serial port after writing on it
body: <p>I am working on a project that has my computer communicating with an arduino board that reads the sensor output and put it on the serial port only if a "t" was received.the arduino code as shown below is working.</p>
  
  <pre><code>const int inputPin = 0;
  void setup(){
    Serial.begin(9600);
    pinMode(13, OUTPUT);}
  
  void loop(){
   if (Serial.available() &gt; 0){
      char c=Serial.read();
     if(c=='t'){
        int value = analogRead(inputPin);
        float celsius = (5.0 * value * 100.0)/1024.0; 
        Serial.println(celsius);
      }
    }
  }
  </code></pre>
  
  <p>My problem is in the C code when Im trying to read what arduino puts on the serial port. My C code is:</p>
  
  <pre><code>#include&lt;string.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;stdio.h&gt;
  #include&lt;errno.h&gt;
  #include&lt;unistd.h&gt;
  #include&lt;fcntl.h&gt;
  
  int main(){     
      int STATE_OK=0;
      int STATE_WARNING=1;
      int STATE_CRITICAL=2; 
      char tempbuf[10];
      int fd=open("/dev/ttyACM0",O_RDWR | O_NOCTTY | O_NONBLOCK);
      if(fd == -1){
              printf("Unable to open /dev/ttyACM0\n");
              return STATE_WARNING;
      } else {
          fcntl(fd, F_SETFL, FNDELAY);
          int w=write(fd, "t", 1);
          printf("The number of bytes written to the serial port is %d \n",w);
          fprintf(stderr, "fd = %d.\n", fd);
          sleep(10);
          int n=read(fd,tempbuf,5);
          printf("%d,%s \n",n,strerror(errno));
          if(n&gt;0){
              float temp=atof(tempbuf);
              printf("Temperature is: %f Celsius\n", temp);
              if (temp&gt;27){
                  return STATE_CRITICAL;
              }else{
                  printf("The temperature is %f Celsius and checked 10 seconds ago\n",temp);
                  return STATE_OK;
              }
          }
      }
      close(fd);
      return 0;
  }
  </code></pre>
  
  <p>n is always=0 and i can't figure out what is the problem.
  Thanks in advance.</p>