Arduino 正在处理的串行端口读取,找不到字符串
我正在尝试从处理中读取串行端口。为此,我尝试了一个基本的hello world示例。我写你好世界!从Arduino开始,并尝试通过处理捕捉它。代码如下: 以下是Arduino Uno的代码:Arduino 正在处理的串行端口读取,找不到字符串,arduino,serial-port,processing,Arduino,Serial Port,Processing,我正在尝试从处理中读取串行端口。为此,我尝试了一个基本的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 p
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!
根据输出,我可以成功读取串口。(但是有很多空值,我不知道为什么。)但是我无法捕获指定的字符串
你知道问题是什么吗
关于使用println时Arduino发送的
\r\n
当您比较时,它会失败,因为您正在将“Hello,world!\r”
与“Hello,world!”
进行比较
您可以使用Serial.print()
并手动将\n
添加到字符串或发送Serial.write('\n')代码>在文本之后(重复可以替换为辅助函数)