打开串行端口时严重滞后

eaf3rand  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(158)

我经历了5秒的延迟和严重的滞后。我也尝试停止计时器,它将消除滞后,但串行端口不打开。可能是如果有其他方式,如改变我的行动监听或其他方式读取串行端口。

Timer timer ;   
ActionListener readRF = new ActionListener() {
        @Override
             public void actionPerformed(ActionEvent e) {
                    String str;
SerialPort serialPort = new SerialPort("COM3");
    //In the constructor pass the name of the port with which we work

    try {
        //Open port
        serialPort.openPort();

        //We expose the settings. You can also use this line - serialPort.setParams(9600, 8, 1, 0);
        serialPort.setParams(SerialPort.BAUDRATE_9600,
                SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE);

        Thread.sleep(1000);//Very important !!!

        //Read data from port
        for (int i = 0; i < 30; i++) {
            str = serialPort.readString();

            if (str != null) {
                    System.out.println(str);
                if(str.contains("15538849")){
                queue1.setText(String.valueOf(q.Table1Peek()));
                }else if(str.contains("15538850")){
                q.Table1DeQueue();    
                queue1.setText(String.valueOf(q.Table1Peek()));
                }
            }

            Thread.sleep(100);
        }

        //Closing the port
        serialPort.closePort();

    } catch (SerialPortException ex ) {
        System.out.println(ex);
    } catch (InterruptedException ex) {
        System.out.println(ex);
    }
            }

    };
                    timer = new Timer(1000,readRF);
                    timer.setInitialDelay(0);
                    timer.start();

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题