【发布时间】:2023-02-22 21:23:01
【问题描述】:
我正在使用 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 类型的引用。
有谁知道我可能错过了什么?
【问题讨论】:
-
您要构建什么目标运行时? (.NET 版本?)
-
嗨@K。 Jensen,很高兴知道您找到了解决此问题的方法!请考虑接受它作为答案,将其状态更改为已回答。见can I answer my own question..,提醒一下:)
标签: c# visual-studio reference