title: Arduino Serial Communication from Third Party Platform
body: <p>I'm designing a Serial Communications platform that sends data from c++ code that uses Boost.Asio, and receives it via Arduino.</p>
  
  <p>My test program structure is just an echoing program: the computer sends some characters over to the Arduino, the Arduino sends the characters back to the computer, which <code>async_read</code>s them. </p>
  
  <p>I'm close to having good communication asynchronously, but one problem persists. Disclosure: my Baud rates are matching, at least in the two programs. My problem is that whenever I send data at a baud rate other than 9600, some sort of error occurs between echoing the data from the Arduino to the computer. An unknown character is appended to the start of the message, and in some cases, two unknown characters are appended.</p>
  
  <p>It would seem that this could be some form of synchronization issue with the start and stop bits, but I'm not sure. Can anyone suggest anything I should check (besides that my baud rates, which are indeed matching)? I'm not sure where I should look for the problem.</p>
  
  <p><strong><em>Echo program on computer side:</em></strong></p>
  
  <pre><code>#ifndef IOSTREAM_H
  #include &lt;iostream&gt;
  #endif
  #ifndef SERIAL_H
  #include "serial.h"
  #endif
  #include &lt;time.h&gt;
  #define PORT "/dev/tty.usbmodem1411"
  
  int main(){
      std::clock_t t;
      t = std::clock();
      boost::asio::io_service io;
      Serial::Serial serial(PORT, &amp;io, 19200);
      if(!serial.is_open()){
        serial.open(PORT);
      }
      std::string s = "hellomynameiswill~";
      serial.async_write(s);
      serial.async_read_until("\n");
      io.run();
      t = std::clock() - t;
      std::cout &lt;&lt; "Execution time:" &lt;&lt; ((float)t)/CLOCKS_PER_SEC &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p><strong><em>Echo program on Arduino side:</em></strong></p>
  
  <pre><code>boolean isSerial = false;
  
  void setup() {
    pinMode(13, OUTPUT);
      Serial.begin(9600);
  }
  
  void loop() {
    isSerial = false;
    while(Serial.available() &gt; 0){
  //    delay(1);
      char byteRead = Serial.read();
      isSerial = true;
      while(byteRead != 126){
        if(Serial.available() &gt; 0){      
          Serial.print(byteRead);
          byteRead = Serial.read();
        }
      }
      if(isSerial){
        Serial.print('\n');
      }
    } 
  }
  </code></pre>
  
  <p>The output I receive on the echo program with a baud rate other than 9600 is always either <code>"?hellomynameiswill~"</code> or <code>"??hellomynameiswill~"</code>. The latter output is much more common. I am using an Arduino Uno.  </p>
  
  <p>Edit 2: The ? at the beginning of the sequence is a negative integer when converted to ascii value.  I'm still not sure how to approach the fix.
  Edit 3: The ? at the beginning of the sequence can be traced to the original boost::asio::async_write of the message to the arduino.  Alternating between boost::asio::async_write and boost::asio::write makes no difference as to the outputted text.</p>