Results 1 to 4 of 4
- 07-29-2009, 06:36 PM #1
Member
- Join Date
- Jul 2009
- Posts
- 2
- Rep Power
- 0
SWT GUI for Serial Communication - no update if focus is lost
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:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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.getOutpu tStream());
// 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();
}
}
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.
- 09-14-2009, 05:37 PM #2
Member
- Join Date
- Sep 2009
- Posts
- 2
- Rep Power
- 0
Solved?
I am trying to write something similar. Did you ever solve your problem? My issue is that my program does not seem to respond to the Serial Events.
- 09-23-2009, 02:14 PM #3
Member
- Join Date
- Jul 2009
- Posts
- 2
- Rep Power
- 0
- 09-23-2009, 09:28 PM #4
Member
- Join Date
- Sep 2009
- Posts
- 2
- Rep Power
- 0
Hex Codes don't seem to go through.
I also have mine working as long as I am sending printable characters. I need to send 5 hex codes in a row but the board does not seem to respond.
I could not see your method when I tried to open you web page.
Do know know of anything that would prevent out.write() from sending a hex code?
Thanks,
Kevin
Similar Threads
-
I'm lost :(
By leonardjr in forum New To JavaReplies: 8Last Post: 03-03-2009, 05:18 AM -
So Lost
By kandt in forum New To JavaReplies: 5Last Post: 12-13-2008, 10:55 PM -
Has Java lost its way?
By alexj44 in forum Advanced JavaReplies: 4Last Post: 09-23-2008, 02:26 PM -
Absolutely Lost
By Lehane_9 in forum New To JavaReplies: 2Last Post: 12-03-2007, 07:25 PM -
Help Needed - I'm so lost
By adlb1300 in forum New To JavaReplies: 3Last Post: 11-14-2007, 02:54 AM
Bookmarks