我正在使用 BufferedReader 从 USB 网关读取数据,该网关定期从 Arduino 设备接收 ZigBee 网络帧。
这就是框架应该看起来的样子:
~f�}3�@v<-,R#}3�@v<--mac:0013A20040763C2D -H:-25.80 -T:22.58 -L:2.6451 -N:100.00 -D:0.0290 -B:35
但相反,它总是在 MAC 地址末尾附近缺少一些字符,如下所示:
~f�}3�@v<-,R#}3�@v<--mac:0013A2004076D -H:-25.80 -T:22.58 -L:2.6451 -N:100.00 -D:0.0290 -B:35
或者
~f�}3�@v<-,R#}3�@v<--mac:0013A2004076C2:-25.80 -T:22.58 -L:2.6451 -N:100.00 -D:0.0290 -B:35
我猜一开始的垃圾是低级网络头信息。
我在 Ubuntu 上,从终端读取时帧显示得非常好,使用
cat /dev/ttyUSB0
我用来从 USB 端口读取的代码如下所示。它在自己的线程中运行。
public void run() {
Boolean keepRunning = true;
BufferedReader br = new BufferedReader(new InputStreamReader(portReader.getInputStream()));
String line;
while (keepRunning) {
try {
while ((line = br.readLine()) != null) {
handleData(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
我正在使用 RXTXcomm.jar,可在此处获得http://rxtx.qbang.org/wiki/index.php/Main_Page
这是我打开端口的地方。
while (!connected && !timedOut) {
System.out.print("\n\nConnecting to " + portName);
//Open Ports
CommPort commPort = portIdentifier.open(this.getClass()
.getName(), 9600);
//TODO Should we rule out other kinds?
if (commPort instanceof SerialPort) {
//Pass the open port
SerialPort serialPort = (SerialPort) commPort;
serialPort.enableReceiveTimeout(15000);
//Configure the port communication interface
serialPort.setSerialPortParams(bauds,
SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
//Open a stream and read from the port
inputStream = serialPort.getInputStream();
int portBuffer = inputStream.read();
//Check if there is something in the buffer, which
//means that a connection was established
if (portBuffer > -1) {
connected = true;
} else {
System.err.println("Connection to " + portName
+ " timed out");
serialPort.close();
inputStream.close();
timedOut = true;
}
} else {
System.err
.println("Error: Only serial ports are handled by this application.");
}
}
关于可能出现问题的任何想法?