Protobuf Tutorials
Preparation
Extract the d2d-spec.zip and change to the Tutorials directory.
cd d2d-spec/Tutorials
sudo python3 -m pip install virtualenv
python3 -m virtualenv rapi
source rapi/bin/activate
pip3 install -r requirements.txt
python3 generate_pb.py
Framing Tutorial
In terminal start python 3 application.
python3
Type in following code to create a protobuf message:
import remoteapi_rs232_pb2 as rapi
from common_functions import *
msg = rapi.ApiMessage()
msg.product_list.response_code = 1
msg.product_list.product_list["Cappuccino_0"] = "Cappuccino"
msg
Output shows the protocol buffer created message:
product_list {
response_code: SUCCESS
product_list {
key: "Cappucino_0"
value: "Cappuccino"
}
}
Serialize the protocol buffer into a string
msg_b = msg.SerializeToString()
msg_b
Output shows the serialized string.
b'\x92\x80\x08\x1d\x08d\x12\x19\n\x0bCappucino_0\x12\nCappuccino'
Print the serialized string as hexadecimal code.
list(map(hex, msg_b))
Output shows previous string as hex values.
['0x92', '0x80', '0x8', '0x1d', '0x8', '0x1', '0x12', '0x19', '0xa', '0xb', '0x43', '0x61', '0x70', '0x70', '0x75', '0x63', '0x69', '0x6e', '0x6f', '0x5f', '0x30', '0x12', '0xa', '0x43', '0x61', '0x70', '0x70', '0x75', '0x63', '0x63', '0x69', '0x6e', '0x6f']
Create the framing for the serial interface.
msg_b = append_crc(msg_b)
msg_b = escape_message(msg_b)
msg_b = add_start_and_stop(msg_b)
list(map(hex, msg_b))
Output hex values.
['0x2', '0x92', '0x80', '0x8', '0x1d', '0x8', '0x1', '0x12', '0x19', '0xa', '0xb', '0x43', '0x61', '0x70', '0x70', '0x75', '0x63', '0x69', '0x6e', '0x6f', '0x5f', '0x30', '0x12', '0xa', '0x43', '0x61', '0x70', '0x70', '0x75', '0x63', '0x63', '0x69', '0x6e', '0x6f', '0x21', '0x20', '0x3']
If input is a list of hex values, convert them into a bytes object.
msg_b = ['0x2', '0x92', '0x80', '0x8', '0x1d', '0x8', '0x1', '0x12', '0x19', '0xa', '0xb', '0x43', '0x61', '0x70', '0x70', '0x75', '0x63', '0x69', '0x6e', '0x6f', '0x5f', '0x30', '0x12', '0xa', '0x43', '0x61', '0x70', '0x70', '0x75', '0x63', '0x63', '0x69', '0x6e', '0x6f', '0x21', '0x20', '0x3']
msg_b = bytes([int(x,0) for x in msg_b])
Remove framing from the serial interface
msg_b = strip_start_and_stop(msg_b)
msg_b = unescape_message(msg_b)
msg_b = remove_crc(msg_b)
list(map(hex, msg_b))
Output hex values.
['0x92', '0x80', '0x8', '0x1d', '0x8', '0x64', '0x12', '0x19', '0xa', '0xb', '0x43', '0x61', '0x70', '0x70', '0x75', '0x63', '0x69', '0x6e', '0x6f', '0x5f', '0x30', '0x12', '0xa', '0x43', '0x61', '0x70', '0x70', '0x75', '0x63', '0x63', '0x69', '0x6e', '0x6f']
Back conversion into protocoll buffer message.
rec_msg = rapi.ApiMessage()
rec_msg.ParseFromString(msg_b)
print(rec_msg)
Master/Slave Tutorial
There is no instruction for the master/slave files. Play and learn.
Creating an example/docu package
The script generate_zip_package.py creates a ZIP file which contains several example sources and the protobuf proto-file for the remote API together with example encodings of messages. To run the script, make sure you have setup the environment as described in Preparation. Then simply run
python3 generate_zip_package.py
Now you find a ZIP file called RemoteApi.zip with the example code and the documentation.