Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.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
python中的Protobuf在消息反序列化期间抱怨';意外的端组标签&x27; 我正在开发ZMQ/ToBuffF应用程序,并且我对从C++发送到Python的消息反序列化有问题。我很容易处理Python到C++的消息,但是在另一个方向,我有个问题。_Python_C++_Protocol Buffers_Zeromq - Fatal编程技术网

python中的Protobuf在消息反序列化期间抱怨';意外的端组标签&x27; 我正在开发ZMQ/ToBuffF应用程序,并且我对从C++发送到Python的消息反序列化有问题。我很容易处理Python到C++的消息,但是在另一个方向,我有个问题。

python中的Protobuf在消息反序列化期间抱怨';意外的端组标签&x27; 我正在开发ZMQ/ToBuffF应用程序,并且我对从C++发送到Python的消息反序列化有问题。我很容易处理Python到C++的消息,但是在另一个方向,我有个问题。,python,c++,protocol-buffers,zeromq,Python,C++,Protocol Buffers,Zeromq,python客户端应用程序中的Protobuf库抱怨它检测到: 文件“C:\Python27\lib\site packages\google\protobuf\internal\python\u message.py”,第844行,位于MergeFromString中 引发消息\u mod.DecodeError(“意外的结束组标记”) 我假定C++ Serial化和Python反序列化之间存在问题。我想知道C/C++中的空终止符是否有问题 我使用RaspberryPi running Ras

python客户端应用程序中的Protobuf库抱怨它检测到:
文件“C:\Python27\lib\site packages\google\protobuf\internal\python\u message.py”,第844行,位于MergeFromString中 引发消息\u mod.DecodeError(“意外的结束组标记”)

我假定C++ Serial化和Python反序列化之间存在问题。我想知道C/C++中的空终止符是否有问题

我使用RaspberryPi running Raspian C++代码和X64 CPU运行Python代码的Windows 7。

这是我的C++序列化代码…< /P>

// Test Code.
// Try to send some 'demo' response back.
RPiProtocol::Message response;
std::string response_string;
response.set_type(RPiProtocol::Message::RESPONSE);
response.set_command(RPiProtocol::Message::GET_SYS_INFO);
response.set_version(0);

// Serialize ZMQ message to string.
if (response.SerializeToString(&response_string))
{
    //  Send response message back to the client.
    zmq::message_t reply(response_string.length());
    memcpy((void *)reply.data(), (void *)&response_string, response_string.length());
    socket.send(reply);
}
#  Get the reply.
message = socket.recv()
response = rpi_protocol_pb2.Message()

# This line fails.
response.ParseFromString(str(message))
这是我的python反序列化代码

// Test Code.
// Try to send some 'demo' response back.
RPiProtocol::Message response;
std::string response_string;
response.set_type(RPiProtocol::Message::RESPONSE);
response.set_command(RPiProtocol::Message::GET_SYS_INFO);
response.set_version(0);

// Serialize ZMQ message to string.
if (response.SerializeToString(&response_string))
{
    //  Send response message back to the client.
    zmq::message_t reply(response_string.length());
    memcpy((void *)reply.data(), (void *)&response_string, response_string.length());
    socket.send(reply);
}
#  Get the reply.
message = socket.recv()
response = rpi_protocol_pb2.Message()

# This line fails.
response.ParseFromString(str(message))
我调试了此函数中的反序列化失败\google\protobuf\internal\python\u message.py

  def InternalParse(self, buffer, pos, end):
    self._Modified()
    field_dict = self._fields
    unknown_field_list = self._unknown_fields
    while pos != end:
      (tag_bytes, new_pos) = local_ReadTag(buffer, pos)
      field_decoder, field_desc = decoders_by_tag.get(tag_bytes, (None, None))
      if field_decoder is None:
        value_start_pos = new_pos
        new_pos = local_SkipField(buffer, new_pos, end, tag_bytes)
        if new_pos == -1: # HERE I HAVE -1 !!!
          return pos
        if not unknown_field_list:
          unknown_field_list = self._unknown_fields = []
        unknown_field_list.append((tag_bytes, buffer[value_start_pos:new_pos]))
        pos = new_pos
      else:
        pos = field_decoder(buffer, new_pos, end, self, field_dict)
        if field_desc:
          self._UpdateOneofState(field_desc)
    return pos
  cls._InternalParse = InternalParse

你能帮我启用我的应用程序吗?

最后我找到了错误代码。我在C++服务器中的这一行中有一个错误:

memcpy((void *)reply.data(), (void *)&response_string, response_string.length());
与上面的错误代码不同,它应该是:

memcpy((void *)reply.data(), (void *)response_string.data(), response_string.length());

我理解了如何将C++字符串转换成ZMQ字符串,因为我在Web上找到了这个函数:

//  Convert string to 0MQ string and send to socket    
static bool s_send (zmq::socket_t & socket, const std::string & string) {

    zmq::message_t message(string.size());
    memcpy (message.data(), string.data(), string.size());

    bool rc = socket.send (message);
    return (rc);
}

下面是链接到 ZHelpS.Hpp头文件,其中包含上面粘贴的函数和许多其他基于C++的基于ZMQ的应用程序有用的函数:


我在python中反序列化protobuff数据时也遇到了问题。protobuff数据已使用“C”protobuff代码序列化

简单的回答是看如何使用binascii.unhexlify()

在python上序列化protobuff并将其发送到反序列化的“C”代码,效果很好。但直到我做了以下操作,情况才相反:binstring=binascii.unhexlify(hexstring)。然后protoBuff.ParseFromString(binstring)工作得很好