3

I am using Arduino and Processing. I am trying to send a string from the Arduino to Processing

void serialDataOutput() {
 dataString = "";
 for (int i = 2; i <= 13; i++) {
if (digitalRead(i) == HIGH) {
  dataString.concat(i);
  dataString.concat(",1/");
} else {
  dataString.concat(i);
  dataString.concat(",0/");
}
}
  //output "2,0/3,0/4,0/5,0/6,0/7,0/8,1/9,1/10,1/11,1/12,1/13,0";
  Serial.write(dataString);

This is the code I have for generating the string, and an example output in the serial terminal.

However, in Processing, I am trying to get this string like this:

while (myPort.available() > 0) {
  rawInput = myPort.readString();
  println(rawInput);
  myPort.clear();
}

This gives an output like this:

2,0/3,0
/4,0/5,0/6,0/7,0
/8,1/9,1/10,1/11
,1/12,1/13,1

It is broken up over multiple lines. I need the input in processing exactly how it was sent from Arduino.

How do I do this?

2
  • Very interesting behaviour... I will stay tuned for more info. Could it be that the arduino didn't finished the transmission but Processing has emptied the buffer so stop reading and prints the string? The arduino continues writing in the Processing's serial buffer, meanwhile Processing writes on console, when it check again ´myPort.available()´ Arduino has filled it then prints again. Commented Oct 13, 2015 at 8:00
  • Try reading and fill the string until you read ´'\0'´(end of string) Commented Oct 13, 2015 at 8:01

1 Answer 1

1

I think that your are reading the input buffer of Processing faster than your are sending the complete string from Arduino. Processing read the buffer because it has info ( myPort.available() > 0) but arduino is sending info. When the buffer is empty, Processing understand that it is the end of the string so it stops reading and print it. In next iteration of loop(), Arduino has sended info and Processing repeat until Arduino end. Try reading the string char-by-char and concat it to a string until you read \0

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

2 Comments

the idea was right, it was reading too fast, i put a delay in the send function and it all works fine, curiously it does not matter what the delay is, even delay(0); still make it read the full string
I think that is a better idea to read one-by-one till \0 to ensure you that you has read a complete string and then analize it or activate what you want.

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.