我正在尝试使用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);
}
}
}
}
答案 0 :(得分:2)
问题:
if (portcheck = false) {
。而是安全地做到:if (!portcheck) {
"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);
}
}
}
}