阴鸦 2018-11-13 02:22 采纳率: 100%
浏览 1047

java GUI获取电子秤数据不完整问题

获取电子秤的数据如下:

    +53.1
8  g 

   +53.1
8  g 

   +53.1
8  g 

   +53.1
8  g 

确实应该获取的数据是53.08g,但是不知道为什么一个数据是分成两部分返回的
代码如下:

    /**
 * 从串口读取数据
 * @param serialPort 当前已建立连接的SerialPort对象
 * @return 读取到的数据
 * @throws ReadDataFromSerialPortFailure 从串口读取数据时出错
 * @throws SerialPortInputStreamCloseFailure 关闭串口对象输入流出错
 */
public static byte[] readFromPort(SerialPort serialPort) throws ReadDataFromSerialPortFailure, SerialPortInputStreamCloseFailure {

    InputStream in = null;
    byte[] bytes = null;

    try {
        in = serialPort.getInputStream();//取入数据
        int bufflenth = in.available();     //获取buffer里的数据长度

        while (bufflenth != 0) {                             
            bytes = new byte[bufflenth];    //初始化byte数组为buffer中数据的长度
            in.read(bytes);
            bufflenth = in.available();
        } 
    } catch (IOException e) {
        throw new ReadDataFromSerialPortFailure();
    } finally {
        try {
            if (in != null) {
                in.close();
                in = null;
            }
        } catch(IOException e) {
            throw new SerialPortInputStreamCloseFailure();
        }
    }

    return bytes;

}


/**
 * 以内部类形式创建一个串口监听类
 * @author zhong
 *
 */
private class SerialListener implements SerialPortEventListener {
    /**
     * 处理监控到的串口事件
     */
    public void serialEvent(SerialPortEvent serialPortEvent) {
        switch (serialPortEvent.getEventType()) {
            case SerialPortEvent.BI: // 10 通讯中断
                JOptionPane.showMessageDialog(null, "与串口设备通讯中断", "错误", JOptionPane.INFORMATION_MESSAGE);
                break;
            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 串口存在可用数据
                byte[] data = null;
                try {

                    if (serialPort == null) {//判断串口对象是否为空
                        JOptionPane.showMessageDialog(null, "串口对象为空!监听失败!", "错误", JOptionPane.INFORMATION_MESSAGE);
                    }else {
                        data = SerialTool.readFromPort(serialPort); //读取数据,存入字节数组
                        System.out.println(new String(data));
                        //自定义解析过程
                        if (data != null && data.length > 1) {  //检查数据是否读取正确
                                try {
                                    weight.setText(new String(data) + " g");
                                } catch (ArrayIndexOutOfBoundsException e) {
                                    JOptionPane.showMessageDialog(null, "数据解析过程出错,更新界面数据失败!请检查设备或程序!", "错误", JOptionPane.INFORMATION_MESSAGE);
                                    System.exit(0);
                                }
                        }else {
                            JOptionPane.showMessageDialog(null, "读取数据过程中未获取到有效数据!请检查设备或程序!", "错误", JOptionPane.INFORMATION_MESSAGE);
                            System.exit(0);
                        }
                    }   
                } catch (ReadDataFromSerialPortFailure | SerialPortInputStreamCloseFailure e) {
                    JOptionPane.showMessageDialog(null, e, "错误", JOptionPane.INFORMATION_MESSAGE);
                    System.exit(0); //发生读取错误时显示错误信息后退出系统
                }   
                break;
        }
    }
}

请知道的朋友说一下问题出现在哪里

  • 写回答

1条回答 默认 最新

  • 阴鸦 2018-11-13 02:23
    关注

    解決辦法:參考:https://bbs.csdn.net/topics/300069815

    public void serialEvent(SerialPortEvent event) {
            try {
                // 等待1秒钟让串口把数据全部接收后在处理
                Thread.sleep(delayRead);
            } 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 {
                    // 多次读取,将所有数据读入
                    // while (inputStream.available() > 0) {
                    // numBytes = inputStream.read(readBuffer);
                    // }
                    numBytes = inputStream.read(readBuffer);
                    changeMessage(readBuffer, numBytes);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            }
        }
    
        // 通过observer pattern将收到的数据发送给observer
        // 将buffer中的空字节删除后再发送更新消息,通知观察者
        public void changeMessage(byte[] message, int length) {
            byte[] temp = new byte[length];
            System.arraycopy(message, 0, temp, 0, length);
            System.out.println(new String(temp));
            System.out.println(1);
        }
    
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!