我有一些使用 .NET 框架创建 C# 应用程序的经验。然而,我很困惑如何解决这个问题。
好的,花了最后一个小时尝试使用 C# 和 .NET 8 控制台应用程序运行最简单的应用程序。
我的目标:从 ESP8266 串行监视器的串行 USB 连接读取文本。
我见过很多这样做的例子:
using System.IO.Ports;
using System.IO;
SerialPort port = new SerialPort();
这在我的系统上完全失败了。似乎缺少了一些东西:System.IO.Ports。
Determining projects to restore...
All projects are up-to-date for restore.
C:\sources\esp8266\USBComPortReader6b\USBComPortReader6b\Program.cs(7,1): error CS1069: The type name 'SerialPort' could not be found in the namespace 'System.IO.Ports'. This type has been forwarded to assembly 'Syst
em.IO.Ports, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' Consider adding a reference to that assembly. [C:\sources\esp8266\USBComPortReader6b\USBComPortReader6b\USBComPortReader6b.csproj]
C:\sources\esp8266\USBComPortReader6b\USBComPortReader6b\Program.cs(7,29): error CS1069: The type name 'SerialPort' could not be found in the namespace 'System.IO.Ports'. This type has been forwarded to assembly 'Sys
tem.IO.Ports, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' Consider adding a reference to that assembly. [C:\sources\esp8266\USBComPortReader6b\USBComPortReader6b\USBComPortReader6b.csproj]
Build FAILED.
C:\sources\esp8266\USBComPortReader6b\USBComPortReader6b\Program.cs(7,1): error CS1069: The type name 'SerialPort' could not be found in the namespace 'System.IO.Ports'. This type has been forwarded to assembly 'Syst
em.IO.Ports, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' Consider adding a reference to that assembly. [C:\sources\esp8266\USBComPortReader6b\USBComPortReader6b\USBComPortReader6b.csproj]
C:\sources\esp8266\USBComPortReader6b\USBComPortReader6b\Program.cs(7,29): error CS1069: The type name 'SerialPort' could not be found in the namespace 'System.IO.Ports'. This type has been forwarded to assembly 'Sys
tem.IO.Ports, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' Consider adding a reference to that assembly. [C:\sources\esp8266\USBComPortReader6b\USBComPortReader6b\USBComPortReader6b.csproj]
0 Warning(s)
2 Error(s)
Time Elapsed 00:00:01.50
在项目资源管理器中我看到这些:
我到处搜索,有人建议我应该为此安装一个 NuGet 包:
Results in:
C:\sources\esp8266\USBComPortReader6b\USBComPortReader6b>dotnet add package System.IO.Ports --version 8.0.0
Determining projects to restore...
Writing C:\Users\e2692\AppData\Local\Temp\tmpb01oaz.tmp
info : X.509 certificate chain validation will use the default trust store selected by .NET for code signing.
info : X.509 certificate chain validation will use the default trust store selected by .NET for timestamping.
info : Adding PackageReference for package 'System.IO.Ports' into project 'C:\sources\esp8266\USBComPortReader6b\USBComPortReader6b\USBComPortReader6b.csproj'.
info : Restoring packages for C:\sources\esp8266\USBComPortReader6b\USBComPortReader6b\USBComPortReader6b.csproj...
error: NU1101: Unable to find package System.IO.Ports. No packages exist with this id in source(s): Microsoft Visual Studio Offline Packages
error: Package 'System.IO.Ports' is incompatible with 'all' frameworks in project 'C:\sources\esp8266\USBComPortReader6b\USBComPortReader6b\USBComPortReader6b.csproj'.
有些人建议 .NET 8(或任何版本)不支持
System.IO.Ports
。但是,Microsoft 有一个手册页
在 Windows 11 上运行。尝试过 Jetbrains Rider 和 Visual Studio 2022 社区版。
执行项目文件夹中的
dotnet --version
:
C:\sources\esp8266\USBComPortReader6b\USBComPortReader6b>dotnet --version
8.0.402
外面:
C:\>dotnet --version
8.0.402
C:\>dotnet --list-sdks
6.0.425 [C:\Program Files\dotnet\sdk]
8.0.400 [C:\Program Files\dotnet\sdk]
8.0.402 [C:\Program Files\dotnet\sdk]
有关我的项目的更多信息:
溶液性质:
# Visual Studio Version 17
VisualStudioVersion = 17.11.35312.102
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "USBComPortReader6b", "USBComPortReader6b\USBComPortReader6b.csproj", "{92218195-33A1-48E3-B5DB-19B44769E322}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{92218195-33A1-48E3-B5DB-19B44769E322}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{92218195-33A1-48E3-B5DB-19B44769E322}.Debug|Any CPU.Build.0 = Debug|Any CPU
{92218195-33A1-48E3-B5DB-19B44769E322}.Release|Any CPU.ActiveCfg = Release|Any CPU
{92218195-33A1-48E3-B5DB-19B44769E322}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0298EA09-525B-42F7-954D-EC886703AEAC}
EndGlobalSection
EndGlobal
.csproj
文件:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
说我感到沮丧是轻描淡写的。我已经处理了这些与平台相关的问题,但我完全不知道如何解决这个问题。
任何帮助将不胜感激!
错误消息指出:
源中不存在具有此 ID 的包:Microsoft Visual Studio 离线包。
要添加 System.IO.Ports NuGet 包请指定源:
dotnet add package "System.IO.Ports" --version 8.0.0 --source "https://api.nuget.org/v3/index.json"
注意:如果使用 Visual Studio,请在 Visual Studio 菜单中选择
如果
https://api.nuget.org/v3/index.json
未列为来源,请添加它。 (名称:nuget.org;来源:https://api.nuget.org/v3/index.json)。
此外,请确保将 Package Management 设置为 PackageReference