skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

    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!


  2. 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)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search