Pages: [1]   Go Down
Author Topic: Help with Java GUI.  (Read 573 times)
0 Members and 1 Guest are viewing this topic.
USA
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Uno Otto Otto Cinque
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I finally got RXTX working.  Now I'm looking for a way to get output from and Arduino Uno streaming text into a TextArea rather than console.
I've looked here: http://docs.oracle.com/javase/tutorial/uiswing/index.html   but I can not find an example that could output:
input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));  into a GUI text area.

What wold be nice if I could use a button to generate a text stream from the RXTX code from the Arduino.
Any hint would be appreciated:

Code:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.util.Enumeration;


public class SerialTest implements SerialPortEventListener {
SerialPort serialPort;
        /** The port we're normally going to use. */
private static final String PORT_NAMES[] = { "/dev/ttyUSB0"};
/**
* A BufferedReader which will be fed by a InputStreamReader
* converting the bytes into characters
* making the displayed results codepage independent
*/
private BufferedReader input;
/** The output stream to the port */
private OutputStream output;
/** Milliseconds to block while waiting for port open */
private static final int TIME_OUT = 2000;
/** Default bits per second for COM port. */
private static final int DATA_RATE = 9600;

public void initialize() {
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();

//First, Find an instance of serial port as set in PORT_NAMES.
while (portEnum.hasMoreElements()) {
CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
for (String portName : PORT_NAMES) {
if (currPortId.getName().equals(portName)) {
portId = currPortId;
break;
}
}
}
if (portId == null) {
System.out.println("Could not find COM port.");
return;
}

try {
// open serial port, and use class name for the appName.
serialPort = (SerialPort) portId.open(this.getClass().getName(),
TIME_OUT);

// set port parameters
serialPort.setSerialPortParams(DATA_RATE,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);

// open the streams
input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
output = serialPort.getOutputStream();

// add event listeners
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
} catch (Exception e) {
System.err.println(e.toString());
}
}

/**
* This should be called when you stop using the port.
* This will prevent port locking on platforms like Linux.
*/
public synchronized void close() {
if (serialPort != null) {
serialPort.removeEventListener();
serialPort.close();
}
}

/**
* Handle an event on the serial port. Read the data and print it.
*/
public synchronized void serialEvent(SerialPortEvent oEvent) {
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
String inputLine=input.readLine();
System.out.println(inputLine);
} catch (Exception e) {
System.err.println(e.toString());
}
}
// Ignore all the other eventTypes, but you should consider the other ones.
}

public static void main(String[] args) throws Exception {
SerialTest main = new SerialTest();
main.initialize();
Thread t=new Thread() {
public void run() {
//the following line will keep this app alive for 1000 seconds,
//waiting for events to occur and responding to them (printing incoming messages to console).
try {Thread.sleep(1000000);} catch (InterruptedException ie) {}
}
};
t.start();
System.out.println("Started");
}
}
Logged

cwc 1885

Netherlands
Offline Offline
Full Member
***
Karma: 3
Posts: 109
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Read Strings from your 'input' Reader and append( ... ) the String to your JTextArea. Take care that the document doesn't become too large.

kind regards,

Jos
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 86
Posts: 3466
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you haven't looked at it already, there is an example program called Bluetooth Chat. You don't need the bluetooth function, just the layout. This program uses bluetooth serial to send and receive messages between two devices, including an Arduino with a serial.read/writen message.

Give it a try.
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

0
Offline Offline
Sr. Member
****
Karma: 8
Posts: 291
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Processing which is built on top of processing, has a read serial function and has built in GUI for text and graphics. You can port that into your java app.
« Last Edit: March 15, 2013, 02:58:57 pm by mistergreen » Logged

Pages: [1]   Go Up
Jump to: