gpt4 book ai didi

java - 从 Arduino 读取数据到处理中返回空结果

转载 作者:行者123 更新时间:2023-12-01 11:59:52 30 4
gpt4 key购买 nike

我想从Arduino读取数据。我的 Arduino 代码很简单:

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;
String val;

void setup() {

String portName = Serial.list()[1];
myPort = new Serial(this, portName, 9600);
}

void draw() {

if (myPort.available() > 0)
{
// If data is available,
val = myPort.readStringUntil('\n');
}
println(val); // pr
}

但是 val 始终为 Null。我不明白为什么它每次都返回这个值。端口可用。

最佳答案

在处理中,如果您输入:

if (val == null) {
val = "0";
}

它应该可以工作。它将继续打印“val”而不停止程序,但它会返回值“0”而不是“null”。

基本上,即使它收到“null”值,您也必须告诉处理运行。

关于java - 从 Arduino 读取数据到处理中返回空结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28055194/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com