【fast-rtps】fastddsgen工具的使用

 fast-rtps中通过fastrtpsgen工具将IDL文件生成C++代码

要使用fastrtpsgen工具首先要安装以下三个库:

1、cd  ~/eProsima/src/fastcdr/build 

sudo make install 

默认安装在/usr/local/lib/libfastcdr.so

2、cd ~/eProsima/src/memory-main/build  

sudo make install

默认安装在/usr/local/lib/libfoonathan_memory-0.7.3.a

3、/usr/local/lib/libfastrtps.so

新建testRTPS文件夹:新建HelloWorld.idl

struct HelloWorld

{

        string msg;

}

执行fastddsgen工具

java -jar ~/eProsima/src/fastddsgen/share/fastddsgen/java/fastddsgen.jar -ezample CMake ~/testFastRTPS/HelloWorld.idl -d ./src

src目录下产生代码:

HelloWorldCdrAux.ipp HelloWorld.h HelloWorldPublisher.h HelloWorldPublisherTypes.cxx  

HelloWorldSubscriber.cxx HelloWorldCdrAux.hpp HelloWorld.cxx HelloWorldPublisher.cxx 

HelloWorldPublisherMain.cxx HelloWorldPublisherTypes.h HelloWorldSubscriber.h

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Fast-RTPS 是一个高性能的实时通信协议,用于在分布式系统中传输实时数据。在 Fast-RTPS 中,每个消息都由头部和数据组成。在本示例中,我们将展示如何解析 Fast-RTPS 消息的头部字段以及 topic name。 首先,我们需要了解 Fast-RTPS 消息头部字段的含义。Fast-RTPS 消息头部包括以下字段: - `protocolId`:Fast-RTPS 协议的版本号。 - `vendorId`:Fast-RTPS 实现的厂商 ID。 - `guidPrefix`:全局唯一标识符的前缀,用于识别 Fast-RTPS 实例。 - `guidEntityId`:Fast-RTPS 实体的唯一标识符。 - `sequenceNumber`:消息的序列号。 - `sourceTimestamp`:消息的时间戳。 - `destinationGuidPrefix`:消息的目标实体的 GUID 前缀。 其中,`guidPrefix` 和 `guidEntityId` 组成了消息的唯一标识符 GUID,用于在 Fast-RTPS 网络中识别消息。 接下来,我们可以通过以下代码示例解析 Fast-RTPS 消息的头部字段和 topic name: ```c++ #include <fastrtps/fastrtps.h> #include <fastrtps/attributes/TopicAttributes.h> void parseFastRTPSMessageHeader(const void* buffer, size_t size) { // Deserialize the message header using Fast-RTPS library eprosima::fastcdr::FastBuffer cdrbuffer(reinterpret_cast<char*>(const_cast<void*>(buffer)), size); eprosima::fastrtps::rtps::CDRMessage_t cdrm(cdrbuffer); eprosima::fastcdr::Cdr cdr_des(cdrm, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN, eprosima::fastcdr::Cdr::DDS_CDR); // Parse the fields in the header eprosima::fastrtps::rtps::Header_t header; header.readFromCDRMessage(cdrm); std::cout << "protocolId: " << std::to_string(header.protocolId) << std::endl; std::cout << "vendorId: " << std::to_string(header.vendorId) << std::endl; std::cout << "guidPrefix: " << header.guidPrefix << std::endl; std::cout << "guidEntityId: " << header.guidEntityId << std::endl; std::cout << "sequenceNumber: " << std::to_string(header.sequenceNumber.to64long()) << std::endl; std::cout << "sourceTimestamp: " << header.sourceTimestamp.to_string() << std::endl; std::cout << "destinationGuidPrefix: " << header.destinationGuidPrefix << std::endl; // Deserialize the topic name from the message payload eprosima::fastrtps::TopicAttributes topic_attr; topic_attr.deserialize(&cdr_des); std::cout << "topic name: " << topic_attr.getTopicName() << std::endl; } ``` 在上述示例中,我们使用Fast-RTPS 库中的 `Header_t` 类和 `TopicAttributes` 类来解析消息头部和 topic name。首先,我们将消息头部反序列化为 `Header_t` 类型,并读取其中的各个字段。然后,我们从消息负载中反序列化出 `TopicAttributes` 类型,以获取 topic name。 该示例代码可以在 Fast-RTPS 库提供的各种平台和语言中使用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值