当我将int数组编码为消息,然后在我的C#应用程序中解码它时,我遇到了问题。当我解码int数组时,MQTT会将编码的字节发送到C#。我在那里破译它,我的所有价值在那里翻了一番。然后,我将相同的字节直接发送回NanoPB应用程序,在那里它也会被解码,这里的所有值都是正常的。怎么一回事?
我的原文件:
syntax = "proto2";
message stringCallback{
required string name = 1;
required string surname = 2;
required int32 age = 3;
repeated int32 values = 4;
}我的NanoPB编码函数:
bool IntArray_encode(pb_ostream_t *ostream, const pb_field_t *field, void* const* arg){
IntArray* arr = (IntArray*)*arg;
for(int i = 0; i < arr->count; i++){
if(!pb_encode_tag_for_field(ostream, field)){
Serial.println("Encoding failed!");
return false;
}
if(!pb_encode_svarint(ostream, arr->values[i])){
Serial.println("Encoding failed!");
return false;
}
}
return true;
}我的NanoPB解码功能:
bool IntArray_decode(pb_istream_t *stream, const pb_field_t *field, void** arg){
IntArray* arr = (IntArray*)*arg;
int64_t number;
if(!pb_decode_svarint(stream, &number)){
Serial.println("Decoding failed!");
return false;
}
Serial.println(number);
IntArray_add(arr, (int32_t)number);
return true;
}输出NanoPB:
Name = Testing
Surname = Purpose
Age = 23
[14 11 10 5 109 32321 23 19 15]输出C#:
Name = Testing
Surname = Purpose
Age = 23
[28 22 20 10 218 64642 46 38 30 ]发布于 2022-08-24 09:39:31
if(!pb_encode_svarint(ostream,arr->valuesi)){
斯瓦林()函数用于sint32和sint64类型。
您的.proto文件指定了int32,因此您应该使用pb_encode_varint()。
https://stackoverflow.com/questions/73469789
复制相似问题