The type name 'SerialPort' could not be found in the namespace 'System.IO.Ports'
Asked Answered
S

3

6

I am using Visual Studio Community 2019 to write a new serial port downloader program. I am relatively new to C# but have had to maintain a large-ish c# application that also uses the serial port to configure an embedded device.

Problematic (new) code is below. The "System.IO.Ports" using statement has no effect (is greyed out) and the issue is that the compiler has no clue what a "SerialPort" is (see the error after the code).

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.Ports;
using System.Drawing;

namespace PangeaUpdater
{
    class Download
    {
        const byte STX_CHAR = (byte)'\x2';
        const byte ETX_CHAR = (byte)'\x3';
        const byte DLE_CHAR = (byte)'\x10';

        const int MAX_DATA_PACKET_LEN = 128;
        private BinaryReader pkgStream = null;

        public SerialPort serialPort;

        private void Create(String comPort, String baudrate,         
            System.Windows.Forms.RichTextBox msgListBox,
            System.Windows.Forms.ProgressBar progressBar)
        {
            //Lots of stuff should be configurable but is not (yet)
            serialPort = new SerialPort(comPort,
                                    Convert.ToInt32(baudrate),
                                    Parity.None,
                                    8,
                                    StopBits.One);

            serialPort.Handshake = Handshake.None;

            serialPort.Open();
            serialPort.ReadTimeout = 1000;
            progBar = progressBar;
            messages = msgListBox;
         }

        public Download(String comPort,
                String baudrate,
                //String Product,
                System.Windows.Forms.RichTextBox msgListBox,
                System.Windows.Forms.ProgressBar progressBar)
        {
            Create(comPort, baudrate, /*Product,*/ msgListBox, progressBar);
        }

    }
}

C:...\Download.cs(20,16,20,26): error CS1069: 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=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' Consider adding a reference to that assembly.

I have checked against the app that I maintain and which compiles and runs fine. The old app has a set of References, but the new app has just a set of dependencies, but I gather these are very similar or the same?

I suspect adding a reference is what is required but I have no clue what is needed. When I try to add a Reference in the new app I see nothing except for COM assemblies. There are loads but there is no clue as to which might be needed for serial port stuff. The old config app does not give any clue as the where it finds a reference for the SerialPort type.

Does anyone have a clue what I may have missed?

Spectroheliograph answered 8/8, 2022 at 16:33 Comment(2)
What target runtime are you building for? (.NET Version?)Cathodoluminescence
Hi @K. Jensen , glad to know you've found the solution to resolve this issue! Please consider accepting it as the answer to change its status to Answered. See can I answer my own question.., Just a reminder :)Discretional
S
5

I found the problem. It was that I had inadvertently chosen a framework "core" project as the template. As a newbie to Visual Studio C#, I don't understand why there are so many templates. It seems to be a confusing array of project types with no explanation of the subtle differences between them.

Anyway, thanks for the suggestions. I think that I focussed to closely on the actual error message and not enough on the other symptoms of this problem, like most of the components in the toolbox being missing!

Spectroheliograph answered 31/8, 2022 at 8:37 Comment(1)
I also find the template selection menu very unintuive, it is not clear what the difference between a .NET Core and a traditional .NET template is.Heartwhole
H
9

You probably need to add a reference to System.ComponentModel.Component. See: https://learn.microsoft.com/en-us/dotnet/api/system.io.ports.serialport?view=netframework-4.8

If its a .NET 6 application you probably need VS2022 and a NuGet package: https://www.nuget.org/packages/System.IO.Ports/

(.NET 6 is not supported in VS2019)

Heartwhole answered 8/8, 2022 at 16:38 Comment(1)
Adding a NuGet package worked for me for .NET 7Cressida
S
5

I found the problem. It was that I had inadvertently chosen a framework "core" project as the template. As a newbie to Visual Studio C#, I don't understand why there are so many templates. It seems to be a confusing array of project types with no explanation of the subtle differences between them.

Anyway, thanks for the suggestions. I think that I focussed to closely on the actual error message and not enough on the other symptoms of this problem, like most of the components in the toolbox being missing!

Spectroheliograph answered 31/8, 2022 at 8:37 Comment(1)
I also find the template selection menu very unintuive, it is not clear what the difference between a .NET Core and a traditional .NET template is.Heartwhole
I
0

To solve the serial port problem in Visual studio 2019 do the following:

  1. while creating your project choose, the following windows "Configure your new project" will apear.

  2. In the "Configure your new project" windows you will find Framework Combobox.

  3. From the Framework Combox choose".Net Framework 2.0" or any ".Net Framework 4.0" or 4.5 --->4.7.2.

  4. After doing this , you will find the serial port component in you project tool box.

  5. List item

As Shown In the Picture

Idelson answered 2/5, 2024 at 7:15 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.