简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM