动态获取本机可用串口,

ltj1682 2008-12-27 11:49:04
其中Port_Option_tmi 一个[ToolStripMenuItem],相当于文件-》打开的情况。
我要动态获取本机可用串口,
并添加为Port_Option_tmi的子菜单,只能单选。

C# code

#region 选择端口

private void GetSerialPort() //获取串口列表
{
RegistryKey keyCom = Registry.LocalMachine.OpenSubKey("Hardware\\DeviceMap\\SerialComm");
if (keyCom != null)
{
string[] sSubKeys = keyCom.GetValueNames();
foreach (string sName in sSubKeys)
{
string sValue = (string)keyCom.GetValue(sName);
Port_Option_tmi.DropDownItems.Add(sValue, null, new EventHandler(NoPort_Click));
}
}
}

private void NoPort_Click(object sender, EventArgs e)
{
ToolStripMenuItem mi = (ToolStripMenuItem)sender;
for (int i = 0; i < Port_Option_tmi.DropDownItems.Count; i++)
{
//在这里写除去所有子菜单Checked代码
}
mi.Checked=true;

port = mi.Text;
Port_Main_ss.Text = port;
}
#endregion



...全文
577 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
whn1119 2008-12-28
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 Sader_L 的回复:]
C# code
private void GetSerialPort() //获取串口列表
{
RegistryKey keyCom = Registry.LocalMachine.OpenSubKey("Hardware\\DeviceMap\\SerialComm");
if (keyCom != null)
{
string[] sSubKeys = keyCom.GetValueNames();
foreach (string sName in sSubKeys)
{

[/Quote]

菜鸟学习,请问需要引用那些指令集?
jxxx2967 2008-12-27
  • 打赏
  • 举报
回复
问题是什么?
much0726 2008-12-27
  • 打赏
  • 举报
回复
没接触过,UP
饺子87 2008-12-27
  • 打赏
  • 举报
回复
        private void GetSerialPort()   //获取串口列表                                             
{
RegistryKey keyCom = Registry.LocalMachine.OpenSubKey("Hardware\\DeviceMap\\SerialComm");
if (keyCom != null)
{
string[] sSubKeys = keyCom.GetValueNames();
foreach (string sName in sSubKeys)
{
string sValue = (string)keyCom.GetValue(sName);
SerialToolStripMenuItem.DropDownItems.Add(sValue, null, new EventHandler(NoPorts_Click));
}
}

for (int i = 0; i < SerialToolStripMenuItem.DropDownItems.Count; i++)
{
if (SerialToolStripMenuItem.DropDownItems[i].Text == VariableLib.portName)
{
((ToolStripMenuItem)(SerialToolStripMenuItem.DropDownItems[i])).Checked = true;
SerialToolStripStatusLabel.Text = ((ToolStripMenuItem)(SerialToolStripMenuItem.DropDownItems[i])).Text;
}
}
}
优途科技 2008-12-27
  • 打赏
  • 举报
回复
顶一下。
ICanUseThisID 2008-12-27
  • 打赏
  • 举报
回复
获取本机串口有更简便方法:

string[] ports = System.IO.Ports.SerialPort.GetPortNames();

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧