关于FastDDS在C#中使用的简单实例

2023-11-07 16:40

本文主要是介绍关于FastDDS在C#中使用的简单实例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

fastdds是一套开源的又C++开发的基于DDS通讯协议的中间件。因为项目需要,需要在B/S项目中使用,我们采用的是C#开发,所以如何打通如何在C#中使用fastdds,下面就是实现的过程:

首先,我们需要下载fastdds的源码,我是直接下载到一个eProsima_Fast-DDS-2.10.0-Windows.exe,安装包的方式安装代码以及需要的东西,这个方式其实就是把代码包装成安装包,所以下载源码和这个是基本一致的。

1)前提条件

在Windows环境中从源安装FastDDS需要在系统中安装以下工具:

Visual Studio

Chocolatey

CMake、pip3、wget和git

Gtest[可选]

具体安装就不做过多介绍,其实我主要用到的是vs2022,和CMake。

vs2022需要安装支持c++开发的功能模块

2)安装

eProsima_Fast-DDS-2.10.0-Windows.exe 安装完成后,会在目录下生成项目文件夹,如下图所示:

 examples就是该中间件使用的详细例子文件夹。例子路径:

F:\Program Files\eProsima\fastrtps 2.10.0\examples\cpp\dds

接下来,我们需要对所有的例子进行生成可以以vs打开的项目,我们需要在上免得路径下进入命令窗体,

然后依次执行如下命令:


mkdir build
cd build
cmake ..//cmake ..可以换成下面这句,这是官网的推荐
cmake -Bbuildexample -DFASTDDS_STATIC=ON .

就可以在build文件夹下生成所有实例的项目文件,用vs可以打开。

关于上面项目能否正常运行的问题,请参考进行处理:Windows下运行Fast DDS示例程序(包含.idl文件的使用方法)_fastdds_Eliza_Her的博客-CSDN博客

接下来,就是对一些可以用到的实例进行简单处理,生成可以被C#调用的动态库。

 我们新建一个C++的动态库项目,将HelloWorldExample的例子进行处理,引入需要的文件,结构如下:

其中FastDDSWrapper.h,FastDDSWrapper.cpp文件时新建的,其他的都是用的 HelloWorldExample的文件。

FastDDSWrapper.h

#pragma once
#include "pch.h"#define FASTDDSWRAPPER_EXPORTS true#ifdef FASTDDSWRAPPER_EXPORTS
#define FASTDDSWRAPPER_API __declspec(dllexport)
#else
#define FASTDDSWRAPPER_API __declspec(dllimport)
#endifextern "C" {FASTDDSWRAPPER_API int init_publisher(bool use_env);FASTDDSWRAPPER_API int publish(const char* message);FASTDDSWRAPPER_API int init_subscriber();FASTDDSWRAPPER_API int subscribe(char* buffer, int bufferSize);
}

 FastDDSWrapper.cpp

// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License./*** @file HelloWorldPublisher.cpp**/
#include "pch.h"
#include "HelloWorldPublisher.h"
#include <fastrtps/attributes/ParticipantAttributes.h>
#include <fastrtps/attributes/PublisherAttributes.h>
#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
#include <fastdds/dds/publisher/Publisher.hpp>
#include <fastdds/dds/publisher/qos/PublisherQos.hpp>
#include <fastdds/dds/publisher/DataWriter.hpp>
#include <fastdds/dds/publisher/qos/DataWriterQos.hpp>#include <thread>using namespace eprosima::fastdds::dds;HelloWorldPublisher::HelloWorldPublisher(): participant_(nullptr), publisher_(nullptr), topic_(nullptr), writer_(nullptr), type_(new HelloWorldPubSubType())
{
}bool HelloWorldPublisher::init(bool use_env)
{hello_.index(0);hello_.message("HelloWorld");DomainParticipantQos pqos = PARTICIPANT_QOS_DEFAULT;pqos.name("Participant_pub");auto factory = DomainParticipantFactory::get_instance();if (use_env){factory->load_profiles();factory->get_default_participant_qos(pqos);}participant_ = factory->create_participant(0, pqos);if (participant_ == nullptr){return false;}//REGISTER THE TYPEtype_.register_type(participant_);//CREATE THE PUBLISHERPublisherQos pubqos = PUBLISHER_QOS_DEFAULT;if (use_env){participant_->get_default_publisher_qos(pubqos);}publisher_ = participant_->create_publisher(pubqos,nullptr);if (publisher_ == nullptr){return false;}//CREATE THE TOPICTopicQos tqos = TOPIC_QOS_DEFAULT;if (use_env){participant_->get_default_topic_qos(tqos);}topic_ = participant_->create_topic("HelloWorldTopic","HelloWorld",tqos);if (topic_ == nullptr){return false;}// CREATE THE WRITERDataWriterQos wqos = DATAWRITER_QOS_DEFAULT;if (use_env){publisher_->get_default_datawriter_qos(wqos);}writer_ = publisher_->create_datawriter(topic_,wqos,&listener_);if (writer_ == nullptr){return false;}return true;
}HelloWorldPublisher::~HelloWorldPublisher()
{if (writer_ != nullptr){publisher_->delete_datawriter(writer_);}if (publisher_ != nullptr){participant_->delete_publisher(publisher_);}if (topic_ != nullptr){participant_->delete_topic(topic_);}DomainParticipantFactory::get_instance()->delete_participant(participant_);
}void HelloWorldPublisher::PubListener::on_publication_matched(eprosima::fastdds::dds::DataWriter*,const eprosima::fastdds::dds::PublicationMatchedStatus& info)
{if (info.current_count_change == 1){matched_ = info.total_count;firstConnected_ = true;std::cout << "Publisher matched." << std::endl;}else if (info.current_count_change == -1){matched_ = info.total_count;std::cout << "Publisher unmatched." << std::endl;}else{std::cout << info.current_count_change<< " is not a valid value for PublicationMatchedStatus current count change" << std::endl;}
}void HelloWorldPublisher::runThread(uint32_t samples,uint32_t sleep)
{if (samples == 0){while (!stop_){if (publish(false)){std::cout << "Message: " << hello_.message() << " with index: " << hello_.index()<< " SENT" << std::endl;}std::this_thread::sleep_for(std::chrono::milliseconds(sleep));}}else{for (uint32_t i = 0; i < samples; ++i){if (!publish()){--i;}else{std::cout << "Message: " << hello_.message() << " with index: " << hello_.index()<< " SENT" << std::endl;}std::this_thread::sleep_for(std::chrono::milliseconds(sleep));}}
}void HelloWorldPublisher::run(uint32_t samples,uint32_t sleep)
{stop_ = false;std::thread thread(&HelloWorldPublisher::runThread, this, samples, sleep);if (samples == 0){std::cout << "Publisher running. Please press enter to stop the Publisher at any time." << std::endl;std::cin.ignore();stop_ = true;}else{std::cout << "Publisher running " << samples << " samples." << std::endl;}thread.join();
}bool HelloWorldPublisher::publish(bool waitForListener)
{if (listener_.firstConnected_ || !waitForListener || listener_.matched_ > 0){hello_.index(hello_.index() + 1);writer_->write(&hello_);return true;}return false;
}

这两个时对方法的再次封装处理。

都处理完成后,最重要的步骤需要对项目进行配置,要不然无法编译通过,如下所示:

以上需要配置的都是添加,不要删除项目原有的,不要删除项目原有的,不要删除项目原有的,添加的东西依次为:

 包含目录:F:\Program Files\eProsima\fastrtps 2.10.0\include

库目录:F:\Program Files\eProsima\fastrtps 2.10.0\lib\x64Win64VS2019

链接器-输入-附加依赖项:F:\Program Files\eProsima\fastrtps 2.10.0\lib\x64Win64VS2019\fastrtps-2.10.lib;F:\Program Files\eProsima\fastrtps 2.10.0\lib\x64Win64VS2019\libfastcdr-1.0.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;comdlg32.lib;advapi32.lib

上面标红的都是根据自己安装fastdds的实例调整路径。

都配置完成后,修改

需要release ,然后编译。到此,就可以生成需要的.dll文件。

3)C#调用

建立C# 控制台程序,调用生成的.dll文件,

同时运行两个项目,就可以使用了。

内容补充:关于在windows下如何根据.idl 文件内容的结构体生成对应的fastdds核心的代码,基于上面已经通过步骤2 “eProsima_Fast-DDS-2.10.0-Windows.exe”安装的,安装完成后可以在安装路径下看到如下内容:

到此之后,就可以新建一个文件夹,里面建立一个HelloWorld.idl文件(需要生成代码的结构体),在该文件的路径下通过cmd命令进入到命令窗体,执行如下命令:

fastddsgen.bat -example CMake HelloWorld.idl -ppDisable

注意后面的参数“ -ppDisable” ,意思是

  • 如果您的 idl 上没有预处理器指令,请使用-ppDisable

 如上图所示,表示已经自动生成了需要的代码文件,如下图所示:

 到此时,就可以直接在继续在cmd命令窗体执行如下命令:

cmake -Bbuildexample -DFASTDDS_STATIC=ON .

注意参数:-Bbuildexample 是将生成的vs代码放在当前路径下新建的buildexample文件夹下。

生成的代码如下:

 

,可以以用vs2022打开,安装上面的步骤进行编译。 

过程中需要的安装文件下载地址如下:

链接:https://pan.baidu.com/s/1xVN_mDjb3pIRJynx0bHluQ?pwd=ewfn 
提取码:ewfn

这篇关于关于FastDDS在C#中使用的简单实例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/364933

相关文章

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

C# 比较两个list 之间元素差异的常用方法

《C#比较两个list之间元素差异的常用方法》:本文主要介绍C#比较两个list之间元素差异,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 使用Except方法2. 使用Except的逆操作3. 使用LINQ的Join,GroupJoin

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

ModelMapper基本使用和常见场景示例详解

《ModelMapper基本使用和常见场景示例详解》ModelMapper是Java对象映射库,支持自动映射、自定义规则、集合转换及高级配置(如匹配策略、转换器),可集成SpringBoot,减少样板... 目录1. 添加依赖2. 基本用法示例:简单对象映射3. 自定义映射规则4. 集合映射5. 高级配置匹

Spring 框架之Springfox使用详解

《Spring框架之Springfox使用详解》Springfox是Spring框架的API文档工具,集成Swagger规范,自动生成文档并支持多语言/版本,模块化设计便于扩展,但存在版本兼容性、性... 目录核心功能工作原理模块化设计使用示例注意事项优缺点优点缺点总结适用场景建议总结Springfox 是

嵌入式数据库SQLite 3配置使用讲解

《嵌入式数据库SQLite3配置使用讲解》本文强调嵌入式项目中SQLite3数据库的重要性,因其零配置、轻量级、跨平台及事务处理特性,可保障数据溯源与责任明确,详细讲解安装配置、基础语法及SQLit... 目录0、惨痛教训1、SQLite3环境配置(1)、下载安装SQLite库(2)、解压下载的文件(3)、