2016-01-22 53 views
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 )。

我正在考慮使用下一個數據包的開始分隔符,但它沒有解決。

+0

考慮使用這個庫,而不是重新發明輪子。它應該更容易將數據作爲幀而不是流處理。 https://docs.digi.com/display/XBJLIB/XBee+Java+Library – tomlogic

回答

0

我想出了使用api數據包的長度。由於長度位於第一個數組的位置[1]和[2],因此我可以使用此值在我接收數據時繼續構建數據包。當我的msgLengthLeft值爲0時,我知道數據包已全部收到,並且我可以返回完整數據包。這裏是我的代碼:

private static class PortReader implements SerialPortEventListener { 
    private short msgLength; 
    private short msgLengthLeft; 
    private byte[] receivedMsg = new byte[packetSize]; 
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 

    @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{ 
        if(receivedData[0] == 0x7e){ //0x7e is the start delimiter of a frame 
         ByteBuffer wrapped = ByteBuffer.wrap(receivedData,1,2); // big-endian by default 
         msgLength = (short) (wrapped.getShort()+4); //Need to add 4 to the value becaus some field are not include in the length field of the api packet 
         msgLengthLeft = (short) (msgLength - receivedData.length); 
         outputStream.write(receivedData); 
         return; 
        }else{ 
         msgLengthLeft = (short) (msgLengthLeft - receivedData.length); 
         outputStream.write(receivedData); 
         if(msgLengthLeft != 0){ 
          return; 
         } 
        } 
        System.out.println("Received response from port: " + Utils.toHex(outputStream.toByteArray())); 
        int dataLength = Utils.toHex(receivedMsg).length(); 
        //receiveFile("testRx", receivedData, dataLength); 
       } 

      } 
      catch (SerialPortException | IOException ex) { 
       System.out.println("Error in receiving response from port: " + ex); 
      } 
     } 
    } 
}