[英]The type name 'SerialPort' could not be found in the namespace 'System.IO.Ports'
我正在使用 Visual Studio Community 2019 編寫一個新的串口下載程序。 我對 C# 比較陌生,但必須維護一個大型 c# 應用程序,該應用程序也使用串行端口配置嵌入式設備。
有問題的(新)代碼如下。 “System.IO.Ports” using 語句無效(灰顯),問題是編譯器不知道“SerialPort”是什么(參見代碼后面的錯誤)。
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):錯誤 CS1069:在命名空間“System.IO.Ports”中找不到類型名稱“SerialPort”。 此類型已轉發到程序集 'System.IO.Ports, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' 考慮添加對該程序集的引用。
我已經檢查了我維護的應用程序,該應用程序編譯和運行良好。 舊應用程序有一組引用,但新應用程序只有一組依賴項,但我認為這些非常相似還是相同?
我懷疑添加參考是必需的,但我不知道需要什么。 當我嘗試在新應用程序中添加參考時,除了 COM 程序集外,我什么也看不到。 有負載,但不知道串行端口可能需要哪個負載。 舊的配置應用程序沒有提供任何線索,因為它找到了 SerialPort 類型的引用。
有誰知道我可能錯過了什么?
您可能需要添加對 System.ComponentModel.Component 的引用。 請參閱: https://docs.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/
(VS2019 不支持 .NET 6)
我發現了問題。 那是我無意中選擇了一個框架“核心”項目作為模板。 作為 Visual Studio C# 的新手,我不明白為什么會有這么多模板。 這似乎是一系列令人困惑的項目類型,沒有解釋它們之間的細微差別。
無論如何,感謝您的建議。 我認為我將重點放在實際的錯誤消息上,而對這個問題的其他症狀還不夠,比如工具箱中的大多數組件都丟失了!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.