Fast-DDS-python icon indicating copy to clipboard operation
Fast-DDS-python copied to clipboard

Does it support serialization to SerializedPayload_t at the Python layer?

Open Jackie2chen opened this issue 1 year ago • 3 comments

i cant find the SerializedPayload_t in fastdds_python

Jackie2chen avatar Mar 27 '24 02:03 Jackie2chen

No. You can modify the templates in FastDDSGen to add any helper functions you need. I had a go at adding these types but was unsuccessful. In the end I wrote a deserialze overload like this

    //This is used for python deserialization
    bool HardwareStatusPubSubType::deserialize(char* rawData, uint32_t dataSize, HardwareStatus* data)
    {
        if (m_payload == nullptr)
            m_payload = new eprosima::fastrtps::rtps::SerializedPayload_t();

        m_payload->length = dataSize;
        m_payload->data = reinterpret_cast<unsigned char*>(rawData);

        return this->deserialize(m_payload, data);
    }

Then in python I can use a byte array to provide the data type to deserialize.

jtroeth1 avatar Mar 28 '24 01:03 jtroeth1

data_bytes = b"data"
        char_pointer = ctypes.c_char_p(data_bytes)
        print(char_pointer)
        print(data)
        datasize=self.data_type.serialize(data,char_pointer)

but print::

   c_char_p(139629848265840)
<basestructure.BaseHeader; proxy of <Swig Object of type 'krider::basestructure::BaseHeader *' at 0x7efe1b7f72a0> >
TypeError: Wrong number or type of arguments for overloaded function 'BaseHeaderPubSubType_serialize'.
  Possible C/C++ prototypes are:
    krider::basestructure::BaseHeaderPubSubType::serialize(void *,eprosima::fastrtps::rtps::SerializedPayload_t *)
    krider::basestructure::BaseHeaderPubSubType::serialize(void *,eprosima::fastrtps::rtps::SerializedPayload_t *,eprosima::fastdds::dds::DataRepresentationId_t)
    krider::basestructure::BaseHeaderPubSubType::serialize(krider::basestructure::BaseHeader *,char *)
    
is something wrong?
 

Jackie2chen avatar Mar 28 '24 08:03 Jackie2chen


char* serialize1(type* data )
{
    if (data != nullptr)
    {
        char* rawData;
        auto m_payload=new eprosima::fastrtps::rtps::SerializedPayload_t();

        if (!this->serialize(data, m_payload, eprosima::fastdds::dds::DEFAULT_DATA_REPRESENTATION))
        {
            std::cout << "serialize err" << std::endl;
            return 0;
        }

        // rawData=reinterpret_cast<char*>(data);
        rawData = new char[m_payload->length];
        if (rawData == nullptr)
        {
            return 0;
            std::cout<<"rawData err"<<std::endl;
        }
        std::memcpy(rawData, m_payload->data, m_payload->length);
        return rawData;

    }
    return 0;
}

and in  bool BaseHeaderPubSubType::serialize (void* data,SerializedPayload_t* payload,DataRepresentationId_t data_representation)
          try
            {
                // Serialize encapsulation
                ser.serialize_encapsulation();
                // Serialize the object.
                ser << *p_type;
            }
            catch (eprosima::fastcdr::exception::Exception& e)
            {
                std::cout << "Exception caught: " << e.what() << std::endl;
                return false;
            }

”Exception caught:Not enough memory in the buffer stream fastdds“

is something wrong?

Jackie2chen avatar Mar 28 '24 09:03 Jackie2chen