【问题标题】:How can I receive data from Arduino?如何从 Arduino 接收数据?
【发布时间】:2012-08-08 17:08:47
【问题描述】:

我想通过串口从 Arduino 接收数字引脚值 1(高)或 0(低)。

我无法使用以下代码读取任何数据:

byte[] bbuffer = new byte[1];

try {
    mInputStream.read(bbuffer);
} 
catch (IOException e) {
    new Exception("Arduino-Board antwortet nicht! Timeout!!");
}

if (bbuffer[0] == (byte)1){
    PinValue.setText("HIGH");
}
else if (bbuffer[0] == (byte)0){
    PinValue.setText("LOW");
}

【问题讨论】:

  • 请注意代码格式。这次我给你修好了

标签: java android serial-port arduino


【解决方案1】:

您没有在问题中提供相关信息:您如何设置InputStream

但是您的代码仍然存在一些缺陷:

mInputStream.read(bbuffer);

InputStream.read 的一般约定是,您必须检查read 的返回值。如果read 返回零或负数,则bbuffer 的内容保持不变。

new Exception("Arduino-Board antwortet nicht!超时!!");

你只是创建一个新的异常,但你不会抛出它。您必须将throw 添加到该行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多