Working code for bidirectional communication using protocol buffers
This commit is contained in:
@@ -37,6 +37,7 @@ public class Local extends AbstractArduino {
|
|||||||
public void send(byte[] buffer) throws ArduinoException {
|
public void send(byte[] buffer) throws ArduinoException {
|
||||||
try {
|
try {
|
||||||
outputStream.write(buffer);
|
outputStream.write(buffer);
|
||||||
|
outputStream.flush();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new ArduinoException("Failed to write to arduino");
|
throw new ArduinoException("Failed to write to arduino");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,15 +22,11 @@ public class ParsingPort extends Port {
|
|||||||
|
|
||||||
public void serialEvent(SerialPortEvent event) {
|
public void serialEvent(SerialPortEvent event) {
|
||||||
try {
|
try {
|
||||||
Method m = messageClass.getMethod("parseDelimitedFrom", InputStream.class);
|
Method method = messageClass.getMethod("parseDelimitedFrom", InputStream.class);
|
||||||
Object object = m.invoke(null, inputStream);
|
Object object = method.invoke(null, inputStream);
|
||||||
for (Listen<Object> listen : listenList) {
|
for (Listen<Object> listen : listenList) {
|
||||||
listen.add(object);
|
listen.add(object);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {} catch (Throwable e) {} finally {}
|
||||||
logger.error("", e);
|
|
||||||
} catch (Throwable e) {
|
|
||||||
logger.error("", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,13 +68,14 @@ public class Port implements SerialPortEventListener {
|
|||||||
CommPortIdentifier portid = null;
|
CommPortIdentifier portid = null;
|
||||||
Enumeration<?> portEnum = CommPortIdentifier.getPortIdentifiers();
|
Enumeration<?> portEnum = CommPortIdentifier.getPortIdentifiers();
|
||||||
while (portEnum.hasMoreElements()) {
|
while (portEnum.hasMoreElements()) {
|
||||||
portid = (CommPortIdentifier)portEnum.nextElement();
|
portid = (CommPortIdentifier) portEnum.nextElement();
|
||||||
if (portid != null) {
|
if (portid != null) {
|
||||||
System.out.println("Trying: " + portid.getName());
|
System.out.println("Trying: " + portid.getName());
|
||||||
for ( String portName: PORT_NAMES) {
|
for ( String portName: PORT_NAMES) {
|
||||||
if (portid.getName().equals(portName) || portid.getName().contains(portName)) {
|
if (portid.getName().equals(portName) || portid.getName().contains(portName)) {
|
||||||
try {
|
try {
|
||||||
serialPort = (SerialPort) portid.open("", TIME_OUT);
|
serialPort = (SerialPort) portid.open("", TIME_OUT);
|
||||||
|
serialPort.setSerialPortParams(19200, 8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
|
||||||
serialPort.setFlowControlMode(
|
serialPort.setFlowControlMode(
|
||||||
SerialPort.FLOWCONTROL_XONXOFF_IN +
|
SerialPort.FLOWCONTROL_XONXOFF_IN +
|
||||||
SerialPort.FLOWCONTROL_XONXOFF_OUT);
|
SerialPort.FLOWCONTROL_XONXOFF_OUT);
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user