title: Serial communication Python to Arduino - can't recognise carriage return
body: <p>I'm trying to communicate via serial between a BeagleBone running Ubuntu 10.04 and an Arduino. So far, I can send a string okay, but I'm having problems recognising the newline character.</p>
  
  <p>From the BB side, in Python3 and using PySerial, I have (edited for just the relevant bits):</p>
  
  <pre><code>import serial
  ser = serial.Serial('/dev/ttyACM0', 9600)
  
  ser.write(chr(13).encode('ascii')) # establishes serial connection 
                                     # which will reset Arduino
  
  delay(3000) # give time for reset. This method is from my own library
  
  mesg = 'Beagle\n'
  ser.write(mesg.encode('ascii'))
  </code></pre>
  
  <p>On the Arduino side I have:</p>
  
  <pre><code>boolean newMsg = false;
  String incomingMsg = "";
  
  void setup()
  {
     /* some stuff - not relevant */
     Serial.begin(9600);
  }
  
  void loop()
  {
      if(newMsg) {
          lcd.print(incomingMsg);
          newMsg = false;
      }
  }
  
  void serialEvent()
  {
      incomingMsg = "";
      while (Serial.available() &gt; 0) {
          char inByte = Serial.read();
          if (inByte == '\n') {
              newMsg = true;
          } else {
              incomingMsg = incomingMsg + inByte;
          }
      }
  }
  </code></pre>
  
  <p>The problem is that newMsg never gets set to true because the <code>if (inByte == '\n')</code> never gets validated as true. I'even tried using '\r' on both sides and even a type-able character ('#'), but this test never works. The string 'Beagle' makes it through fine, though, so <code>incomingMsg</code> builds up okay.</p>
  
  <p>Weirdly, I have this working between an Arduino and Processing (the Arduino on a robot connecting to Processing on a laptop via Bluetooth). I just can't see why it isn't working here. Any thoughts?</p>
  
  <p><strong>UPDATE</strong>: I have a partial answer, and one that will work for now, although I'd still like to know what's broken.</p>
  
  <p>The <code>if (inByte == '\n')</code> is getting validated as true. But for some reason, the assignment of true to the global boolean var <code>newMsg</code> isn't working. Despite the assignment in serialEvent(), its value remains resolutely false inside loop(). Yet it's a global var (which, despite most programmers' aversion to them, are used quite a bit with the Arduino). I replaced the test in loop() with:</p>
  
  <pre><code>if (messageReceived != "")
  </code></pre>
  
  <p>and now it works. But I'd still like to know why that boolean problem exists.</p>