我有一个非常小的代码可以显示可用的 COM 端口。
我的问题是:
有没有一种简单的方法可以让程序在托盘中运行,并且仅在新的 COM 端口可用时弹出,并且是否可以添加您可以在设备管理器中看到的 COM 端口的名称 ec“USB serial港口”?
我经常添加/删除一个 USB->RS232 转换器,这让我很头疼,因为我必须进入设备管理器才能看到它分配给了哪个 COM 端口。每次都不一样
也许已经有一个小应用程序可以做到这一点,但我还没有在谷歌上找到它
using System;
using System.Windows.Forms;
using System.IO.Ports;
namespace Available_COMports
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//show list of valid com ports
foreach (string s in SerialPort.GetPortNames())
{
listBox1.Items.Add(s);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
最佳答案
public static void Main()
{
// Get a list of serial port names.
string[] ports = SerialPort.GetPortNames();
Console.WriteLine("The following serial ports were found:");
// Display each port name to the console.
foreach(string port in ports)
{
Console.WriteLine(port);
}
Console.ReadLine();
}
关于c# - 列出可用的 COM 端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5445847/