title: Linux C Serial Program Freezes
body: <p>I'm working on a small Linux server (Ubuntu Server 13.04 running on a Beagleboard xM ARM computer) that will be communicating between a laptop wirelessly and an Arduino.  The issue I seem to be having is regarding the communication between the Arduino and the Beagleboard.  The program will run just fine for a certain amount of time, ~30 seconds or so, then it will halt.  The program will stay running but the port apparently freezes.</p>
  
  <p>The program I'm running is currently just a test program that will sweep a servo over a certain range.  The code for the functions used to set up the ports was found <a href="https://stackoverflow.com/questions/6947413/how-to-open-read-and-write-from-serial-port-in-c">here</a>.</p>
  
  <p>My program code is as follows, with exception of the code found in the separate thread:</p>
  
  <pre><code>    #include &lt;errno.h&gt;
      #include &lt;termios.h&gt;
      #include &lt;stdio.h&gt;
      #include &lt;string.h&gt;
      #include &lt;fcntl.h&gt;
      #include &lt;unistd.h&gt;
      #include &lt;iostream&gt;
  
      using namespace std;
  
     ...
  
     int main (int argc, const char* argv[]) {
          cout &lt;&lt; "TestIO running...\n";
          char* portname = "/dev/ttyACM0";
          // Open serial port
          int fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
  
          // Return error if port cannot be opened
          if (fd &lt; 0)
          {
                  cout &lt;&lt; "error " &lt;&lt; errno &lt;&lt; " opening " &lt;&lt;  portname &lt;&lt; ": " &lt;&lt; strerror (errno) &lt;&lt; "\n";
                  return -1;
          }
  
          set_interface_attribs (fd, B9600, 0);   // set speed to 9600 bps, 8n1 (no parity)
          set_blocking (fd, 0);                   // set no blocking
  
          // Read from serial port
          //char inputBuffer[64];
          //int inputLength = read(fd, inputBuffer, sizeof inputBuffer);
  
          double output = 575;
          char outputString[255];
          char outputLength;
          int incrimentor = 1;
          char inChar;
          for(;;) {
                  if (output &gt;= 675 )
                          incrimentor = -1;
                  else if (output &lt;= 375)
                          incrimentor = 1;
                  output += incrimentor;
  
                  // Sweep wheels on car, set drive motor to 0
                  outputLength = sprintf(outputString, "%0.2f", output);
                  write(fd, outputString, outputLength);
                  write(fd, ",", 1);
                  write(fd, "0", 1);
                  write(fd, "\n", 1);
  
                  cout &lt;&lt; outputString &lt;&lt; "\n";
  
                  // Sleep thread for 5000 uS (5 ms)
                  usleep(5000);
          }
          close(fd);
          return 0;
  }
  </code></pre>
  
  <p>On a slightly different note, when the program freezes I must force it to quit the code to close the port is never reached and thus I cannot run the program again to test it.  I'm curious if anyone might know how to close a serial port through a Linux command run in the terminal.</p>
  
  <p>Thanks!</p>