简体   繁体   English

从Arduino到Processing的完整串行字符串

[英]Complete Serial String from Arduino to Processing

I am using Arduino and Processing. 我正在使用Arduino和Processing。 I am trying to send a string from the Arduino to Processing 我正在尝试从Arduino发送一个字符串到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: 但是,在Processing中,我试图得到这样的字符串:

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. 我需要输入处理从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的输入缓冲区比从Arduino发送完整字符串更快。 Processing read the buffer because it has info ( myPort.available() > 0 ) but arduino is sending info. 处理读取缓冲区因为它有info( myPort.available() > 0 )但是arduino正在发送信息。 When the buffer is empty, Processing understand that it is the end of the string so it stops reading and print it. 当缓冲区为空时,Processing知道它是字符串的结尾,因此它会停止读取并打印它。 In next iteration of loop() , Arduino has sended info and Processing repeat until Arduino end. loop()下一次迭代中,Arduino已经发送了信息并且处理重复,直到Arduino结束。 Try reading the string char-by-char and concat it to a string until you read \\0 尝试读取字符串char-by-char并将其连接到字符串,直到您读取\\0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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