- Joined
- Jul 29, 2009
- Messages
- 1
- Reaction score
- 0
Hi!
I'm currently writing a SWT application, which uses RXTX to communicate with an Atmel microcontroller to download its external memory. The GUI is opening a new Thread for Serial Communication. During serial communication a status bar is shown and continuously updated. Everything works fine, exept if i change the window focus. When I then got back to the SWT application, it is frozen, until the serial communication is finished.
I'm using Kuligowski's tutorial code (worldwideweb dot kuligowski dot pl) for serial communication. I expanded his RS232Example Class (and renamed to SerialCommunication) by the following code, which additionally opens a status bar shell during serial communication:
DataTransfer() is called by pressing a botton inside the GUI. Inside DataTransfer another Thread is started, which does the serial communication. Inside a while loop i check, if this thread is still alive. If yes, i update the progress bar, if not, i close the progress bar shell.
Everything works fine, but when i change the window focus (e.g. going to the internet browser and back) the status bar is not updated anymore and the GUI is frozen, until the serial communication is done.
Does anyone know, what i do wrong? I also tried to update the statusbar inside the serial communication thread via display.async(), but also without success.
I'm currently writing a SWT application, which uses RXTX to communicate with an Atmel microcontroller to download its external memory. The GUI is opening a new Thread for Serial Communication. During serial communication a status bar is shown and continuously updated. Everything works fine, exept if i change the window focus. When I then got back to the SWT application, it is frozen, until the serial communication is finished.
I'm using Kuligowski's tutorial code (worldwideweb dot kuligowski dot pl) for serial communication. I expanded his RS232Example Class (and renamed to SerialCommunication) by the following code, which additionally opens a status bar shell during serial communication:
import java.io.IOException;
import java.io.InputStream;
import java.iutputStream;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;
public class SerialCommunication {
protected CommPortIdentifier portIdentifier;
protected SerialPort serialPort;
public OutputStream outStream;
protected InputStream inStream;
private static CommPortReceiver receiver;
private static MessageInterpreter interpreter;
private static String comport;
private static Shell parent_shell;
private Label label;
private ProgressBar progress;
public enum Processing {
PROCESS_BAR, TIMEOUT
}
SerialCommunication(String c, Shell s)
{
interpreter = new MessageInterpreter();
comport = c;
parent_shell = s;
}
public boolean connect(String portName, Processing p) throws Exception {
portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
// points who owns the port and connection timeout
try{
serialPort = (SerialPort) portIdentifier.open("Testapplication", 2000);
// setup connection parameters
serialPort.setSerialPortParams(230400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
// setup serial port writer
CommPortSender.setWriterStream(serialPort.getOutputStream());
// setup serial port reader
receiver = new CommPortReceiver(serialPort.getInputStream(), interpreter);
receiver.start();
if(p.name().equals("TIMEOUT"))
{
TimeOutThread timeout = new TimeOutThread(receiver);
timeout.start();
System.err.println("Timeout Thread started...");
}
return true;
}catch(PortInUseException e){
MessageBox msg = new MessageBox(parent_shell, SWT.OK | SWT.ICON_ERROR);
msg.setText("Error");
msg.setMessage("Port already in use... \n");
msg.open();
return false;
}
}
public void disconnect() {
if (serialPort != null) {
try {
// close the i/o streams.
serialPort.getInputStream().close();
serialPort.getOutputStream().close();
} catch (IOException ex) {
// don't care
}
serialPort.close();
serialPort = null;
}
}
public void DataTransfer(String s) throws Exception
{
Shell shell = new Shell(parent_shell, SWT.TITLE | SWT.PRIMARY_MODAL);
shell.setText("Processing");
shell.setSize(400, 90);
Rectangle splashRect = shell.getBounds();
Rectangle displayRect = parent_shell.getDisplay().getBounds();
final int x = (displayRect.width - splashRect.width) / 2;
final int y = (displayRect.height - splashRect.height) / 2;
shell.setLocation(x, y);
label = new Label(shell, SWT.LEFT);
label.setText("Checking connectivity...");
label.setBounds(12,10,370,20);
progress = new ProgressBar(shell, SWT.HORIZONTAL | SWT.SMOOTH);
progress.setBounds(10,30,370,20);
shell.open();
try
{
boolean connected = this.connect(comport, Processing.TIMEOUT);
if(connected == true)
{
interpreter.setConnection(false);
CommPortSender.send(new ProtocolImpl(interpreter).getMessage("A"));
while(receiver.isAlive())
{}
this.disconnect();
System.err.println("Starting Datatransfer = " + interpreter.getConnection());
if(interpreter.getConnection() == true)
{
label.setText("Transfering data...");
progress.setMinimum(0);
progress.setMaximum(interpreter.getMax_address());
this.connect(comport, Processing.PROCESS_BAR);
CommPortSender.send(new ProtocolImpl(interpreter).getMessage("E"));
while(receiver.isAlive())
{
progress.setSelection(interpreter.getAddress());
}
this.disconnect();
interpreter.moveDumpFile(s);
if(interpreter.getError() == false)
{
MessageBox msg = new MessageBox(parent_shell, SWT.OK | SWT.ICON_INFORMATION);
msg.setText("Information");
msg.setMessage("Data transfered successfully ... \n");
msg.open();
}
else
{
MessageBox msg = new MessageBox(parent_shell, SWT.OK | SWT.ICON_ERROR);
msg.setText("Error");
msg.setMessage("Error during data transfer, please try again ... \n");
msg.open();
}
}
else
{
MessageBox msg = new MessageBox(parent_shell, SWT.OK | SWT.ICON_ERROR);
msg.setText("Error");
msg.setMessage("Timeout... Check Serial Interface \n");
msg.open();
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
shell.close();
}
}
DataTransfer() is called by pressing a botton inside the GUI. Inside DataTransfer another Thread is started, which does the serial communication. Inside a while loop i check, if this thread is still alive. If yes, i update the progress bar, if not, i close the progress bar shell.
Everything works fine, but when i change the window focus (e.g. going to the internet browser and back) the status bar is not updated anymore and the GUI is frozen, until the serial communication is done.
Does anyone know, what i do wrong? I also tried to update the statusbar inside the serial communication thread via display.async(), but also without success.