我试图从处理中读取串口。为此,我正在尝试一个基本的hello world示例。我写“你好世界”!从Arduino开始试着用处理的方法抓住它。以下是密码:
以下是Arduino Uno的代码:
void setup()
{
//initialize serial communications at a 9600 baud rate
Serial.begin(9600);
}
void loop()
{
//send 'Hello, world!' over the serial port
Serial.println("Hello, world!");
//wait 100 milliseconds so we don't drive ourselves crazy
delay(100);
}以下是用于处理的代码:
import processing.serial.*;
Serial myPort; // Create object from Serial class
String val; // Data received from the serial port
String check = "Hello, world!";
String portName = Serial.list()[1]; //COM4
void setup() {
myPort = new Serial(this, portName, 9600);
println("Starting Serial Read Operation");
}
void draw()
{
if ( myPort.available() > 0) { // If data is available,
val = myPort.readStringUntil('\n');
println(val);
if (val != null && val.equals("Hello, world!") == true) {
println("Found the starting Point");
}
}
}我抓不到支票串。
加工输出:
null
Hello, world!
null
Hello, world!
null
Hello, world!
Hello, world!根据输出,我可以成功地读取串口。(然而,有很多空白处,我不知道为什么。)但我无法捕获指定的字符串。
你知道问题出在哪里吗?
问候
发布于 2015-07-24 06:17:31
Arduino在使用println时发送\r\n。
当您比较时,它会失败,因为您正在比较"Hello, world!\r"和"Hello, world!"。
您可以通过使用Serial.print()并手动向字符串中添加一个\n或在文本之后发送一个Serial.write('\n');来解决这个问题(可以用一个助手函数替换重复)。
https://stackoverflow.com/questions/31594382
复制相似问题