【发布时间】:2017-07-11 20:29:18
【问题描述】:
我在使用 arduino 时遇到了一些问题。在课堂上,我们正在学习 arduino/java 通信。因此,我们被要求解释从 arduino 发送的字节并将其写在 Eclipse 的控制台中,因为消息的“密钥”告诉我们将其写入。
到目前为止,我只是在测试输入流,但我似乎无法获得完整的消息。这就是我正在做的:
public void run() throws SerialPortException {
while (true) {
if (port.available()) { //code written in another class, referenced below
byte byteArray[] = port.readByte(); //also code written in another class, referenced below
char magicNum = (char) byteArray[0];
String outputString = null;
for (int i = 0; i < byteArray.length; ++i) {
char nextChar = (char) byteArray[i];
outputString += Character.toString(nextChar);
}
System.out.println(outputString);
}
}
}
下面是上面代码中使用的另一个类的代码
public boolean available() throws SerialPortException {
if (port.getInputBufferBytesCount() == 0) {
return false;
}
return true;
}
public byte[] readByte() throws SerialPortException {
boolean debug= true;
byte bytesRead[] = port.readBytes();
if (debug) {
System.out.println("[0x" + String.format("%02x", bytesRead[0]) + "]");
}
return bytesRead;
}
【问题讨论】:
-
我忘了说,我从 arduino 接口输入的输入流是“这是一个测试”,我得到的输出是“nullthis is a”和“nulla test”或只是“null”
标签: java eclipse arduino communication