Godot 4.4 C#无法使用System.IO.Ports的SerialPort问题求助
环境
- Ubuntu 24.04
- Godot 4.4
问题描述
尝试在Godot中实现串口通信,插件和扩展尝试失败后改用C#开发,但编辑器提示System.IO.Ports命名空间中不存在SerialPort类型。
代码示例
using Godot; using System; using System.IO.Ports; using System.Threading; public partial class ReadFromSerial : Node { static SerialPort port; public override void _Ready() { port = new SerialPort(); } }
错误信息
The type name 'SerialPort' could not be found in the namespace 'System.IO.Ports'. This type has been forwarded to assembly 'System.IO.Ports, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
Consider adding a reference to that assembly.
已尝试操作
- 在VS Code解决方案资源管理器中右键点击依赖项添加引用,无弹出窗口;
- 在Windows机器测试,出现相同问题。
解决方法
方法1:手动修改.csproj文件
Godot的C#项目依赖通过.csproj文件管理,直接编辑添加引用:
- 找到项目根目录下的
.csproj文件(如YourProjectName.csproj); - 打开文件,在
<ItemGroup>节点内添加以下内容:
<PackageReference Include="System.IO.Ports" Version="8.0.0" />
- 保存文件,Godot会自动重新加载项目并恢复依赖。
方法2:使用Godot内置NuGet包管理器
Godot 4.4自带NuGet支持,通过编辑器操作添加依赖:
- 打开Godot编辑器,点击顶部菜单栏
Project->Manage NuGet Packages...; - 在弹窗的
Browse标签页搜索System.IO.Ports; - 选择与项目目标.NET版本兼容的版本(Godot 4.4默认支持.NET 6及以上,推荐选8.0.0),点击
Install; - 安装完成后重启编辑器或重新加载项目,错误即可消除。
补充说明
- 确保
System.IO.Ports包版本与项目目标.NET版本兼容; - 以上方法同时适用于Ubuntu和Windows平台。
内容的提问来源于stack exchange,提问作者Jacques Duflos
相关产品推荐
相关产品推荐

