Reputation: 459
I am trying to read serial port from Processing. In that purpose I am trying basic hello world example. I write Hello world! from Arduino and try to catch it with Processing. Here are the codes:
Here is the code for Arduino Uno:
void setup()
{
//initialize serial communications at a 9600 baud rate
Serial.begin(9600);
}
void loop()
{
//send 'Hello, world!' over the serial port
Serial.println("Hello, world!");
//wait 100 milliseconds so we don't drive ourselves crazy
delay(100);
}
Here is the code for Processing:
import processing.serial.*;
Serial myPort; // Create object from Serial class
String val; // Data received from the serial port
String check = "Hello, world!";
String portName = Serial.list()[1]; //COM4
void setup() {
myPort = new Serial(this, portName, 9600);
println("Starting Serial Read Operation");
}
void draw()
{
if ( myPort.available() > 0) { // If data is available,
val = myPort.readStringUntil('\n');
println(val);
if (val != null && val.equals("Hello, world!") == true) {
println("Found the starting Point");
}
}
}
I cannot catch the check string.
Output of the Processing :
null
Hello, world!
null
Hello, world!
null
Hello, world!
Hello, world!
According to the output, I can successfully read the serial port. (However there are many nulls, I don't know why.) But I cannot catch the specified string.
Do you have an idea what the problem can be?
Regards
Upvotes: 1
Views: 616
Reputation: 1485
The Arduino sends \r\n
when using println.
When you compare, it fails as you are comparing "Hello, world!\r"
with "Hello, world!"
.
You can solve this by using Serial.print()
and manually adding an \n
to your strings or sending a Serial.write('\n');
after your text (repetition can be replaced with a helper function).
Upvotes: 1