Results 1 to 5 of 5
Thread: Gui question
- 01-11-2013, 02:09 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 7
- Rep Power
- 0
Gui question
Allo
Starting a new project on sending/receiving data with the arduino and eclipse via serial port. This is the tutorial presented by Arduino Arduino Playground - Java.
Everything works fine now...(since I don't have much knowledge with eclipse/java, it tooks me about 2 weeks to figure it out).
Project goal: Creating a runnable jar file(application) which print out the data received from the arduino.
Project status right now: Monitor the data received from the Arduino and print out the message in the eclipse console
Next step I want to achieve: Print this message on a simple design application.(one button , one text area)
Note: WindowBuilder installed.
Thank you if you give me hints
Here is the method that generates the print out:
public synchronized void serialEvent(SerialPortEvent oEvent) {
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
int available = input.available();
byte chunk[] = new byte[available];
input.read(chunk, 0, available);
// Displayed results are codepage dependent
System.out.print(new String(chunk));
Here is the full project code from Arduino website:
import java.io.InputStream;
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/tty.usbserial-A9007UX1", // Mac OS X
"/dev/ttyUSB0", // Linux
"COM3", // Windows
};
/** Buffered input stream from the port */
private InputStream 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();
// iterate through, looking for the port
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 = 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 {
int available = input.available();
byte chunk[] = new byte[available];
input.read(chunk, 0, available);
// Displayed results are codepage dependent
System.out.print(new String(chunk));
} 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();
System.out.println("Started");
}
}Last edited by Playagood; 01-11-2013 at 03:03 AM.
- 01-11-2013, 03:31 AM #2
Re: Gui question
Why do they call it rush hour when nothing moves? - Robin Williams
- 01-11-2013, 03:32 AM #3
Member
- Join Date
- Feb 2012
- Posts
- 7
- Rep Power
- 0
Re: Gui question
thank you
- 01-11-2013, 05:45 AM #4
Member
- Join Date
- Dec 2012
- Posts
- 74
- Rep Power
- 0
Re: Gui question
1. To create a .jar from Eclipse, choose "Export" from the "File" menu and then choose "Java" and then choose "Runnable Jar". You'll probably have to include a .jar file with your libraries in it for your serial communications stuff.
2. To have a simple GUI and have your output go there instead of going to the console, you could implement a JTextArea like this:
ConsolePrintWriter.java
ConsoleFrame.javaJava Code:import java.io.IOException; import java.io.PrintWriter; import java.io.Writer; import javax.swing.JTextArea; class ConsoleWriter extends Writer { private JTextArea jTextArea; public ConsoleWriter(JTextArea jTextArea) { this.jTextArea = jTextArea; } public JTextArea getTextArea() { return jTextArea; } public void setTextArea(JTextArea jTextArea) { this.jTextArea = jTextArea; } public String charBufToString(char[] cbuf, int off, int len) { StringBuffer sb = new StringBuffer(); int max = Math.min(off+len, cbuf.length); for (int i = 0; i < max; i++) { char ch = cbuf[i]; sb.append(ch); } return sb.toString(); } @Override public void write(char[] cbuf, int off, int len) throws IOException { String string = charBufToString(cbuf, off, len); jTextArea.append(string); } @Override public void close() throws IOException { // TODO we should probably have a flag of whether the writer is open or not } @Override public void flush() throws IOException { } } public class ConsolePrintWriter extends PrintWriter { public ConsolePrintWriter(JTextArea jTextArea) { super(new ConsoleWriter(jTextArea)); } public JTextArea getTextArea() { ConsoleWriter consoleWriter = (ConsoleWriter)out; return consoleWriter.getTextArea(); } }
Java Code:import java.lang.reflect.InvocationTargetException; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.SwingUtilities; public class ConsoleFrame { private JFrame jFrame; private ConsolePrintWriter consolePrintWriter; public ConsoleFrame() throws InterruptedException, InvocationTargetException { SwingUtilities.invokeAndWait(new Runnable() { public void run() { jFrame = new JFrame("Console"); JTextArea jTextArea = new JTextArea(); jTextArea.setEditable(false); JScrollPane jScrollPane = new JScrollPane(jTextArea); consolePrintWriter = new ConsolePrintWriter(jTextArea); jFrame.add(jScrollPane); jFrame.setSize(500,500); jFrame.setVisible(true); } }); } public static void main(String[] args) throws InterruptedException, InvocationTargetException { // create a window to test the ConsolePrintWriter ConsoleFrame consoleFrame = new ConsoleFrame(); // get a reference to the ConsolePrintWriter ConsolePrintWriter out = consoleFrame.consolePrintWriter; // test printing out the the ConsolePrintWriter because it's a type of PrintWriter out.println("Hello World"); out.println("More Stuff"); out.println(1.0); // test printing a double for (int i = 1; i <= 100; i++) { out.println(i); } } }
- 01-13-2013, 05:18 AM #5
Member
- Join Date
- Feb 2012
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Forum question on why discussion threads are locked though question is unanswered
By pseeburger in forum Suggestions & FeedbackReplies: 2Last Post: 05-25-2012, 04:00 PM -
Java Question [Beginner Question]
By joker760 in forum New To JavaReplies: 3Last Post: 12-13-2011, 04:01 PM -
question posted by indissa: library question.
By Fubarable in forum New To JavaReplies: 2Last Post: 11-18-2011, 01:14 AM -
Question concerning question marks and colons
By jim01 in forum New To JavaReplies: 17Last Post: 01-14-2011, 12:05 AM -
Question mark colon operator question
By orchid in forum Advanced JavaReplies: 9Last Post: 12-19-2010, 08:49 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks