Inital test (from beerduino, https://github.com/ilyevsky/beerduino) to establish protocol buffer communication between java and arduino powered by purejavacomm (https://github.com/nyholku/purejavacomm).

This commit is contained in:
2015-03-16 20:12:56 +00:00
commit 6cb5db6c10
10 changed files with 439 additions and 0 deletions

View File

@@ -0,0 +1,92 @@
package test;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.TooManyListenersException;
import purejavacomm.CommPortIdentifier;
import purejavacomm.PortInUseException;
import purejavacomm.SerialPort;
import purejavacomm.SerialPortEvent;
import purejavacomm.SerialPortEventListener;
import purejavacomm.UnsupportedCommOperationException;
import beerduino.Beerduino.Echo;
public class Arduino implements SerialPortEventListener {
public static final int TIME_OUT = 1000;
public static final String PORT_NAMES[] = {
"tty.usbmodem", // Mac OS X
"usbdev", // Linux
"tty", // Linux
"serial", // Linux
"COM3", // Windows
};
protected SerialPort port = null;
protected InputStream inputStream = null;
protected ArrayList<EchoListener> listenerList = new ArrayList<EchoListener>();
public void connect() throws UnsupportedCommOperationException, PortInUseException, IOException, TooManyListenersException {
CommPortIdentifier portid = null;
Enumeration<?> portEnum = CommPortIdentifier.getPortIdentifiers();
while (portEnum.hasMoreElements()) {
portid = (CommPortIdentifier)portEnum.nextElement();
if (portid != null) {
System.out.println("Trying: " + portid.getName());
for ( String portName: PORT_NAMES) {
if ( portid.getName().equals(portName) || portid.getName().contains(portName)) {
port = (SerialPort) portid.open("", TIME_OUT);
port.setFlowControlMode(
SerialPort.FLOWCONTROL_XONXOFF_IN +
SerialPort.FLOWCONTROL_XONXOFF_OUT);
inputStream = port.getInputStream();
System.out.println( "Connected on port: " + portid.getName());
port.addEventListener(this);
port.notifyOnDataAvailable(true);
}
}
}
}
}
public void serialEvent(SerialPortEvent event) {
try {
switch (event.getEventType()) {
case SerialPortEvent.DATA_AVAILABLE:
Echo echo = Echo.parseDelimitedFrom(inputStream);
for (EchoListener listener : listenerList) {
listener.receive(echo);
}
break;
default:
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
public InputStream getInputStream() throws IOException {
return port.getInputStream();
}
public OutputStream getOutputStream() throws IOException {
return port.getOutputStream();
}
public void addListener(EchoListener listener) {
listenerList.add(listener);
}
public void removeListener(EchoListener listener) {
listenerList.remove(listener);
}
public void close() {
port.close();
}
}

View File

@@ -0,0 +1,7 @@
package test;
import beerduino.Beerduino.Echo;
public interface EchoListener {
public void receive(Echo echo);
}

View File

@@ -0,0 +1,35 @@
package test;
import java.io.OutputStream;
import beerduino.Beerduino.Echo;
import beerduino.Beerduino.Ping;
public class Test implements EchoListener {
public static void main(String[] argv) {
try {
new Test().start();
} catch (Exception e) {
e.printStackTrace();
}
}
private void start() throws Exception {
Arduino arduino = new Arduino();
arduino.connect();
arduino.addListener(this);
OutputStream outputStream = arduino.getOutputStream();
int i = 123;
while ( i < 10000) {
Ping ping = Ping.newBuilder().setId(i++).build();
System.out.println("writing!");
ping.writeDelimitedTo(outputStream);
Thread.sleep(1000);
}
arduino.close();
}
public void receive(Echo echo) {
System.out.println("> " + echo.getMessage() + " " + echo.getId());
}
}