title: Beaglebone Black Serial c++
body: <p>It seems to be a pain to get serial working in c++, adding to that, to do it on the Beaglebone Black is hard, so I need someone with some expertise!</p>
  
  <p>I created /dev/ttyO4 with the following command:</p>
  
  <pre><code>echo BB-UART4 &gt; /sys/devices/bone_capemgr.9/slots
  </code></pre>
  
  <p>This gives me /dev/ttyO4. I then write a small program in cpp using a library linked <a href="http://serialib.free.fr/html/index.html" rel="nofollow">here</a>. My code is found below:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include "serialib.h"
  
  
  #if defined (_WIN32) || defined( _WIN64)
  #define         DEVICE_PORT             "COM1"                               // COM1 for windows
  #endif
  
  #ifdef __linux__
  #define         DEVICE_PORT             "/dev/ttyO4"                         // ttyS0 for linux
  #endif
  
  
  int main()
  {
      serialib LS;                                                            // Object of the serialib class
      int Ret;                                                                // Used for return values
      char Buffer[128];
  
      // Open serial port
  
      Ret=LS.Open(DEVICE_PORT,115200);                                        // Open serial link at 115200 bauds
      if (Ret!=1) {                                                           // If an error occured...
          printf ("Error while opening port. Permission problem ?\n");        // ... display a message ...
          return Ret;                                                         // ... quit the application
      }
      printf ("Serial port opened successfully !\n");
  
      // Write the AT command on the serial port
  
      Ret=LS.WriteString("AT\n");                                             // Send the command on the serial port
      if (Ret!=1) {                                                           // If the writting operation failed ...
          printf ("Error while writing data\n");                              // ... display a message ...
          return Ret;                                                         // ... quit the application.
      }
      printf ("Write operation is successful \n");
  
      // Read a string from the serial device
      Ret=LS.ReadString(Buffer,'\n',128,5000);                                // Read a maximum of 128 characters with a timeout of 5 seconds
                                                                          // The final character of the string must be a line feed ('\n')
      if (Ret&gt;0)                                                              // If a string has been read from, print the string
          printf ("String read from serial port : %s",Buffer);
      else
          printf ("TimeOut reached. No data received !\n");                   // If not, print a message.
  
      // Close the connection with the device
  
      LS.Close();
  
      return 0;
  }
  </code></pre>
  
  <p>When I run the code, it says it opened the port successfully and successfully wrote serially, but I receive no data on the RX. I have connected the RX pin to the TX pin for UART4 (P9.11 and P9.13). I also connected the RX and TX pins for UART5 just incase (P8.37 and P8.38), as the ttyO4 confused me a bit as to which UART I'm using.</p>
  
  <p>Is there something I'm missing to get the serial port working? Or can someone refer me to a working example for serial communication with c++ on the beaglebone black, perhaps like a step-by-step guide?</p>
  
  <p>Regards,
  Cornel</p>
  
  <h2><strong>EDIT:</strong></h2>
  
  <p>The Boost serial libraries work more reliably than serialib, find them <a href="http://boost.org" rel="nofollow">here</a>.</p>