BufferedReader を使用して、Arduino デバイスから ZigBee ネットワーク フレームを定期的に受信する USB ゲートウェイからデータを読み取ります。
フレームは次のようになります。
~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.");
}
}
何がうまくいかないのかについてのアイデアはありますか?