Created
February 17, 2012 14:11
-
-
Save xseignard/1853637 to your computer and use it in GitHub Desktop.
Demo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fr.free.mdwhatever.arduino.maven; | |
import gnu.io.CommPortIdentifier; | |
import gnu.io.NoSuchPortException; | |
import gnu.io.SerialPort; | |
import gnu.io.SerialPortEvent; | |
import gnu.io.SerialPortEventListener; | |
import java.io.IOException; | |
import java.net.URL; | |
/** | |
* | |
*/ | |
public class Demo { | |
/** | |
* The port on which your arduino is plugged | |
*/ | |
private static final String PORT_NAME = "/dev/tty.usbmodemfd131"; | |
/** | |
* 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; | |
/** | |
* The serial port used for communicating with arduino | |
*/ | |
private SerialPort serialPort; | |
/** | |
* Loads the jnilib | |
*/ | |
public static void loadJniLib() { | |
// loads the jnilib from the source folder "src/main/resources" | |
URL url = Demo.class.getResource("/librxtxSerial.jnilib"); | |
try { | |
System.load(url.getPath()); | |
} | |
catch (UnsatisfiedLinkError unsatisfiedLinkError) { | |
// native code library failed to load. | |
unsatisfiedLinkError.printStackTrace(); | |
} | |
} | |
/** | |
* Will initialize the serial connection with your arduino. | |
*/ | |
public void initialize() { | |
// initializes the port identifier | |
CommPortIdentifier portId = null; | |
try { | |
portId = CommPortIdentifier.getPortIdentifier(PORT_NAME); | |
} | |
catch (NoSuchPortException e1) { | |
// if this exception occurs, it means the PORT_NAME constant is not OK | |
System.out.println("Could not find COM port. Check the PORT_NAME constant"); | |
} | |
try { | |
// opens serial port, and uses class name for the appName. | |
this.serialPort = (SerialPort) portId.open(this.getClass().getName(), TIME_OUT); | |
// sets port parameters | |
this.serialPort.setSerialPortParams(DATA_RATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, | |
SerialPort.PARITY_NONE); | |
// adds event listeners | |
this.serialPort.addEventListener(new MyListener(this.serialPort)); | |
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 (this.serialPort != null) { | |
this.serialPort.removeEventListener(); | |
this.serialPort.close(); | |
System.out.println("Closing the serial connection and the program"); | |
} | |
} | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) throws Exception { | |
// loads the jnilib | |
Demo.loadJniLib(); | |
Demo demo = new Demo(); | |
// initializes the connection with arduino | |
demo.initialize(); | |
System.out.println("Started"); | |
// type something in the console to close the program | |
if (System.in.read() != -1) { | |
demo.close(); | |
} | |
} | |
class MyListener implements SerialPortEventListener { | |
/** | |
* The port to be listened | |
*/ | |
private final SerialPort port; | |
/** | |
* Constructor of the listener, it will listen the given {@link SerialPort}. | |
*/ | |
public MyListener(SerialPort port) { | |
super(); | |
this.port = port; | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see gnu.io.SerialPortEventListener#serialEvent(gnu.io.SerialPortEvent) | |
*/ | |
public void serialEvent(SerialPortEvent event) { | |
// if the event type is DATA_AVAILABLE it means the arduino sent data through the serial port | |
if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) { | |
try { | |
int available = this.port.getInputStream().available(); | |
byte chunk[] = new byte[available]; | |
this.port.getInputStream().read(chunk, 0, available); | |
// Redirects the data to the standard output | |
System.out.print(new String(chunk)); | |
} | |
catch (IOException e) { | |
// something went wrong while reading the data sent by the arduino | |
System.err.println(e.toString()); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment