skip to Main Content

I have some experience in creating a C# application using .NET framework. However, I am stumped how to get this one solved.

Ok, spent the last hour trying to get the simplest of applications running using C# and a .NET 8 console application.

My goal: read text from a serial USB connection from an ESP8266 serial monitor.

I have seen plenty of examples doing this:

using System.IO.Ports;
using System.IO;

SerialPort port = new SerialPort();

This utterly fails on my system. It seams that something is missing: the System.IO.Ports.

Determining projects to restore...
All projects are up-to-date for restore.

C:sourcesesp8266USBComPortReader6bUSBComPortReader6bProgram.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:sourcesesp8266USBComPortReader6bUSBComPortReader6bUSBComPortReader6b.csproj]
C:sourcesesp8266USBComPortReader6bUSBComPortReader6bProgram.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:sourcesesp8266USBComPortReader6bUSBComPortReader6bUSBComPortReader6b.csproj]

Build FAILED.

C:sourcesesp8266USBComPortReader6bUSBComPortReader6bProgram.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:sourcesesp8266USBComPortReader6bUSBComPortReader6bUSBComPortReader6b.csproj]
C:sourcesesp8266USBComPortReader6bUSBComPortReader6bProgram.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:sourcesesp8266USBComPortReader6bUSBComPortReader6bUSBComPortReader6b.csproj]
    0 Warning(s)
    2 Error(s)

Time Elapsed 00:00:01.50

Looking in the project explorer I see these:

enter image description here

I searched everywhere and there was the suggestion that I should install a NuGet package for this:

enter image description here

Results in:


C:sourcesesp8266USBComPortReader6bUSBComPortReader6b>dotnet add package System.IO.Ports --version 8.0.0
  Determining projects to restore...
  Writing C:Userse2692AppDataLocalTemptmpb01oaz.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:sourcesesp8266USBComPortReader6bUSBComPortReader6bUSBComPortReader6b.csproj'.
info : Restoring packages for C:sourcesesp8266USBComPortReader6bUSBComPortReader6bUSBComPortReader6b.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:sourcesesp8266USBComPortReader6bUSBComPortReader6bUSBComPortReader6b.csproj'.

Some people suggest that the System.IO.Ports is not supported by .NET 8 (or any version at all). However, Microsoft has a manual page

enter image description here

Running on Windows 11. Tried Jetbrains Rider and Visual Studio 2022 Community edition.

Executing the dotnet --version in the project folder:

C:sourcesesp8266USBComPortReader6bUSBComPortReader6b>dotnet --version
8.0.402

Outside:

C:>dotnet --version
8.0.402

C:>dotnet --list-sdks
6.0.425 [C:Program Filesdotnetsdk]
8.0.400 [C:Program Filesdotnetsdk]
8.0.402 [C:Program Filesdotnetsdk]

More info on my project:

enter image description here

Solution properties:

# Visual Studio Version 17
VisualStudioVersion = 17.11.35312.102
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "USBComPortReader6b", "USBComPortReader6bUSBComPortReader6b.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

The .csproj file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

</Project>

To say I am frustrated is quite the understatement. I have dealt with my share of these platform related issues but I am completely at a loss how to solve this one.

ANY help would be greatly appreciated!

2

Answers


  1. Chosen as BEST ANSWER

    Thank you everybody for you support and tips!

    So I added the package reference as @Basheer Jarrah suggested. That didn't help.

    THen I followed the lead from @Npras about the NU1101 error (how to resolve "Unable to find package" nuget error). And indeed my package sources list was empty. After adding the indicated source and restored the project the errors finally went away.

    Source list (%appdata%NuGetNuGet.Config) is now:

    <?xml version="1.0"?>
    <configuration>
      <packageSources>
        <add key="NuGet official package source" value="https://api.nuget.org/v3/index.json" />
      </packageSources>
    </configuration>
    

    After this I could compile without errors.

      dotnet restore
    

    Conclusion: after a fresh install of Visual Studio Community Edition 2022 and fresh install of dotnet 6 of dotnet 8, there was no source list for the online packages. This must be fixed manually.


  2. Add System.IO.Ports to .csproj file as the following:

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net8.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="System.IO.Ports" Version="8.0.0" />
      </ItemGroup>
    </Project>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search