Serial with Arduino

edited January 2014 in Arduino

Hey guys.

Im trying to write a processing sketch to receive and work with serial messages sent from my Arduino.

When viewed in the Arduino serial monitor, This is what is found:

 -----------------
Pollux Serial Debugging
(C) 2014
-----------------
Test Mode: True

When I use the following code in processing:

import processing.serial.*;

Serial myPort; 
String val;     


void setup() {
  myPort = new Serial(this, "COM5", 9600); 
}

void draw() {
  if ( myPort.available() > 0) 
  {  // If data is available,
  val = myPort.readStringUntil('\n');
  } 
println(val);
}

The output I get in the processing console is all the messages as expected, But the last one loops indefinitely, or if another message is sent that is looped instead.

Does anybody have any thoughts on how I can receive the message just once and print it, like the output I get from Arduino serial monitor.

Thanks

Answers

  • edited January 2014

    Try this?

    String prevVal = "";
    
    void draw() {
      if ( myPort.available() > 0)
      {  // If data is available,
        val = myPort.readStringUntil('\n');
      }
      if (!val.equals(prevVal))
      {
        println(val);
        prevVal = val;
      }
    }
    
  • I tried some code similar to that last night, and both my code and the code you suggested come up with Null pointer exceptions, Line 17 in the following.

    import processing.serial.*;
    
    Serial myPort; 
    String val;     
    String prevVal = "";
    
    
    void setup() {
      myPort = new Serial(this, "COM5", 9600); 
    }
    
    void draw() {
      if ( myPort.available() > 0)
      {  // If data is available,
        val = myPort.readStringUntil('\n');
      }
      if (!val.equals(prevVal))
      {
        println(val);
        prevVal = val;
      }
    }
    
  • edited January 2014

    Ah I see. That's because val is only initiated when a new message is being sent. The solution is simple... initiate val beforehand :)

    import processing.serial.*;
    
    Serial myPort;
    String val = "";    
    String prevVal = "";
    
    void setup() {
      myPort = new Serial(this, "COM5", 9600);
    }
    
    void draw() {
      if ( myPort.available() > 0)
      {  // If data is available,
        val = myPort.readStringUntil('\n');
      }
      if (!val.equals(prevVal))
      {
        println(val);
        prevVal = val;
      }
    }
    
  • Still no dice unfortunately, It appears the NPE occurs when the serial connects to the Arduino, and if I put some dummy values into the strings, they will print into the console, then the serial connects and the NPE occurs.

    Im using 2.0.3 Win 32 because the latest version has a bug in the Serial library and it was advised on the issues tracker to downgrade, would that be a cause?

  • That could be a bug. The solution is to check if the String is null before evaluating it:

    import processing.serial.*;
    
    Serial myPort;
    String val = "";   
    String prevVal = "";
    
    void setup() {
      myPort = new Serial(this, "COM5", 9600);
    }
    
    void draw() {
      if ( myPort.available() > 0)
      {  // If data is available,
        val = myPort.readStringUntil('\n');
      }
      if (val != null)
      {
        if (!val.equals(prevVal))
        {
          println(val);
          prevVal = val;
        }
      }
    }
    
  • That works exactly as expected, Thanks :)

  • Why not simply doing this:

    if ( myPort.available() > 0)
      {  // If data is available,
        val = myPort.readStringUntil('\n');
        println(val);
      }
    
Sign In or Register to comment.