Java h(中断异常|执行异常e){ //TODO在此处理任何异常 e、 printStackTrace(); } } } } 静态类工作程序扩展SwingWorker{ 布尔端口检查=false; //请注意,我不知道这一部分(SerialPort代码)是否正确

Java h(中断异常|执行异常e){ //TODO在此处理任何异常 e、 printStackTrace(); } } } } 静态类工作程序扩展SwingWorker{ 布尔端口检查=false; //请注意,我不知道这一部分(SerialPort代码)是否正确,java,swing,concurrency,port,swingworker,Java,Swing,Concurrency,Port,Swingworker,h(中断异常|执行异常e){ //TODO在此处理任何异常 e、 printStackTrace(); } } } } 静态类工作程序扩展SwingWorker{ 布尔端口检查=false; //请注意,我不知道这一部分(SerialPort代码)是否正确 //正确的 //因为我不使用这个工具 SerialPort comPort[]=SerialPort.getCommPorts(); 串行端口; 专用PortWorker检查面板portCheckPanel; 公共工作者(PortWorker

h(中断异常|执行异常e){ //TODO在此处理任何异常 e、 printStackTrace(); } } } } 静态类工作程序扩展SwingWorker{ 布尔端口检查=false; //请注意,我不知道这一部分(SerialPort代码)是否正确 //正确的 //因为我不使用这个工具 SerialPort comPort[]=SerialPort.getCommPorts(); 串行端口; 专用PortWorker检查面板portCheckPanel; 公共工作者(PortWorker检查面板portCheckPanel){ this.portCheckPanel=portCheckPanel; } 受保护的void done(){ 系统输出打印项次(“完成”); } @凌驾 受保护的Void doInBackground()引发异常{ 字符串数据; //if块中的这些代码都应该在 //桂,, //如果不使用基于控制台的扫描仪,则应删除信息 //通过 //通过一个构造函数参数输入到这个worker中 如果(!端口检查){ int i=1; System.out.println(“选择端口:”); 用于(串行端口:comPort){ System.out.println(i++“+ports.getSystemPortName()); } 扫描仪s=新的扫描仪(System.in); int chosenPort=s.nextInt(); port=comPort[chosenPort-1]; portcheck=true; if(port.openPort()){ System.out.println(“端口已成功打开”); } port.SetComportTimeout(SerialPort.TIMEOUT\u扫描器,0,0); } 出版(“阅读”); 扫描器读取=新扫描器(port.getInputStream()); while(read.hasNextLine()){ data=read.nextLine(); 发布(数据); } 如果(读取!=null){ read.close(); } 返回null; } //我的进程应该调用getPortText,然后它应该显示 //收到的数据。 @凌驾 受保护的无效进程(列表块){ for(字符串行:块){ portcheck=true; //PortWorkerCheckPanel.getPortText(行); portCheckPanel.appendPortText(行); } } } }
您没有运行Swing GUI,因此当主方法退出时,此代码有退出的风险。您需要显示一个Swing GUI,Swing线程才能继续运行。另外,您是否只需要读取一行数据?。。。。这行代码很糟糕:
if(portcheck=false){
。永远不要设置这样的布尔值。如果(portcheck=false){或者更好的
if(!portcheck){
如果您还记得之前帮助过我的问题,那么数据将以一系列单行的形式传输到端口:您的代码只尝试读取一行。请再次更改不正确的if块。您正在使用该行将布尔值设置为false--不好。
JOptionPane.showConfirmDialog(null, "Cancel", "Cancel this task?", JOptionPane.DEFAULT_OPTION);
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);
        }
    }
}
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);
            }
        }
    }
}