title: Arduino Mega receiving correct data through Serial 0, but not Serial 1-3
body: <p>I currently have a HC-06 Bluetooth device connected to my <a href="https://docs.arduino.cc/hardware/mega-2560" rel="nofollow noreferrer">Arduino Mega 2560</a> in order to receive strings sent from an Android device. With the HC-06 on Serial 0, I am receiving the data without error with the following code:</p>
  <pre class="lang-cpp prettyprint-override"><code>String inString = &quot;&quot;;
  int index = 0;
  boolean stringComplete = false;
  
  void setup() {
    Serial.begin(9600);
    pinMode(pwmPin, OUTPUT);
  }
  
  void loop() {
    if(stringComplete) {
      ParseSerialData();       // Parse the received data
      inString = &quot;&quot;;           // Reset inString to empty
      stringComplete = false;  // Reset the system for further input of data
    }
  }
  
  
  void serialEvent() {
    while(Serial.available() &amp;&amp; stringComplete == false) {
      char inChar = Serial.read();
      inData[index] = inChar;       // Store it in char array
      index++;
  
      if (inChar == '\n') {         // Check for termination character
        index = 0;                  // Reset the index
        stringComplete = true;      // Set completion of read to true
      } else {
        inString += inChar;         // Also store as string
      }
    }
  }
  </code></pre>
  <p>When I try to replace &quot;Serial&quot; with &quot;Serial1&quot; and &quot;serialEvent()&quot; with &quot;serialEvent1()&quot; and move the Bluetooth device to the TX1 and RX1, this program no longer works.</p>
  <p>I have read that some people had similar problems when using AVR-GCC 4.4.x and solved the issue by downgrading to 4.3.x, but I have 4.3.2 (on Windows 8.1, same problem has arisen with Arduino IDE 1.0.3, 1.0.5-r2, and 1.5.6-r2).</p>
  <p>I added the following print statements (with Serial 0 to print to the monitor on my PC) to the code with the Bluetooth device still on Serial 1:</p>
  <pre class="lang-cpp prettyprint-override"><code>String inString = &quot;&quot;;
  int index = 0;
  boolean stringComplete = false;
  
  void setup() {
    Serial1.begin(9600);
    Serial.begin(9600);
    pinMode(pwmPin, OUTPUT);
    Serial.println(&quot;Setting up...&quot;);
  }
  
  void loop() {
    if(stringComplete) {
      ParseSerialData();
      inString = &quot;&quot;;
      stringComplete = false;
    }
  }
  
  void serialEvent1() {
  
    Serial.println(&quot;In serialEvent1...&quot;);
  
    while(Serial1.available() &amp;&amp; stringComplete == false) {
  
      Serial.println(&quot;Char in...&quot;);
  
      char inChar = Serial1.read();
  
      Serial.println(&quot;WTF&quot;);
      Serial.println(inChar);
      Serial.println(&quot;WTF2&quot;);
  
      inData[index] = inChar;
  
      index++;
  
      if (inChar == '\n'){
        Serial.println(&quot;Termination char read...&quot;);
  
        index = 0;
        stringComplete = true;
      }else{
        inString += inChar;
      }
    }
  }
  </code></pre>
  <p>Doing this, on the monitor I get:</p>
  <pre class="lang-none prettyprint-override"><code>Setting up...
  In serialEvent1...
  Char in...
  WTF
  
  WTF2
  </code></pre>
  <p>inChar typically prints as nothing, but during one test it was printing as an '@' character. The string sent is &quot;s,1\n&quot; from the Android device.</p>
  <p>Based on the print out, the serial event is triggered by availability of serial data, but Serial1.available() remains true for only the first iteration, 's' is not read in (nor any of the other characters that do when Serial is used), and a termination character (newline char) is never read in so that I can begin parsing.</p>
  <p>I also tried various baud rates with no difference in behavior. Based on reading Arduino documentation, serial port 1 should work just like serial port 0, and I did not miss substituting Serial for Serial1 in any part of the code.</p>
  <p>What could be causing errors in communicating over Serial1 in the same way that has worked flawlessly on Serial0?</p>
  <p>I also found a related <a href="https://stackoverflow.com/questions/12754972/arduino-serial-communication-not-receiving-entire-message">Stack Overflow question</a>, which was solved with something similar to my code (which works perfectly with Serial0 and is based on the <a href="http://arduino.cc/en/Tutorial/SerialEvent" rel="nofollow noreferrer">Arduino documentation</a>) using an expected termination character (the difference being that his code implements serial reading in the main loop, whereas mine is in a serialEvent). For some reason, it seems that both of us were having issues with Serial1 data not showing as available at the start of the next iteration. For some reason, serialEvent1 is not being called again for me. And I still don't understand why the first/only character read is not 's.' At first I was thinking that the stream was getting flushed before getting to the serial event again, but that still doesn't account for reading in an incorrect first character.</p>
  <p>Also, I added the following Serial1 print statement to run multiple times in the Arduino setup and the Android device receives it each time with no errors, so sending data is working just fine:</p>
  <pre><code>Serial1.print(&quot;b,1\n&quot;);
  </code></pre>
  <p>Or even</p>
  <pre><code>Serial1.print(&quot;A long test message. A long test message.\n&quot;);
  </code></pre>
  <hr/>
  <p>I'm fairly close to answering my own question now with further testing/debugging. I actually think the answer may end up being hardware-related rather than software. I wanted to find out if the problem was with the data sent from the HC-06 to port 1, or with the reading function of port 1. I basically had serial port 0 read in data, then send it serially to port 1, which would read that data, and send feedback over Bluetooth to the Android device. Serial port 1 read the data fine coming from port 0, so the issue is reading in data specifically from the HC-06. It may simply be a voltage level issue, so the answer may not belong with Stack Overflow. I will leave the question unanswered though until I definitively have found the root cause (allowing for the possibility that I might need some define statement for the HC-06 or serial port 1 for data to be read correctly, though I'm guessing a voltage level conversion may do the trick. I'm not sure why there would be such a difference between Serial0 and Serial1 though).</p>