从 SwingWorker 接收数据

Receiving data from SwingWorker

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

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

中所建议,我只收到输出 "Reading" /n "Done",并且未调用我的 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);
        }
    }
}

}

问题:

  • 您发布的代码不会创建 Swing GUI,因此 Swing 事件线程永远不会启动。这意味着当您的 main 方法退出并且任何其他非守护线程退出时,您的程序也会退出。 publish/process 方法可能会在它做任何有用的事情之前就死掉。解决方案:不要在没有 Swing GUI 的情况下使用 SwingWorkers。创建并连接它们。
  • 您的代码只尝试从端口读取一行数据,然后 quits.You 需要一个 while 循环附加到读取端口的扫描器
  • 你的 if 块布尔测试是错误的(根据评论)。永远不要以这种方式进行 if 测试:if (portcheck = false) {。而是安全地进行:if (!portcheck) {
  • 不应出现 if 块中的所有代码。您不应该将 Swing 事件驱动程序与基于控制台的扫描程序混合使用。应通过 Swing GUI 获取数据。
  • 您声明:"I put the worker.execute() into a while(true) loop" -- 不,不要这样做。您不会重复执行 worker,而是只执行一次。这是一个使用一次然后扔掉的对象类型。相反,while 循环进入 inside 扫描程序正在获取数据的工作人员。

类似于:

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);
            }
        }
    }
}