我正在设计一个GUI,它将通过串口与微控制器进行交互。 GUI将查询微控制器,接收的数据将在表中更新。查询时间是可编程的,即GUI将根据指定的时间从1秒到30秒进行查询。
这就是我所做的 -
Table
进行数据更新Spinner
查询时间Button
。有两个java文件GUI.java和serialCommunication.java。具有时间任务的计时器被调度以查询微控制器,并且来自串行事件的数据通过GUI缓冲器在表中更新。
一切都很好,但有时候在缓冲区中存储了许多值后表格会更新 请帮助我同步所有数据获取和更新。我使用display.asyncExec()写入Table。
public class practice {
static Table table;
private SerialComm myCommNew = new SerialComm();
private FileOperation myFile = new FileOperation();
private Text txtOutputData;
public int tblIndex=0;
public static String FILTER_PATH = "C:\\";
private static boolean hasDiagnoseData;
Display display = Display.getDefault();
private static int ijk;
private int timerValue;
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.addControlListener(new ControlAdapter() {
@Override
public void controlResized(ControlEvent e) {
System.out.println(shell.getClientArea());
}
});
shell.addShellListener(new ShellAdapter() {
@Override
public void shellClosed(ShellEvent evtShellClosed) {
if (exitApplication()) {
myCommNew.closePort();
System.out.println("Port Closed Successfully");
evtShellClosed.doit = true;
System.exit(0);
} else {
evtShellClosed.doit = false;
}
}
});
shell.setSize(551, 548);
shell.setText("SWT Application");
shell.setLayout(null);
final Label lblMessage = new Label(shell, SWT.NONE);
lblMessage.setFont(SWTResourceManager.getFont("Segoe UI", 12, SWT.BOLD));
lblMessage.setBounds(292, 64, 155, 33);
final Combo comboPortsList = new Combo(shell, SWT.READ_ONLY);
comboPortsList.setToolTipText("Select COM Port");
comboPortsList.select(0);
comboPortsList.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
String y = comboPortsList.getText();
lblMessage.setText(y + " selected");
try {
myCommNew.connect(y);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
Button btnSearch = new Button(shell, SWT.NONE);
btnSearch.addSelectionListener(new SelectionAdapter(){
@Override
public void widgetSelected(SelectionEvent e) {
String[] no = myCommNew.getPorts();
comboPortsList.setItems(no);
myCommNew.closePort();
System.out.println("Finished successfully");
}
});
comboPortsList.setBounds(218, 21, 91, 23);
comboPortsList.setText("Select Port");
//comboPortsList.setItems(portsNumber);
btnSearch.setBounds(364, 15, 161, 33);
btnSearch.setText("Search for Available Ports");
// *********** *表************************************************ *********** //
table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
table.setBounds(10, 193, 512, 112);
table.setHeaderVisible(true);
table.setLinesVisible(true);
TableColumn tblclmnSrNo = new TableColumn(table, SWT.NONE);
tblclmnSrNo.setWidth(100);
tblclmnSrNo.setText("Sr. No.");
TableColumn tblclmnDate = new TableColumn(table, SWT.NONE);
tblclmnDate.setWidth(100);
tblclmnDate.setText("Date");
TableColumn tblclmnValues = new TableColumn(table, SWT.NONE);
tblclmnValues.setWidth(100);
tblclmnValues.setText("Values");
//final TableItem newRow = new TableItem(table, SWT.NONE);
// *********** * MENU ************************************************ *********** //
Menu menu = new Menu(shell, SWT.BAR);
shell.setMenuBar(menu);
MenuItem mntmfile = new MenuItem(menu, SWT.CASCADE);
mntmfile.setText("&File");
Menu menu_1 = new Menu(mntmfile);
mntmfile.setMenu(menu_1);
MenuItem mntmopen = new MenuItem(menu_1, SWT.PUSH);
mntmopen.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
FILTER_PATH = "C:\\";
myFile.readFileCSV(myFile.openFileDlg());
}
});
mntmopen.setText("&Open");
MenuItem mntmsave = new MenuItem(menu_1, SWT.PUSH);
mntmsave.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
String fileNameReceived = myFile.saveFileDlg();
if(fileNameReceived!=null) {
myFile.writeFileCSV(myFile.saveFileDlg());
}
}
});
mntmsave.setText("&Save");
MenuItem mntmExit = new MenuItem(menu_1, SWT.NONE);
mntmExit.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
shell.close();
}
});
mntmExit.setText("E&xit");
MenuItem mntmhelp = new MenuItem(menu, SWT.CASCADE);
mntmhelp.setText("&Help");
Menu menu_2 = new Menu(mntmhelp);
mntmhelp.setMenu(menu_2);
MenuItem mntmAbout = new MenuItem(menu_2, SWT.NONE);
mntmAbout.setText("About");
Button btnSend = new Button(shell, SWT.NONE);
btnSend.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
String newData = txtOutputData.getText();
myCommNew.sendData(newData+"\n");
/*TableItem row = new TableItem(table, SWT.NONE);
row.setText(0, Integer.toString(table.indexOf(row)+1));
row.setText(1, getCurrentTime());
row.setText(2, newData);*/
}
});
btnSend.setBounds(61, 384, 75, 25);
btnSend.setText("Send");
final Group grpSelectMode = new Group(shell, SWT.NONE);
grpSelectMode.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.NORMAL));
grpSelectMode.setText("Select Mode");
grpSelectMode.setBounds(10, 83, 276, 104);
final Label lblSampleTime = new Label(grpSelectMode, SWT.NONE);
lblSampleTime.setBounds(10, 70, 75, 16);
lblSampleTime.setText("Sample Time");
final Spinner spinSampleTime = new Spinner(grpSelectMode, SWT.BORDER | SWT.READ_ONLY);
spinSampleTime.setMaximum(60);
spinSampleTime.setMinimum(5);
spinSampleTime.setSelection(10);
spinSampleTime.setBounds(103, 67, 47, 22);
Button btnAuto = new Button(grpSelectMode, SWT.RADIO);
btnAuto.setSelection(true);
btnAuto.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
grpSelectMode.setBounds(10, 83, 276, 104);
lblSampleTime.setVisible(true);
spinSampleTime.setVisible(true);
}
});
btnAuto.setBounds(10, 30, 159, 16);
btnAuto.setText("Auto");
Button btnManual = new Button(grpSelectMode, SWT.RADIO);
btnManual.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
lblSampleTime.setVisible(false);
spinSampleTime.setVisible(false);
grpSelectMode.setBounds(10, 83, 276, 70);
}
});
btnManual.setBounds(176, 30, 90, 16);
btnManual.setText("Manual");
final Timer timer = new Timer();
final TimerTask tmrTask = new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
myCommNew.sendData("Query" + " " + (++ijk) + "\n");
System.out.println("Timer code Executed" + " " + (ijk));
System.out.println(getCurrentTime());
getTableData();
}
};
final Button btnStart = new Button(shell, SWT.PUSH);
final Button btnStop = new Button(shell, SWT.NONE);
btnStart.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
if(myCommNew.isSerialPortOpen()) {
timerValue = spinSampleTime.getSelection();
timer.scheduleAtFixedRate(tmrTask, 1000, (timerValue*1000));
btnStop.setEnabled(true);
btnStart.setEnabled(false);
} else
System.out.println("Serial Port is Not Open, Kindly Check");
}
});
btnStart.setBounds(450, 103, 75, 25);
btnStart.setText("Start");
btnStop.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
timer.cancel();
System.out.println("\nTimer Cancelled");
btnStart.setEnabled(true);
btnStop.setEnabled(false);
}
});
btnStop.setBounds(450, 162, 75, 25);
btnStop.setText("Stop");
btnStop.setEnabled(false);
}
private String getCurrentTime() {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH.mm.ss");
Date date1 = new Date();
String date = dateFormat.format(date1);
return date;
}
public static boolean recvDiagnoseDataExist() {
if(hasDiagnoseData)
return true;
else
return false;
}
private void getTableData() {
display.asyncExec(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
if(myCommNew.serialDataExist()) {
try {
while(tblIndex<myCommNew.getSerialDataRecvd().size()) {
final TableItem newRow = new TableItem(table, SWT.NONE);
System.out.println("tblIndex Value:" + tblIndex);
System.out.println("Recvd Data Value:" + myCommNew.getSerialDataRecvd());
newRow.setText(0, Integer.toString(tblIndex));
newRow.setText(1, getCurrentTime());
newRow.setText(2,myCommNew.getSerialDataRecvd().get(tblIndex));
System.out.println("The Value Recvd is :" + myCommNew.getSerialDataRecvd().get(tblIndex));
tblIndex++;
}
} catch (Exception excWhile) {
System.out.println("Error in creating row");
System.err.println(excWhile.toString());
excWhile.printStackTrace();
}
try {
myCommNew.resetSerialDataExist();
} catch (Exception exc1) {
System.out.println("Error setting flag");
exc1.printStackTrace();
}
}
}
});
}
private boolean exitApplication() {
MessageBox msgBox = new MessageBox(shell, SWT.ICON_QUESTION
| SWT.YES | SWT.NO);
msgBox.setMessage("Do you really want to exit?");
msgBox.setText("Closing Application");
//msgBox.
int response = msgBox.open();
if (response == SWT.YES) {
return true;
} else return false;
}
}
答案 0 :(得分:0)
我会使用像
这样的东西ILock lock....
Job dataFetcher = new Job("fetcher")
{
// override run
void run(...){
lock.lock();
try{
//get data from micro controller
}finally{
Lock.unlock();
}
};
UIJob viewerUpdater= new UIJob (display, ""){
//override runInUIThread to update your viewer
};
// add change listener and override the done method
dataFetcher.addJobChangeListener(
new JobChangeListener()
{
void done(){
viewerUpdater.schedule();
}
}
);