Working code for bidirectional communication using protocol buffers

This commit is contained in:
2015-07-16 15:55:55 +01:00
parent df4bab1750
commit 394a413691
5 changed files with 6 additions and 4658 deletions

View File

@@ -37,6 +37,7 @@ public class Local extends AbstractArduino {
public void send(byte[] buffer) throws ArduinoException {
try {
outputStream.write(buffer);
outputStream.flush();
} catch (IOException e) {
throw new ArduinoException("Failed to write to arduino");
}

View File

@@ -22,15 +22,11 @@ public class ParsingPort extends Port {
public void serialEvent(SerialPortEvent event) {
try {
Method m = messageClass.getMethod("parseDelimitedFrom", InputStream.class);
Object object = m.invoke(null, inputStream);
Method method = messageClass.getMethod("parseDelimitedFrom", InputStream.class);
Object object = method.invoke(null, inputStream);
for (Listen<Object> listen : listenList) {
listen.add(object);
}
} catch (Exception e) {
logger.error("", e);
} catch (Throwable e) {
logger.error("", e);
}
} catch (Exception e) {} catch (Throwable e) {} finally {}
}
}

View File

@@ -68,13 +68,14 @@ public class Port implements SerialPortEventListener {
CommPortIdentifier portid = null;
Enumeration<?> portEnum = CommPortIdentifier.getPortIdentifiers();
while (portEnum.hasMoreElements()) {
portid = (CommPortIdentifier)portEnum.nextElement();
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)) {
try {
serialPort = (SerialPort) portid.open("", TIME_OUT);
serialPort.setSerialPortParams(19200, 8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.setFlowControlMode(
SerialPort.FLOWCONTROL_XONXOFF_IN +
SerialPort.FLOWCONTROL_XONXOFF_OUT);

File diff suppressed because it is too large Load Diff

View File

@@ -1,24 +0,0 @@
package jlibarduino;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.lang.reflect.Method;
import tm1638.Tm1638.Echo;
public class Test {
public static void main(String[] args) throws Exception {
Echo echo = Echo.newBuilder().setMessage("abc").setId(123).build();
ByteArrayOutputStream output = new ByteArrayOutputStream();
echo.writeDelimitedTo(output);
byte[] buffer = output.toByteArray();
ByteArrayInputStream input = new ByteArrayInputStream(buffer);
Class<?> messageClass = Echo.class;
Method m = messageClass.getMethod("parseDelimitedFrom", InputStream.class);
Object object = m.invoke(null, input);
echo = (Echo) object;
System.out.println(echo.getMessage());
System.out.println(echo.getId());
}
}