Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use Google protobuf to communicate over a serial port?

I'm working on a project that uses RXTX and protobuf to communicate with an application on a development board and I've been running into issues which implies that I'm likely doing things the wrong way. Here's what I currently have for writing the request to the board (the read code is similar):

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

The following is the setting that are used to prepare the RXTX serial connection which in turn suplies the OutputStream used by the write command:

// 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);

The OutputStream that is used is prepared by RXTX and the development board seems to indicate that data is being received, but it is getting garbled or is otherwise not being understood.

So far all of the usual suspects (e.g. serial connection not being established, communication issues, etc.) have been eliminated so it appears that the problem is in how the call to writeTo is made since communications over the serial connection are successful.

There seems to be little documentation on using protobuf over a serial connection so I'm assuming that passing the OutputStream should be sufficient. Is this in fact correct, or is this the wrong way of sending the response over the serial connection?

like image 608
rjzii Avatar asked Jun 28 '13 02:06

rjzii


People also ask

How is protobuf serialized?

The Protobuf serialization mechanism is given through the protoc application, this compiler will parse the . proto file and will generate as output, source files according to the configured language by its arguments, in this case, C++. You can also obtain more information about, reading the section compiler invocation.

How do I use Google protobuf?

To use the Any type, you must import the google/protobuf/any. proto definition. In the C# code, the Any class provides methods for setting the field, extracting the message, and checking the type. Protobuf's internal reflection code uses the Descriptor static field on each generated type to resolve Any field types.

How do I send protobuf over HTTP?

You can certainly send even a binary payload with an HTTP request, or in an HTTP response. Just write the bytes of the protocol buffer directly into the request/response, and make sure to set the content type to "application/octet-stream". The client, and server, should be able to take care of the rest easily.

What is Google Protobuf and why we use it?

Google Protobuf has that as one of the focus areas. Efficient serialization and deserialization − In microservice environment, given that multiple communications take place over a network, it is critical how fast can we serialize and deserialize. Google Protobuf ensures that it is as quick as possible in serializing and deserializing the data.

How to read the serialized Boolean in Protobuf?

So, as we see, we are able to read the serialized Boolean by deserializing the binary data to Theater object. enum is one of the composite datatypes of Protobuf. It translates to an enum in the languages that we use, for example, Java.

Does Protobuf work with go and dart?

We have been using Protobuf in Java and Python. But there are multiple languages it supports including C++, C#, Kotlin, Dart, Go, etc. The basic stuff mostly remains the same, i.e., writing a proto schema, generating the source code via protoc binary which our code can use. Let us write a basic example for Go and Dart as part of this section.

How to instruct Protobuf in Java?

Following is the syntax that we need to have to instruct Protobuf − syntax = "proto3"; package theater; option java_package = "com.tutorialspoint.theater"; message Theater { string name = 1; } To use Protobuf, we will now have to use the protoc binary to create the required classes from this ".proto" file. Let us see how to do that −


Video Answer


1 Answers

Protocol Buffer values are encoded using little-endian byte order on the wire. This is ordinarily irrelevant when Protocol Buffers are use on both ends, but it may pose a problem in this context. If so, you may be able to use java.nio.ByteBuffer to effect the conversion, as suggested here.

like image 64
trashgod Avatar answered Oct 25 '22 07:10

trashgod