title: Reading Values Via Serial Port
body: <p>I am working on a project that involves reading data from an Arduino Uno over serial port. In Arduino IDE, I observe that I am successfully printing values over the serial port in the following format:</p>
  
  <pre><code>  Serial.print(x, DEC);
    Serial.print(' ');
    Serial.print(y, DEC);
    Serial.print(' ');
    Serial.println(z, DEC);
  </code></pre>
  
  <p>e.g.: </p>
  
  <p>2 4 -41</p>
  
  <p>4 8 -32</p>
  
  <p>10 5 -50</p>
  
  <p>...etc.</p>
  
  <p>Then, I have a program written in C using XCode to read these values as float data types. However, upon running the program in terminal, the program appears to be stuck (no values are read, and I must use ctrl+C to exit).</p>
  
  <p>Any ideas what I might be doing wrong? Below is my code. As of now, I am just using a for loop to test whether I am actually reading in these values. Let me know if you need more information. Thanks for your help.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;ApplicationServices/ApplicationServices.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;fcntl.h&gt;
  
  // buffer for data
  float buffer[100];
  
  int main(int argc, const char* argv[])
  {
  
      // open serial port
      int port;
  
      port = open("/dev/tty.usbmodem1411", O_RDONLY);
      if (port == -1)
      {
          printf("Unable to open serial port.");
      }
  
      // testing read of data
  
      fcntl(port, F_SETFL, FNDELAY);
  
      for (int i = 0; i &lt; 10; i++)
      {
  
          read(port, buffer, 12);       
          printf("%.2f %.2f %.2f\n", buffer[3*i], buffer[3*i+1], buffer[3*i+2]);
      }
  
      // close serial port
      close(port);
  
      return 0;
  }
  </code></pre>