从串口合并 XBee api 个片段

Merge XBee api fragments from serial port

我尝试合并我的 Xbee (802.15.4) 在 API 模式下通过我的 RS-232 串口接收到的数据包片段。我使用 java.

的 jssc 库中的 readBytes 方法获取片段

这是我的接待码

    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: 2000123200ff1200
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) 中看到的那样将结束定界符发送到指定的数据包结尾。

我想过使用下一个数据包的开始分隔符,但没有成功。

我使用 api 数据包的长度计算出来的。由于长度位于第一个数组的 [1] 和 [2] 位置,因此我可以使用此值在接收数据时继续构建我的数据包。当我的 msgLengthLeft 值为 0 时,我知道数据包已全部收到,我可以 return 完整的数据包。这是我的代码:

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);
            }
        }
    }
}