我正在尝试编写可视表,它将在实时常量基础上显示Modbus值。虽然我知道一切都是独立的,但我无法将这个UI变为多线程。就目前而言,这实际上会不断更新,但如果您尝试最小化,关闭或移动窗口,UI会冻结。这是因为" go()"是一个永无止境的循环,正在打断UI部分。
我想要的只是" initUI"加载,然后运行我的" go()"方法,它将更新我UI的第二列中的标签。
我知道大多数人都不会使用我正在使用的Modbus导入,但是有人可以告诉我在initUI和go()中实现asyncExec的位置和内容吗?我在互联网上找到的所有东西都没有告诉我如何正确地做到这一点,或者至少以我能理解的方式告诉我。
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Label;
import com.focus_sw.fieldtalk.*;
public class TableEx2 {
int y = 21; // Number of registers
int z = 0; // Starting registers offset
int x = 1;
int i;
int j = y + z; // Number of registers plus offset
int r = 1;
int b = 10;
int c = 1;
boolean a = false;
int ReadINT[] = new int[j];
String IPAdd = new String("127.0.0.1");
String Connecting = new String("Connecting...");
String Connected = new String("Connected to " + IPAdd);
Thread pollThread; // The cyclic poll thread
volatile boolean pollThreadExit; // Flag to safely terminate the thread
MbusTcpMasterProtocol mbus = new MbusTcpMasterProtocol(); // The protocol
short readDataTable[] = new short[j]; // The data table to be read
short writeDataTable[] = new short[j]; // The data table to be written
String ReadData[] = new String[j];
String RegNum[] = new String[j];
private Label label; // Must import org.eclipse.swt.widgets.Label; then
// declare it in the class to use it.
private Label status;
private Display display;
private Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER);
private Label ModbusValues[] = new Label[j];
public TableEx2(Display display) {
try {
initUI(display);
invalidateUiData();
}
catch (NoClassDefFoundError e) {
status.setText(e.toString());
}
}
private void initUI(Display display) {
shell.setLayout(new GridLayout(2, false));
Label RegisterLabels[] = new Label[j];
for (i = 1; i < j; i++) {
if (i % 2 == 1) {
GridData gd1 = new GridData(SWT.FILL, SWT.FILL, true, true);
RegisterLabels[i] = new Label(shell, SWT.BORDER);
RegisterLabels[i].setLayoutData(gd1);
gd1.widthHint = 100;
gd1.horizontalSpan = 1;
RegisterLabels[i].setText(Integer.toString(b));
b++;
} else {
GridData gd1 = new GridData(SWT.FILL, SWT.FILL, true, true);
ModbusValues[c] = new Label(shell, SWT.BORDER);
ModbusValues[c].setLayoutData(gd1);
gd1.widthHint = 100;
gd1.horizontalSpan = 1;
ModbusValues[c].getText();
c++;
}
}
GridData gd2 = new GridData(SWT.FILL, SWT.FILL, true, true);
status = new Label(shell, SWT.BORDER);
status.setLayoutData(gd2);
gd2.widthHint = 250;
gd2.heightHint = 15;
gd2.horizontalSpan = 2;
status.getText();
shell.setText("Modbus Tags");
shell.pack();
shell.open();
start();
go();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
public void start() {
try {
status.setText(Connecting);
mbus.openProtocol("127.0.0.1");
pollThreadExit = false;
pollThread = new Thread();
pollThread.start();
status.setText(Connected);
} catch (Exception e) {
status.setText("Unable to connect to " + IPAdd);
}
}
public void stop() {
try {
pollThreadExit = true; // Stop thread safely
try {
pollThread.join();
} catch (java.lang.InterruptedException e) { /* ignore */
}
mbus.closeProtocol();
invalidateUiData();
} catch (Exception e) {
label.setText(e.toString());
invalidateUiData();
}
}
void invalidateUiData() {
status.setText("Unable to see registers.");
display.dispose();
}
public void go() {
while (!pollThreadExit) {
try {
Thread.currentThread().sleep(50);
} catch (java.lang.InterruptedException e) {
}
try {
mbus.readMultipleRegisters(1, 10, readDataTable);
int ModbusAdd = 1;
int AddressMax = 10;
for (ModbusAdd = 1; ModbusAdd <= AddressMax; ModbusAdd++) {
ReadINT[ModbusAdd] = (int) (readDataTable[ModbusAdd]);
ModbusValues[ModbusAdd].setText(Integer
.toString(ReadINT[ModbusAdd]));
}
} catch (Exception e) {
status.setText("Invalid register polled");
invalidateUiData();
}
}
}
@SuppressWarnings("unused")
public static void main(String[] args) {
Display display = new Display();
TableEx2 ex = new TableEx2(display);
display.dispose();
}
}