Should be simple yeah? Arduino talking to Java... For a pro yes. But I found it hard to get Arduino talking to my Java applications, unlike in processing there is no easy automatic set up you kind of have to figure it out yourself. the main problem is talking to the serial port and then getting what Arduino sends out into some sort of readable format. Below is some code that will get you listening to an Arduino through the serial port.
package dualHookUp;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.TooManyListenersException;
import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import gnu.io.UnsupportedCommOperationException;
public class AttachArduino implements SerialPortEventListener, Runnable {
static Enumeration portList;
static CommPortIdentifier portId;
boolean portFound = false;
String defaultPort = "COM6";
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
String myNum = "";
String nextInput;
int inputA, inputB;
int myInt = 0;
public static void main(String[] args) {
new AttachArduino();
}
public AttachArduino(){
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
System.out.println(portList);
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(defaultPort)) {
System.out.println("Found port: "+defaultPort);
portFound = true;
loadArduino();
}
}
}
if (!portFound) {
System.out.println("port " + defaultPort + " not found.");
}
}
private void loadArduino(){
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 9600);
} 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();
}
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {}
}
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:
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read();
if (numBytes != 10){
char c = (char)numBytes;
myNum += (c);
}else{
char index = (myNum.charAt(0));
if (index == 'A'){
myNum = myNum.replace("A", "");
inputA = Integer.parseInt(myNum);
}
if (index == 'B'){
myNum = myNum.replace("B", "");
inputB = Integer.parseInt(myNum);
}
myNum = "";
}
}
} catch (IOException e) { break;}
}
}
public int getInputA(){
return inputA;
}
public int getInputB(){
return inputB;
}
}