简体   繁体   English

通过使用C#更改端口值来获取特定的串行端口信息

[英]get particular serialport information by changing the port value using C#

I have written the following code in form load event. 我在表单加载事件中编写了以下代码。 When the form is getting loaded the available serialports are added into the combobox. 加载表单时,可用的串行端口将添加到组合框中。

 String[] ports = SerialPort.GetPortNames();    
private void Form1_Load(object sender, EventArgs e)
    {
        _serialPort = new SerialPort();
        foreach (string port in ports)
          {
            cbox.Items.Add(port);
          }
    }

In the next step,in combobox selectedindex_changed event,when am changing the port, the port details are updated in multiline textbox. 下一步,在组合框selectedindex_changed事件中,更改端口时,将在多行文本框中更新端口详细信息。

 private void cbox_SelectedIndexChanged(object sender, EventArgs e)
      {
          using (var searcher = new ManagementObjectSearcher("SELECT * FROM WIN32_SerialPort"))
          {
              var prts = searcher.Get().Cast<ManagementBaseObject>().ToList();
              var tList = (from n in ports
                           join p in prts on n equals p["DeviceID"].ToString()
                           select n + " - " + p["Caption"]).ToList();
              foreach (string s in tList)
              {
               mtxt.AppendText(s);  // multiline textbox
              }
          }
      }

For example: In this program, ports are added into combo box from (COM3 to comX). 例如:在此程序中,端口从(COM3到comX)添加到组合框中。 But my problem is whatever the port i select, it returns only the details of 1st port from the combobox (ie it return only the details of COM3).Pls any one help me for getting details of port which i select in combobox. 但是我的问题是我选择的端口是什么,它仅从组合框中返回第一个端口的详细信息(即,它仅返回COM3的详细信息)。请任何人帮助我获取我在组合框中选择的端口的详细信息。

Follow discussion over here 在这里关注讨论

http://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/c236cac4-a954-4a70-882d-bc20e2cc6e81 http://social.msdn.microsoft.com/Forums/zh-CN/winformsdesigner/thread/c236cac4-a954-4a70-882d-bc20e2cc6e81

Read Following in that 阅读以下内容

We can't get the information through the SerialPort type. 我们无法通过SerialPort类型获取信息。 I don't know why you need this info in your application. 我不知道为什么您需要在应用程序中使用此信息。 However, there's a solved thread with the same question as you. 但是,有一个与您相同的问题已解决的线程。 You can check out the code there, and see if it can help you. 您可以在此处查看代码,看看它是否可以为您提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM