I'm having a hard time in realizing how to use the repeated field rule. for example, this is my .proto:
message Test
{
repeated float value = 1;
}
Run Code Online (Sandbox Code Playgroud)
now, I'm initialize a new Test object:
Test test = test_init_zero()
Run Code Online (Sandbox Code Playgroud)
Finally, I want to assign some values. For example:
float values[] = { 1.0, 2.2, 5.5, 7.13 }
Run Code Online (Sandbox Code Playgroud)
My question is how can I assign them? is it like
test.value = values
//or
test.value[0] = values[0] //... etc.
Run Code Online (Sandbox Code Playgroud)
and then, …
我有一个 proto 消息定义为:
message SimpleMessage {
repeated int32 number = 1;}
Run Code Online (Sandbox Code Playgroud)
现在,编译后,该字段为 of pb_callback_t,我想编写该函数。(没有 .options 文件)
现在,该函数应该包含在哪里以及包含哪些内容?数据本身存储在哪里以及如何访问它/为其分配新数据?
* 编辑 *
根据@Groo 的回答,这是我试过的代码:
typedef struct {
int numbers_decoded;
} DecodingState;
bool read_single_number(pb_istream_t *istream, const pb_field_t *field, void **arg)
{
// get the pointer to the custom state
DecodingState *state = (DecodingState*)(*arg);
int32_t value;
if (!pb_decode_varint32(istream, &value))
{
const char * error = PB_GET_ERROR(istream);
printf("Protobuf error: %s", error);
return false;
}
printf("Decoded successfully: %d", value);
state->numbers_decoded++;
return true;
}
int main(void) …Run Code Online (Sandbox Code Playgroud) 我正在使用 Nanopb,其中生成的 proto 文件中的字符串变量被转换为 pb_callback_t
所以,到目前为止,我正在尝试使用 nanopb 回调的测试示例;
bool encode_string(pb_ostream_t* stream, const pb_field_t* field, void* const* arg)
{
char str[14] = "Hello world!";
if (!pb_encode_tag_for_field(stream, field))
return false;
return pb_encode_string(stream, (uint8_t*)str, strlen(str));
}
Run Code Online (Sandbox Code Playgroud)
int main()
{
FeatureFile featurefile = FeatureFile_init_zero;
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
featurefile.features.Id.funcs.encode = &encode_string;
}
Run Code Online (Sandbox Code Playgroud)
但是在这个例子中,字符串“Hello world!” 是相当硬编码的 xD,我如何才能从 main 动态地将字符串传递给该函数?
我的任务是通过蓝牙从Android设备连接到运行nanoPB的设备.
我知道NonoPB处理诸如枚举之类的证据......如果它们通过,NanoPB可以处理枚举吗?或者失败,任何Protobuf模式如"Lite"允许他们进行通信.
我有一个非常简单的原型:
syntax = "proto2";
message TestMessage {
optional int32 val = 1;
optional string msg = 2; // I set max size to 40 in options, so TestMessage_size is defined.
}
Run Code Online (Sandbox Code Playgroud)
...我的 arduino 程序的主循环中有以下代码:
TestMessage test_msg = TestMessage_init_zero;
test_msg.val = 123;
// Print message length.
size_t msg_length;
bool get_msg_length = pb_get_encoded_size(&msg_length, TestMessage_fields, &test_msg);
Serial.println(msg_length);
// Encode and print message.
uint8_t testbuffer[TestMessage_size];
pb_ostream_t teststream = pb_ostream_from_buffer(testbuffer, sizeof(testbuffer));
bool teststatus = pb_encode(&teststream, TestMessage_fields, &test_msg);
if (!teststatus) {
Serial.println("Failed to encode test message."); …Run Code Online (Sandbox Code Playgroud) 我正在使用Nanopb尝试从基于VxWorks的National Compact Compact RIO(9025)发送protobuf消息。我的交叉编译效果很好,甚至可以发送不需要额外编码的数据类型的完整消息。让我着迷的是回调。我的代码是通过LabVIEW交叉编译和调用的,基于Nanopb的基于回调的结构似乎在目标计算机上中断(错误,崩溃,目标重新启动等)。如果我不带任何回调地运行它,则效果很好。
这是有问题的代码:
bool encode_string(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
{
char *str = "Woo hoo!";
if (!pb_encode_tag_for_field(stream, field))
return false;
return pb_encode_string(stream, (uint8_t*)str, strlen(str));
}
extern "C" uint16_t getPacket(uint8_t* packet)
{
uint8_t buffer[256];
uint16_t packetSize;
ExampleMsg msg = {};
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
msg.name.funcs.encode = &encode_string;
msg.value = 17;
msg.number = 18;
pb_encode(&stream, ExampleMsg_fields, &msg);
packetSize = stream.bytes_written;
memcpy(packet, buffer, 256);
return packetSize;
}
Run Code Online (Sandbox Code Playgroud)
这是原始文件:
syntax = "proto2"
message ExampleMsg { …Run Code Online (Sandbox Code Playgroud)