fastdds实现set get的通信方式 c++示例代码
时间: 2024-03-30 21:37:05 浏览: 135
以下是使用 Fast DDS 实现 set 和 get 方法通信的 C++ 示例代码:
```c++
#include <fastrtps/attributes/ParticipantAttributes.h>
#include <fastrtps/attributes/PublisherAttributes.h>
#include <fastrtps/attributes/SubscriberAttributes.h>
#include <fastrtps/Domain.h>
#include <fastrtps/publisher/Publisher.h>
#include <fastrtps/subscriber/Subscriber.h>
#include <fastrtps/xmlparser/XMLProfileManager.h>
using namespace eprosima::fastrtps;
// 定义数据类型
struct MyDataType {
int32_t id;
std::string name;
};
class MyReaderListener : public SubscriberListener {
public:
void onNewDataMessage(Subscriber *sub) override {
MyDataType data;
SampleInfo_t info;
if (sub->takeNextData(&data, &info)) {
std::cout << "Received data: " << data.name << std::endl;
}
}
};
class MyWriter {
public:
MyWriter() {
// 创建参与者
ParticipantAttributes participantAttrs;
participantAttrs.rtps.builtin.domainId = 0;
participantAttrs.rtps.builtin.leaseDuration = c_TimeInfinite;
participantAttrs.rtps.setName("MyParticipant");
participant = Domain::createParticipant(participantAttrs);
if (participant == nullptr) {
std::cout << "Error creating participant" << std::endl;
return;
}
// 创建发布者
PublisherAttributes publisherAttrs;
publisherAttrs.topic.topicDataType = "MyDataType";
publisherAttrs.topic.topicName = "MyTopic";
publisher = Domain::createPublisher(participant, publisherAttrs, nullptr);
if (publisher == nullptr) {
std::cout << "Error creating publisher" << std::endl;
return;
}
}
void setData(const MyDataType& data) {
// 发布数据
publisher->write(&data);
}
private:
Participant *participant;
Publisher *publisher;
};
class MyReader {
public:
MyReader() {
// 创建参与者
ParticipantAttributes participantAttrs;
participantAttrs.rtps.builtin.domainId = 0;
participantAttrs.rtps.builtin.leaseDuration = c_TimeInfinite;
participantAttrs.rtps.setName("MyParticipant");
participant = Domain::createParticipant(participantAttrs);
if (participant == nullptr) {
std::cout << "Error creating participant" << std::endl;
return;
}
// 创建订阅者
SubscriberAttributes subscriberAttrs;
subscriberAttrs.topic.topicDataType = "MyDataType";
subscriberAttrs.topic.topicName = "MyTopic";
listener = new MyReaderListener();
subscriber = Domain::createSubscriber(participant, subscriberAttrs, listener);
if (subscriber == nullptr) {
std::cout << "Error creating subscriber" << std::endl;
return;
}
}
MyDataType getData() {
// 从订阅者中获取数据
MyDataType data;
SampleInfo_t info;
if (subscriber->takeNextData(&data, &info)) {
return data;
}
return {};
}
private:
Participant *participant;
Subscriber *subscriber;
MyReaderListener *listener;
};
int main() {
// 创建数据写入者和读取者
MyWriter writer;
MyReader reader;
// 设置数据并发布
MyDataType data;
data.id = 1;
data.name = "Alice";
writer.setData(data);
// 获取数据并打印
MyDataType receivedData = reader.getData();
std::cout << "Received data: " << receivedData.name << std::endl;
return 0;
}
```
以上代码演示了如何使用 Fast DDS 创建数据写入者和读取者,并通过发布/订阅模型实现数据的 set 和 get。在示例中,数据类型为 `MyDataType`,包括一个 `id` 和一个 `name` 字段,数据写入者使用 `MyWriter` 类,数据读取者使用 `MyReader` 类,数据写入者发布数据,数据读取者从订阅者中获取数据,并打印接收到的数据。
阅读全文