0

I'm trying to set up communications between my PC, and my Arduino with the Processing environment, but the Arduino doesn't seem to be getting any of the messages I send. I doubled checked, and I know I can receive messages from the Arduino, but I can't send anything back. Does anyone know how to fix this?

Here's my test code for processing:

import processing.serial.*;
Serial myPort;

void setup(){
  myPort = new Serial(this, Serial.list()[0], 9600);
}

void draw(){
  myPort.write("test");
  while (myPort.available() > 0) {
    String inByte = myPort.readString();
    println(inByte);
  }
}

Here's my test code for the Arduino:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

String data;
void loop() {
  // put your main code here, to run repeatedly:
  //Serial.println("is running");
  if (Serial.available() > 0) {
    // read the incoming byte:
    data = Serial.readString();

    // say what you got:
    Serial.print("I received: ");
    Serial.println(data);
  }
}

I'd appreciate any help I can get! Thanks!

2
  • While that's true, the draw() function loops repeatedly until the program stops, so even if it misses the first serial message, it'll get the next one. Commented Feb 22, 2019 at 23:11
  • Ah, you are right. Commented Feb 22, 2019 at 23:19

1 Answer 1

1

Okay, after looking into several different posts on the Arduino forum, I figured out what the problem is. The function processing uses to send data over serial does not automatically include a return character at the end of the string. This is important because the Arduino won't read from the serial buffer until it sees that return character. All I had to do was add "\r\n" to the end of each string I sent over serial, and that solved the problem!

Sign up to request clarification or add additional context in comments.

2 Comments

Serial.readString() doesn't wait for newline characters. It just waits until it gets nothing for 1 second. If this worked for you then you have different code on the Arduino than in your question. Are you using readStringUntil?
No, I'm just using readString on the Arduino. I wasn't getting any communication until I added that return character to the end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.