Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complete Serial String from Arduino to Processing

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?

like image 622
Oisian Avatar asked Feb 07 '26 03:02

Oisian


1 Answers

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

like image 153
jabujavi Avatar answered Feb 09 '26 15:02

jabujavi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!