【发布时间】:2021-05-27 10:52:27
【问题描述】:
我正在开发一个 mqtt 项目,我订阅了一个以字节字符串形式向我返回数据的代理。我必须将该数据转换为protobuf,然后在dict中。这是我收到的数据示例。
b'\n\x108cf9572000023509\x10\x03\x1a\x06uplink \xd4\xc9\xc6\xea\x9a/*\x10b827ebfffebce2d30\xbe\xff\xff\xff\xff\xff\xff\xff\xff\x01=\x00\x00\x18AE4\x13YDH\x05P\x01Z\x01C`\x8c\x06h\x08z \x02\x05e!\x08\x01\x00f\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xd4\x0f\x00\x82\x01"\n\x10b827ebfffebce2d3\x10\xbe\xff\xff\xff\xff\xff\xff\xff\xff\x01\x1d\x00\x00\x18A'
我的 .proto 文件的结构
message RXInfoSimplified {
string ID = 1;
int32 RSSI = 2;
float SNR = 3;
}
message DeviceUplink {
string DevEUI = 1;
int64 ApplicationID = 2;
string MsgType = 3;
int64 Timestamp = 4;
string GatewayID = 5;
int32 RSSI = 6;
float SNR = 7;
float Frequency = 8;
int32 DataRate = 9;
bool ADR = 10;
string Class = 11;
uint32 FCnt = 12;
int32 FPort = 13;
bool Confirm = 14;
bytes Data = 15;
repeated RXInfoSimplified Gateways = 16;
}
我在回调中试过这个:
m = sub_message.DeviceUplink()
def on_message(client, userdata, msg):
m.ParseFromString(msg.payload)
print(m)
我明白了:
DevEUI: "8cf9572000023509"
ApplicationID: 3
MsgType: "uplink"
Timestamp: 1622117284775
GatewayID: "b827ebfffebce2d3"
RSSI: -61
SNR: 10.25
Frequency: 868.1
DataRate: 5
ADR: true
Class: "C"
FCnt: 796
FPort: 8
Data: "\002\005\220!\023\004\000p\001\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\335\016\000"
Gateways {
ID: "b827ebfffebce2d3"
RSSI: -61
SNR: 10.25
}
如何转换字符串中的数据值?
【问题讨论】:
标签: python string dictionary byte protocol-buffers