从SwingWorker接收数据

时间:2018-04-13 01:19:44

标签: java swing concurrency port swingworker

我正在尝试使用SwingWorker通过串口读取数据。但是,执行此代码时,我没有收到任何输出。通过添加行

JOptionPane.showConfirmDialog(null, "Cancel", "Cancel this task?", JOptionPane.DEFAULT_OPTION);

正如SwingWorker not executing doInBackGround()中所建议的,我只接收输出"阅读" / n"完成",并且不调用我的getPortText方法。用于显示可用端口的IF块也不会执行。

可以在http://fazecast.github.io/jSerialComm/

找到fazecast SerialComm JAR

非常感谢所有帮助,我已经坚持了好几天了!

import java.util.List;
import java.util.Scanner;

import javax.swing.JOptionPane;
import javax.swing.SwingWorker;

import com.fazecast.jSerialComm.SerialPort;

public class PortWorkerCheck {

public static void main(String[] args) {
    Worker worker = new Worker();
    worker.execute();
}

public static void getPortText(String dir) {
    System.out.println(dir);
}

static class Worker extends SwingWorker<Void, String>{
    boolean portcheck = false;
    SerialPort comPort[] = SerialPort.getCommPorts();
    SerialPort port;
    protected void done() {
        System.out.println("Done");
    }

    @Override
    protected Void doInBackground() throws Exception {
        String data;
     //This if block attempts to list available ports and have the user select one.
        if (portcheck = false) {
            int i = 1;
            System.out.println("Select a port:");
            for(SerialPort ports : comPort) {
                System.out.println(i++ + ". " + ports.getSystemPortName());
            }

            Scanner s = new Scanner(System.in);
            int chosenPort = s.nextInt();
            port = comPort[chosenPort - 1];
            portcheck = true;
            if(port.openPort()) {
                System.out.println("Port opened successfully");
            }
            port.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER, 0, 0);
        }

        publish("Reading");
        Scanner read = new Scanner(port.getInputStream());
        data = read.nextLine();
        publish(data);
        return null;
    }

    //My process should call getPortText, which should then display the received data.
    @Override
    protected void process(List<String> chunks) {
        for(String line : chunks) {
        portcheck = true;
        PortWorkerCheck.getPortText(line);
        }
    }
}

}

1 个答案:

答案 0 :(得分:2)

问题:

  • 您发布的代码不会创建Swing GUI,因此永远不会启动Swing事件线程。这意味着当您的main方法退出并且任何其他非守护程序线程退出时,您的程序将退出。该发布/处理方法可能会在它执行任何有用之前死亡。解决方案:如果没有Swing GUI,请不要使用SwingWorkers。创建两者并连接它们。
  • 您的代码只尝试从端口读取一行数据,然后退出。您需要连接到扫描器的while循环读取端口
  • 您的if块布尔测试是错误的(根据评论)。切勿以这种方式进行if测试:if (portcheck = false) {。而是安全地做到:if (!portcheck) {
  • if块中的所有代码都不应该存在。您不应该将Swing事件驱动程序与基于控制台的Scanner混合使用。应通过Swing GUI获取数据。
  • 你说:"I put the worker.execute() into a while(true) loop" - 不,不要这样做。您不会重复执行worker,而只执行一次。这是一次使用 - 然后丢弃对象类型。相反,while循环在内部扫描程序获取数据的工作者。

类似的东西:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.InputStream;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.ExecutionException;

import javax.swing.*;

// not a class that I'm familiar with
import com.fazecast.jSerialComm.SerialPort;

public class PortWorkerCheckPanel extends JPanel {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            PortWorkerCheckPanel portCheckPanel = new PortWorkerCheckPanel();
            JFrame frame = new JFrame("Port Check");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(portCheckPanel);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }

    private JTextArea textArea = new JTextArea(20, 50);
    private StartWorkerAction startAction = new StartWorkerAction(this);

    public PortWorkerCheckPanel() {
        textArea.setFocusable(false);
        JScrollPane scrollPane = new JScrollPane(textArea);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        JPanel btnPanel = new JPanel();
        btnPanel.add(new JButton(startAction));

        setLayout(new BorderLayout());
        add(scrollPane);
        add(btnPanel, BorderLayout.PAGE_END);
    }

    public void appendPortText(String text) {
        textArea.append(text + "\n");
    }

    private static class StartWorkerAction extends AbstractAction {
        private PortWorkerCheckPanel portCheckPanel;

        public StartWorkerAction(PortWorkerCheckPanel portCheckPanel) {
            super("Start Worker");
            this.portCheckPanel = portCheckPanel;
        }

        @Override
        public void actionPerformed(ActionEvent arg0) {
            Worker worker = new Worker(portCheckPanel);
            worker.addPropertyChangeListener(new WorkerListener());
            worker.execute();
        }
    }

    static class WorkerListener implements PropertyChangeListener {
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            if (evt.getNewValue() == SwingWorker.StateValue.DONE) {
                Worker worker = (Worker) evt.getSource();
                try {
                    worker.get();
                } catch (InterruptedException | ExecutionException e) {
                    // TODO handle any exceptions here
                    e.printStackTrace();
                }
            }
        }
    }

    static class Worker extends SwingWorker<Void, String> {
        boolean portcheck = false;

        // Note that I have no idea if this part, the SerialPort code, is
        // correct
        // as I do not use this utility
        SerialPort comPort[] = SerialPort.getCommPorts();
        SerialPort port;
        private PortWorkerCheckPanel portCheckPanel;

        public Worker(PortWorkerCheckPanel portCheckPanel) {
            this.portCheckPanel = portCheckPanel;
        }

        protected void done() {
            System.out.println("Done");
        }

        @Override
        protected Void doInBackground() throws Exception {
            String data;

            // this code in the if block should all be done interactively in the
            // GUI,
            // not using a console-based Scanner the information should then be
            // passed
            // into this worker via a constructor parameter
            if (!portcheck) {
                int i = 1;
                System.out.println("Select a port:");
                for (SerialPort ports : comPort) {
                    System.out.println(i++ + ". " + ports.getSystemPortName());
                }

                Scanner s = new Scanner(System.in);
                int chosenPort = s.nextInt();
                port = comPort[chosenPort - 1];
                portcheck = true;
                if (port.openPort()) {
                    System.out.println("Port opened successfully");
                }
                port.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER, 0, 0);
            }

            publish("Reading");
            Scanner read = new Scanner(port.getInputStream());

            while (read.hasNextLine()) {
                data = read.nextLine();
                publish(data);
            }
            if (read != null) {
                read.close();
            }
            return null;
        }

        // My process should call getPortText, which should then display the
        // received data.
        @Override
        protected void process(List<String> chunks) {
            for (String line : chunks) {
                portcheck = true;
                // PortWorkerCheckPanel.getPortText(line);
                portCheckPanel.appendPortText(line);
            }
        }
    }
}