0
我嘗試合併我的Xbee(802.15.4)通過我的RS-232串行端口以API模式接收的數據包的片段。我使用java的jssc庫中的readBytes方法得到我的片段。從串口合併XBee API片段
這裏是我接待的代碼
private static class PortReader implements SerialPortEventListener {
@Override
public void serialEvent(SerialPortEvent event) {
if(event.isRXCHAR() && event.getEventValue() > 0) {
try {
byte[] receivedData = serialPort.readBytes(event.getEventValue());
if(Utils.isConfirmPacket(Utils.toHex(receivedData))){
System.out.println("XBee confim that msg is sent!");
}else{
System.out.println("Received response from port: " + Utils.toHex(receivedData));
int dataLength = Utils.toHex(receivedData).length();
receiveFile("testRx", receivedData, dataLength);
}
}
catch (SerialPortException ex) {
System.out.println("Error in receiving response from port: " + ex);
}
}
}
我receiveFile()方法需要完整的數據包在一個片段。下面是我得到了我的控制檯:
Received response from port: 7e003c8100013200
Received response from port: 200ff1200
Received response from port: 0001000500000000
Received response from port: 0000000020000000
Received response from port: 1251000012455222
Received response from port: 446174613a206365
Received response from port: 6369206573742075
Received response from port: 6e20746573743b90
由於我使用的API模式,我一直沒能結束符發送到數據包中指定的結束就像我在其他的解決辦法看到(Read Complete Line Java Serial Port )。
我正在考慮使用下一個數據包的開始分隔符,但它沒有解決。
考慮使用這個庫,而不是重新發明輪子。它應該更容易將數據作爲幀而不是流處理。 https://docs.digi.com/display/XBJLIB/XBee+Java+Library – tomlogic