0

我尝试通过我的 RS-232 串行端口以 API 模式合并我的 Xbee (802.15.4) 收到的数据包的片段。我使用 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 模式,因此我无法像在其他解决方案中看到的那样发送结束分隔符来指定数据包的结尾(读取完整行 Java 串行端口)。

我正在考虑使用下一个数据包的起始分隔符,但没有成功。

4

1 回答 1

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);
            }
        }
    }
}
于 2016-01-24T20:33:53.097 回答