Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google Protobuf-C和x2B之间的UDP通信+;和Python-google.protobuf.message.DecodeError:unpack需要长度为4的字符串参数_Python_Protocol Buffers - Fatal编程技术网

Google Protobuf-C和x2B之间的UDP通信+;和Python-google.protobuf.message.DecodeError:unpack需要长度为4的字符串参数

Google Protobuf-C和x2B之间的UDP通信+;和Python-google.protobuf.message.DecodeError:unpack需要长度为4的字符串参数,python,protocol-buffers,Python,Protocol Buffers,下午好 我试图在CPP中的一个框架和Python中的另一个框架之间发送消息。我遵循了以下所示的相同流程: 我的Python服务器代码是: import socket from DiceData_pb2 import DiceData UDP_PORT=1555 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.bind(("", UDP_PORT)) dicedata = DiceData() while True:

下午好

我试图在CPP中的一个框架和Python中的另一个框架之间发送消息。我遵循了以下所示的相同流程:

我的Python服务器代码是:

import socket
from DiceData_pb2 import DiceData

UDP_PORT=1555

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(("", UDP_PORT))

dicedata = DiceData()
while True:
    data, addr = sock.recvfrom(1024)
    print data
    dicedata.ParseFromString(data)
    print ("gyrox = {0}".format(dicedata.gyrox))
    print("gyroy = {0}".format(dicedata.gyrox))
    print("gyroz = {0}".format(dicedata.gyroz))
    print("accelx = {0}".format(dicedata.accelx))
    print("accely = {0}".format(dicedata.accely))
    print("accelz = {0}".format(dicedata.accelz))
    print("roll = {0}".format(dicedata.roll))
    print("pitch = {0}".format(dicedata.pitch))
    print("yaw = {0}".format(dicedata.yaw))
    print("side = {0}".format(dicedata.side))
    print("certainty = {0}".format(dicedata.certainty))
    print("time = {0}".format(dicedata.time))
.proto文件如下所示:

package prototest;

message DiceData {
  required float gyrox = 1;
  required float gyroy = 2;
  required float gyroz = 3;
  required float accelx = 4;
  required float accely = 5;
  required float accelz = 6;
  required float roll = 7;
  required float pitch = 8;
  required float yaw = 9;
  required int32 side = 10;
  required float certainty = 11;
  required string time = 12;
}
我知道通信正在工作,因为服务器接收第一条消息并将其作为垃圾打印。但是,在到达ParseFromString行后,会发生以下错误:

Traceback (most recent call last):
  File "server.py", line 13, in <module>
    dicedata.ParseFromString(data)
  File "/usr/local/lib/python2.7/dist-packages/google/protobuf/message.py", line 185, in ParseFromString
    self.MergeFromString(serialized)
  File "/usr/local/lib/python2.7/dist-packages/google/protobuf/internal/python_message.py", line 1095, in MergeFromString
    raise message_mod.DecodeError(e)
google.protobuf.message.DecodeError: unpack requires a string argument of length 4
回溯(最近一次呼叫最后一次):
文件“server.py”,第13行,在
dicedata.ParseFromString(数据)
文件“/usr/local/lib/python2.7/dist packages/google/protobuf/message.py”,第185行,格式为ParseFromString
self.MergeFromString(序列化)
文件“/usr/local/lib/python2.7/dist packages/google/protobuf/internal/python_message.py”,第1095行,位于MergeFromString中
升起消息模块解码错误(e)
google.protobuf.message.DecodeError:解包需要长度为4的字符串参数

有人知道我怎么解决这个问题吗?我知道字符串不是空的,因为前一行打印了垃圾,但我似乎无法将字符串转换回数据结构

>你链接的问题中的C++代码被破坏了。它包含以下行:

sendto(sock, buf.data(), strlen(buf.c_str()), 0, (struct sockaddr *)&addr, sizeof(addr));
这是错误的!它将在第一个零值字节处切断消息。它应该是这样的:

sendto(sock, buf.data(), buf.size(), 0, (struct sockaddr *)&addr, sizeof(addr));
这肯定会导致您看到的错误


< >我编辑了另一个问题来添加这个修复。

< p>你链接的问题中的C++代码被破坏了。它包含以下行:

sendto(sock, buf.data(), strlen(buf.c_str()), 0, (struct sockaddr *)&addr, sizeof(addr));
这是错误的!它将在第一个零值字节处切断消息。它应该是这样的:

sendto(sock, buf.data(), buf.size(), 0, (struct sockaddr *)&addr, sizeof(addr));
这肯定会导致您看到的错误

我已编辑了其他问题以添加此修复