RobotecAI / ros2cs

A C# (.Net) implementation of ros2 client library (rcl), enabling communication between ros2 ecosystem and C#/.Net applications such as Unity3D
Apache License 2.0
89 stars 22 forks source link

How to compile with NuGet installed packages? #7

Closed sk413025 closed 3 years ago

sk413025 commented 3 years ago

Thank you for releasing this great package, and I look forward to your help with compilation issues. I have been able to run the ROS2Talker and ROS2Listener examples in win10, and now I need to read the serial port, here is a simple example.

Use dotnet new console to create a project.

C:\dev\KneeBO

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       2021/9/9  下午 10:18           1492 CMakeLists.txt
-a----       2021/9/9  下午 10:24            171 KneeBO.csproj
-a----       2021/9/9  下午 07:16           1163 package.xml
-a----       2021/9/9  下午 08:28            668 Program.cs

namespace KneeBO { class Program{ static void Main(string[] args){ foreach (string portname in SerialPort.GetPortNames()) { Console.WriteLine(portname); } } } }


Where  `System.IO.Ports`  is [installed through nuget](https://www.nuget.org/packages/System.IO.Ports/).
To add `System.IO.Ports` installed with nuget to the project, I modified CMakeLists.txt from [the solution given by squareskittles](https://stackoverflow.com/a/56093754)  to the following: 
- CMakeLists.txt

cmake_minimum_required(VERSION 3.5)

project(kneebo C)

find_package(ament_cmake REQUIRED) find_package(ros2cs_common REQUIRED) find_package(ros2cs_core REQUIRED) find_package(std_msgs REQUIRED) find_package(sensor_msgs REQUIRED) find_package(builtin_interfaces REQUIRED) find_package(dotnet_cmake_module REQUIRED) find_package(rosidl_generator_cs REQUIRED)

set(CSHARP_TARGET_FRAMEWORK "netcoreapp3.1") find_package(DotNETExtra REQUIRED)

set(_assemblies_dep_dlls ${ros2cs_common_ASSEMBLIES_DLL} ${ros2cs_core_ASSEMBLIES_DLL} ${std_msgs_ASSEMBLIES_DLL} ${sensor_msgs_ASSEMBLIES_DLL} ${builtin_interfaces_ASSEMBLIES_DLL} )

add_dotnet_executable(kneebo_talker Program.cs INCLUDE_DLLS ${_assemblies_dep_dlls} )

set_property(TARGET kneebo_talker PROPERTY VS_PACKAGE_REFERENCES "System.IO.Ports_5.0.1" )

ament_package()


- `package.xml` is exactly the same as [ROS2Talker and ROS2Listener examples](https://github.com/RobotecAI/ros2cs/blob/master/src/ros2cs/ros2cs_examples/package.xml).

My Compilation Steps

mkdir build cd build cmake .. cmake --build . --target INSTALL --config Release


But the following message will appear: a.

C:\dev\KneeBO\Program.cs(12,41): error CS0103: name 'SerialPort' does not exist in the current content [C:\dev\KneeBO\build\kneebo_talker\kneebo_talker_dotnetcore. csproj] [C:\dev\KneeBO\build\kneebo_talker.vcxproj]



I am new to dotnet compilation, and I look forward to your help. Many thanks.
samiam567 commented 2 years ago

Did you ever find a solution to this? I am having a similar issue.

sk413025 commented 2 years ago

The path name may be incorrect, but the overall steps are as follows.


Note that a runtime error will pop up stating that SerialPort cannot be found and therefore cannot be run.


My workaround: Running dotnet add package System.IO.Ports again in the build/KneeBO directory, and then dotnet run, it will run successfully.