2023/12/12 LabVIEW2020 with Python 3.6.8實作Protocol buffer序列化流程-Pyhon下的函數驗証

接著2023/12/12 LabVIEW2020 with Python 3.6.8實作Protocol buffer序列化流程-建置proto檔並產生Python library (.py)這一篇的後續。
這一篇是要記錄要在Python下確認protobuf library有正常運作。

步驟
1. 建立writer.py,將資料序列化後,寫到helloworld.buffer檔案中。
2. 建立reader.py,將序列化後的helloworld.buffer檔案資料讀出,並反序列化後讀出,顯示原始資料在視窗中。

#riter.py
from helloworld_pb2 import helloworld
 
def main():
    hw = helloworld()
    hw.id = 123
    hw.name = "Jobs"
    print(hw)
 
    with open("helloworld.buffer", "wb") as f:
        f.write(hw.SerializeToString())
 
if __name__ == "__main__":
    main()
#reader.py
from helloworld_pb2 import helloworld
 
def main():
    hw = helloworld()
    with open("helloworld.buffer", "rb") as f:
        hw.ParseFromString(f.read())
        print(hw.id)
        print(hw.name)
  
if __name__ == "__main__":
    main()
序列化及反序列化後的結果
序列化後的二進制檔

One reply on “2023/12/12 LabVIEW2020 with Python 3.6.8實作Protocol buffer序列化流程-Pyhon下的函數驗証

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *