如何使用Google protobuf通过串口进行通信?

rjz*_*zii 6 java rxtx protocol-buffers

我正在开发一个使用RXTXprotobuf与开发板上的应用程序进行通信的项目,而且我遇到了一些问题,这些问题意味着我可能会以错误的方式做事.这是我目前用于向板上写请求的内容(读取代码类似):

public void write(CableCommandRequest request, OutputStream out) {
  CodedOutputStream outStream = CodedOutputStream.newInstance(out);
  request.writeTo(outStreatm);
  outStream.flush();
}
Run Code Online (Sandbox Code Playgroud)

以下是用于准备RXTX串行连接的设置,该连接又支持命令OutputStream使用的设置write:

// The baud rate to use when connecting to the development board
private final static int BAUD_RATE = 115200;
// The timeout to use for the serial port
private final static int CONNECTION_TIMEOUT = 50;
// The serial break for the development board, 100
private final static int SERIAL_BREAK = 100;

// <SNIP> ...

SerialPort serialPort = (SerialPort)port.open(appName, CONNECTION_TIMEOUT);
serialPort.setSerialPortParams(BAUD_RATE, 
                               SerialPort.DATABITS_8,
                               SerialPort.STOPBITS_1,
                               SerialPort.PARITY_NONE);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
serialPort.sendBreak(SERIAL_BREAK);
Run Code Online (Sandbox Code Playgroud)

使用OutputStream它是由RXTX准备的,开发板似乎表明正在接收数据,但它正在变得乱码或者没有被理解.

到目前为止,所有常见的嫌疑(例如,串行连接未建立,通信问题等)已被消除,因此看起来问题在于如何进行呼叫,writeTo因为通过串行连接的通信是成功的.

关于在串行连接上使用protobuf的文档似乎很少,所以我假设传递它OutputStream应该足够了.这实际上是否正确,或者这是通过串行连接发送响应的错误方式?

tra*_*god 2

Protocol Buffer值在线路上使用小端字节顺序进行编码。当两端都使用协议缓冲区时,这通常是无关紧要的,但在这种情况下可能会造成问题。如果是这样,您可以按照此处的java.nio.ByteBuffer建议使用来实现转换。