Vote count:
0
i've recently gotten an arduino and started playing with it. I've written a java program to listen for input on the serial port from the arduino (or I've slighthly modified some example code)
This is the code:
public class SerialListener implements SerialPortEventListener {
SerialPort serialPort;
/**
* 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() {
// the next line is for Raspberry Pi and
// gets us into the while loop and was suggested here was suggested http://ift.tt/1qMo5s1
// System.setProperty("gnu.io.rxtx.SerialPorts", "/dev/ttyACM0");
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
while (portEnum.hasMoreElements()) {
CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
if (currPortId.getName().equals("COM3")) {
portId = currPortId;
break;
}
}
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);
FileWriter fWriter = new FileWriter("C:\\Apache24\\htdocs\\temp.txt");
BufferedWriter writer = new BufferedWriter (fWriter);
writer.write(inputLine);
writer.close();
} catch (Exception e) {
System.err.println(e.toString());
}
}
}
public static void main(String[] args) throws Exception {
SerialListener tempListener = new SerialListener();
tempListener.initialize();
}
}
I understand that the SerialListener class implements the method to handle SerialPort Events and that I in the initiate method are setting the object of the SerialListner class to listen for events.
What I don't understand is why this program don't immediately quits since it does nothing in the main method after running the initalize method.
Are event listeners run on different threads or why does this work? (Because this works as I want it to)
The code I've basically copied can be found here: http://ift.tt/1qAxlPK under "Java Sample Code" But as you can see I don't start a class which keeps it alive for 1000 seconds as they say in the example
Why do my program not quit itself when the main method is finished
Aucun commentaire:
Enregistrer un commentaire