Open Splice DDS 示例
根据实际项目写一个能够运行的示例程序:
Publisher:
#include <string> #include <sstream> #include <iostream> #include "ccpp_dds_dcps.h" /* Include the DDS::DCPS API */ #include"interlocks_DCPS.hpp" #include"interlock_enums_DCPS.hpp" #include"common_DCPS.hpp" #include "interlocksSplDcps.h" int main(){ dds::domain::DomainParticipant dp(0); dds::topic::qos::TopicQos topicQos=dp.default_topic_qos() // 保证当前Topic的信息保存在所有publisher之外,且在接下来的是可用的。 << dds::core::policy::Reliability::Reliable(); // 保证当前Topic信息传递的可靠性级别。 dds::topic::Topic<PyramidDDS::interlock_condition> inter_lock_topic(dp, "interlock_condition", topicQos); dds::pub::qos::PublisherQos pubQos = dp.default_publisher_qos() << dds::core::policy::Partition("room1"); dds::pub::Publisher pub(dp, pubQos); dds::pub::DataWriter<PyramidDDS::interlock_condition> dw(pub, inter_lock_topic); PyramidDDS::interlock_e interlock=PyramidDDS::interlock_e::eILK_COUNT; PyramidDDS::interlock_condition data(interlock,true,1,"message"); dw.write(data); std::cout<<"走到这一步"<<std::endl; return 0;
Suscriber:
#include <string> #include <sstream> #include <iostream> #include <algorithm> #include <thread> #include"interlocks_DCPS.hpp" #include"interlock_enums_DCPS.hpp" #include"common_DCPS.hpp" #include "interlocksSplDcps.h" #include "common/example_utilities.h" std::ostream& operator << (std::ostream& os, const PyramidDDS::interlock_condition& dc) { os << "(id = " << dc.condition() << ", interlock = " << dc.interlock() << ", message = " << dc.messageText() << ")"; return os; } int main(){ dds::domain::DomainParticipant dp(0); dds::topic::qos::TopicQos topicQos=dp.default_topic_qos() // 保证当前Topic的信息保存在所有publisher之外,且在接下来的是可用的。 << dds::core::policy::Reliability::Reliable(); // 保证当前Topic信息传递的可靠性级别。 dds::topic::Topic<PyramidDDS::interlock_condition> inter_lock_topic(dp, "interlock_condition", topicQos); dds::sub::qos::SubscriberQos subQos = dp.default_subscriber_qos() << dds::core::policy::Partition("room1"); dds::sub::Subscriber sub(dp, subQos); dds::sub::DataReader<PyramidDDS::interlock_condition> dr(sub, inter_lock_topic); while(true){ std::cout<<"开始读"<<std::endl; auto samples=dr.read(); std::for_each(samples.begin(), samples.end(), [](const dds::sub::Sample<PyramidDDS::interlock_condition>& s) { std::cout << s.data() << std::endl; }); } return 0;
注意:
在上述代码中,publisher和suscriber里面的dp,topic的name,partion都要保持一致,不然接收不到。
在suscriber里面,想打印信息到控制台里面,需要重载<<符号,不然的话会报错误,另外,不知道为什么,我用samples.bengin;samples<samples.end;samples++,也会报同类型错误,即使我不输出什么数据,很奇怪
编译执行程序需要链接文件,可以参考examples里面的示例