Re: Serial Port Communications with GUI



HulkingNightCrawler wrote:
I am trying to read data off a serial port and then display the data
in a GUI and I cannot seem to access the GUI in order to do this. For
some reason whenever I try to append to the text box the program seems
to stall out and I cannot figure out why? Any help would be
appreciated, thanks!!


import java.io.*;
import java.util.*;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import gnu.io.*;


public class Data implements Runnable, SerialPortEventListener{
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
static Shell shell;
static Display dispMain;
static Text txtOutput;

/**
* Method declaration
*
*
* @param args
*
* @see
*/
public static void main(String[] args) {
boolean portFound = false;
String defaultPort;

if (args.length > 0) {
defaultPort = args[0];
}

buildGUI();

portList = CommPortIdentifier.getPortIdentifiers();

while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(defaultPort)) {
System.out.println("Found port: "+defaultPort);
portFound = true;
Data reader = new Data();
}
}
}
if (!portFound) {
System.out.println("port " + defaultPort + " not found.");
}

while (!shell.isDisposed())
{
if (!dispMain.readAndDispatch()) dispMain.sleep();
}
}

/**
* Constructor declaration
*
*
* @see
*/
public Data() {
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
} catch (PortInUseException e) {}

try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {}

try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {}

serialPort.notifyOnDataAvailable(true);

try {
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}



readThread = new Thread(this);

readThread.start();


}

/**
* Method declaration
*
*
* @see
*/
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {}
}

public static void buildGUI()
{
dispMain = new Display();
shell = new Shell(dispMain);

shell.setText("Parking Meter Test Bench Data");
shell.setSize(800,600);
shell.open();

txtOutput = new Text(shell, SWT.BORDER | SWT.H_SCROLL |
SWT.V_SCROLL);
txtOutput.setBounds(10,90,200,200);
txtOutput.setEditable(false);
txtOutput.setText("Test Text\n");
txtOutput.append("Hello World");
txtOutput.append("Hello Look at Me");
txtOutput.setLocation(10, 150);



}

/**
* Method declaration
*
*
* @param event
*
* @see
*/
public void serialEvent(SerialPortEvent event) {
switch (event.getEventType()) {

case SerialPortEvent.BI:

case SerialPortEvent.OE:

case SerialPortEvent.FE:

case SerialPortEvent.PE:

case SerialPortEvent.CD:

case SerialPortEvent.CTS:

case SerialPortEvent.DSR:

case SerialPortEvent.RI:

case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;

case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[20];

try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
System.out.println("1233433423423");
}

System.out.print(new String(readBuffer));
txtOutput.append(new String(readBuffer));

} catch (IOException e) {}

break;
}
}

}

I see you haven't gotten any replies and that could be because of the Serial Port in the subject or it could be because you are using SWT. I don't know a thing about SWT and if that is where your problem lies I cannot help you. If you want to turn this into a Swing or AWT program you might get more help.

If this isn't an exercise in how to do event driven serial I/O then I would change the architecture of your program to use a thread to read from the serial port. It will make your program simpler and you will be able to get a much higher baud rate out of it as well.

--

Knute Johnson
email s/nospam/knute2008/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
.



Relevant Pages

  • Serial Port Communications with GUI
    ... I am trying to read data off a serial port and then display the data ... in a GUI and I cannot seem to access the GUI in order to do this. ... Method declaration ... public static void main{ ...
    (comp.lang.java.gui)
  • Re: Com port Communication
    ... static CommPortIdentifier portId; ... public static void main{ ... static OutputStream outputStream; ...
    (comp.lang.java.programmer)