当前位置:编程学习 > JAVA >>

关于RXTX串口缓存数据的处理

最近使用RXTX进行linux下串口数据开发, 我做的是对硬件设备数据采集,但串口打开后多次发送采集指令,第一次正常,后面每次都会把之前的数据给返回了。我想把每次采集处理后的串口缓存数据清空,我该如何处理?先谢谢啦。


    class PortListener implements SerialPortEventListener {
        /**
         * 串口消息事件
         *
         * @param event
         */
        public void serialEvent(SerialPortEvent event) {
            SerialStatus serialStatus=new SerialStatus();
            try {
                // 等待delayRead秒钟让串口把数据全部接收后在处理
                Thread.sleep(delayRead);
                logger.debug("serialEvent[" + event.getEventType() + "]    ");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            switch (event.getEventType()) {
                case SerialPortEvent.BI: // 10
                case SerialPortEvent.OE: // 7
                case SerialPortEvent.FE: // 9
                case SerialPortEvent.PE: // 8
                case SerialPortEvent.CD: // 6
                case SerialPortEvent.CTS: // 3
                case SerialPortEvent.DSR: // 4
                case SerialPortEvent.RI: // 5
                case SerialPortEvent.OUTPUT_BUFFER_EMPTY: // 2
                    break;
                case SerialPortEvent.DATA_AVAILABLE: // 1
                    try {
                        bufferSize=0;
                        readBuffer=new byte[1024];
                        // 多次读取,将所有数据读入
                        while (inputStream.available() > 0) {
                            bufferSize = inputStream.read(readBuffer,0,readBuffer.length);
                        }

                        serialStatus.setStatus(true);
                        serialStatus.setDataBuff(readBuffer);
                        serialStatus.setBuffLength(bufferSize);
                        //清除串口缓存
                        
                        //将串口返回的消息通知观察者
                        notifyMessage(serialStatus);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
    }
串口缓存清除 --------------------编程问答-------------------- 这么几天了就没个人帮忙的么?
补充:Java ,  Java SE
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,