Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does it support serialization to SerializedPayload_t at the Python layer? #114

Open
Jackie2chen opened this issue Mar 27, 2024 · 3 comments

Comments

@Jackie2chen
Copy link

i cant find the SerializedPayload_t in fastdds_python

@jtroeth1
Copy link

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.

@Jackie2chen
Copy link
Author

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
Copy link
Author

Jackie2chen commented Mar 28, 2024


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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants