試圖通過 BLE 讀取從 ESP32 芯片發送的 protobuf 訊息的結果,并且無法反序列化傳入的訊息。在 Android 設備的 Ionic 應用程式中使用 protobuf for javascript。這是我的代碼:
import * as goog from 'google-protobuf';
export class PrepareMessageService {
protoMessages = require('../assets/proto-js/wifi_config_pb.js');
cmdSetConfig = new this.protoMessages.CmdSetConfig();
respSetConfig = new this.protoMessages.RespSetConfig();
wiFiConfigPayload = new this.protoMessages.WiFiConfigPayload();
deserializeToBinary(message: any){
const binMsg = new Uint8Array(message);
const deserializedMsg = this.protoMessages.RespSetConfig.deserializeBinary(binMsg);
console.log(JSON.stringify(deserializedMsg));
}
console.log 的輸出是:{"wrappers_":null,"arrayIndexOffset_":-1,"array":[],"pivot_":1.7976931348623157e 308,"convertedPrimitiveFields_":{}
我期待在“陣列”中看到一些內容,但它是空的。相反,如果我使用以下代碼通過 BLE 列印從 EPS32 芯片接收到的訊息的每個元素(函式 deserializeToBinary 的引數):
for (let i = 0; i < message.byteLength; i ){
console.log('message[', i, ']: ', message.getUint8(i));
}
這是輸出:
message[0]: 8
message[1]: 3
message[2]: 106
message[3]: 2
message[4]: 8
message[5]: 6
Now I know for a fact that message[5] correctly represents the status of RespSetConfig (shown in my main protobuf file below) because each time I change it on the ESP32 chip side, I get the correct code in message[5]. The constants.proto file shown below shows the various status codes for RespSetConfig. So why isn't my code in deserializeToBinary giving me the correct representation for all elements of the message argument? Even though the ESP32 sends me different status values for RespSetConfig, the console log from deserializeToBinary prints the exact same thing, with no clear indication of what the status value of RespSetConfig is. In this case, I can work with the different elements of the message論點,但在我需要正確反序列化傳入的 protobuf 回應的其他情況下,這不一定能解決我的問題。
這是主要的 protobuf 檔案:
syntax = "proto3";
package espressif;
import "constants.proto";
import "wifi_constants.proto";
message CmdGetStatus {}
message RespGetStatus {
Status status = 1;
WifiStationState sta_state = 2;
oneof state {
WifiConnectFailedReason fail_reason = 10;
WifiConnectedState connected = 11;
}
}
message CmdSetConfig {
string ssid = 1;
string passphrase = 2;
bytes bssid = 3;
int32 channel = 4;
}
message RespSetConfig {
Status status = 1;
}
message CmdApplyConfig {}
message RespApplyConfig {
Status status = 1;
}
enum WiFiConfigMsgType {
TypeCmdGetStatus = 0;
TypeRespGetStatus = 1;
TypeCmdSetConfig = 2;
TypeRespSetConfig = 3;
TypeCmdApplyConfig = 4;
TypeRespApplyConfig = 5;
}
message WiFiConfigPayload {
WiFiConfigMsgType msg = 1;
oneof payload {
CmdGetStatus cmd_get_status = 10;
RespGetStatus resp_get_status = 11;
CmdSetConfig cmd_set_config = 12;
RespSetConfig resp_set_config = 13;
CmdApplyConfig cmd_apply_config = 14;
RespApplyConfig resp_apply_config = 15;
}
}
這是在主 proto 檔案中匯入的 constants.proto 檔案:
syntax = "proto3";
package espressif;
/* Allowed values for the status
* of a protocomm instance */
enum Status {
Success = 0;
InvalidSecScheme = 1;
InvalidProto = 2;
TooManySessions = 3;
InvalidArgument = 4;
InternalError = 5;
CryptoError = 6;
InvalidSession = 7;
}
uj5u.com熱心網友回復:
弄清楚為什么這不起作用。我的 deserializeToBinary 函式中有很多錯誤。這有效:
deserializeToBinary(message: any){
const binMsg = new Uint8Array(message.byteLength);
//for loop required to create a buffer array because `message` is a DataView format
for (let i = 0; i < message.byteLength; i ){
binMsg[i] = message.getUint8(i);
console.log('protobuf response from ESP32 chip: ', binMsg[i]); //this prints protobuf response elements stored in the binMsg array. Output is as expected in question.
};
然后,我們可以在 binMsg 上使用 deserializeBinary 對其進行反序列化,然后在反序列化的訊息上使用 protobuf 生成的函式來獲取我們需要的資料。例如,以下代碼將通過首先反序列化 binMsg withdeserializeBinary然后獲取訊息狀態 with來提供 WifiConfigPayload 物件中 respSetConfig 訊息型別的狀態getRestSetConfig().getStatus(),這兩者都是由 google protobuf 編譯器生成的: this.messages.WiFiConfigPayload.deserializeBinary(binMsg).getRespSetConfig().getStatus()
要使用 protobuf 訊息,必須了解 protobuf 訊息的編碼/解碼。Google 網站上的以下鏈接可以讓您開始了解如何編碼/解碼 protobuf 訊息:https ://developers.google.com/protocol-buffers/docs/encoding
也就是說,Google 自己的檔案對于那些剛接觸 protobufs 的人來說是不夠的。以下鏈接在解釋 protobuf 訊息的編碼/解碼方面做得更好:https ://medium.com/nerd-for-tech/protobuf-what-why-fcb324a64564
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/441165.html
標籤:javascript 打字稿 离子框架 协议缓冲区
下一篇:使用Ionic/React/Typescript傳遞道具時出錯-React.FC中的道具錯誤<'wrongpropshere'>
