<legend id='bR4GD'><style id='bR4GD'><dir id='bR4GD'><q id='bR4GD'></q></dir></style></legend>

    • <bdo id='bR4GD'></bdo><ul id='bR4GD'></ul>
      <i id='bR4GD'><tr id='bR4GD'><dt id='bR4GD'><q id='bR4GD'><span id='bR4GD'><b id='bR4GD'><form id='bR4GD'><ins id='bR4GD'></ins><ul id='bR4GD'></ul><sub id='bR4GD'></sub></form><legend id='bR4GD'></legend><bdo id='bR4GD'><pre id='bR4GD'><center id='bR4GD'></center></pre></bdo></b><th id='bR4GD'></th></span></q></dt></tr></i><div id='bR4GD'><tfoot id='bR4GD'></tfoot><dl id='bR4GD'><fieldset id='bR4GD'></fieldset></dl></div>

      1. <small id='bR4GD'></small><noframes id='bR4GD'>

        <tfoot id='bR4GD'></tfoot>

        如何知道连接了哪个串口外部设备并从该设备读取数据?

        How to know on which serial port external device is connected and read data from that device?(如何知道连接了哪个串口外部设备并从该设备读取数据?)

            <small id='2rMhh'></small><noframes id='2rMhh'>

              <bdo id='2rMhh'></bdo><ul id='2rMhh'></ul>
              <i id='2rMhh'><tr id='2rMhh'><dt id='2rMhh'><q id='2rMhh'><span id='2rMhh'><b id='2rMhh'><form id='2rMhh'><ins id='2rMhh'></ins><ul id='2rMhh'></ul><sub id='2rMhh'></sub></form><legend id='2rMhh'></legend><bdo id='2rMhh'><pre id='2rMhh'><center id='2rMhh'></center></pre></bdo></b><th id='2rMhh'></th></span></q></dt></tr></i><div id='2rMhh'><tfoot id='2rMhh'></tfoot><dl id='2rMhh'><fieldset id='2rMhh'></fieldset></dl></div>
                <tbody id='2rMhh'></tbody>
                <legend id='2rMhh'><style id='2rMhh'><dir id='2rMhh'><q id='2rMhh'></q></dir></style></legend>
                  <tfoot id='2rMhh'></tfoot>
                  本文介绍了如何知道连接了哪个串口外部设备并从该设备读取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我连接了 1 个设备,它生成了一些随机数,我想通过我的 Windows 应用程序读取该数字.

                  I have attached 1 device which generated some random number and i want to read that number through my Windows application.

                  我正在使用此代码:

                  public static void Main()
                  {
                      SerialPort mySerialPort = new SerialPort("COM19");
                  
                      mySerialPort.BaudRate = 115200;
                      mySerialPort.Parity = Parity.None;
                      mySerialPort.StopBits = StopBits.One;
                      mySerialPort.DataBits = 8;
                      mySerialPort.Handshake = Handshake.None;
                  
                      mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
                  
                      mySerialPort.Open();
                  
                      Console.WriteLine("Press any key to continue...");
                      Console.WriteLine();
                      Console.ReadKey();
                      mySerialPort.Close();
                  }
                  
                  private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
                  {
                      SerialPort sp = (SerialPort)sender;
                      string indata = sp.ReadExisting();
                      Debug.Print("Data Received:");
                      Debug.Print(indata);
                  }
                  

                  使用此代码,我成功获取数据.

                  With this code I am successfully getting data.

                  但这里的问题是我已经硬编码了来自设备管理器的 com 端口(Com19) 我已经检查了我的设备连接到哪个端口,所以我已经硬编码了,但我不想要这样做.

                  But problem here is I have hardcoded my com port(Com19) that is from device manager I have checked that to which port my device is connected so I have hardcoded that but I don't want to do this.

                  相反,我将给用户 1 下拉列表,其中用户将只能看到用户设备连接到的端口.因此,当从这个连接的设备获取数据时,我将使用该用户选择的下拉端口.

                  Instead I will give user 1 dropdown in which user will see only that port to which user device is connected. So when fetching data from this connected device I will use that user selected dropdown port.

                  我对 Windows 应用程序非常陌生,从未做过任何与串口相关的事情.

                  I am very much new to windows application and never done anything related to serial port.

                  推荐答案

                  当我将 arduino 连接到 com 端口时.我已经使用了这段代码(假设 arduino 返回了带有来自 Arduino 的信息"的文本):

                  When I have arduino connected to com port. I have use this code (assuming that arduino returned text with "Info from Arduino" inside):

                  SerialPort currentPort; // global variables
                  bool ArduinoPortFound = false;
                  

                  ...

                          try
                          {
                              string[] ports = SerialPort.GetPortNames();
                              foreach (string port in ports)
                              {
                                  currentPort = new SerialPort(port, 9600);
                                  if (ArduinoDetected())
                                  {
                                      ArduinoPortFound = true;
                                      break;
                                  }
                                  else
                                  {
                                      ArduinoPortFound = false;
                                  }
                              }
                          }
                          catch { }
                  

                  ......

                          private bool ArduinoDetected()
                      {
                          try
                          {
                              currentPort.Open();
                              System.Threading.Thread.Sleep(1000); 
                  
                              string returnMessage = currentPort.ReadLine();
                              currentPort.Close();
                  
                              if (returnMessage.Contains("Info from Arduino"))
                              {
                                  return true;
                              }
                              else
                              {
                                  return false;
                              }
                          }
                          catch (Exception e)
                          {
                              return false;
                          }
                      }
                  

                  更新:或者你也可以使用 Windows 管理规范 (WMI)

                  UPDATE: Or you can also use Windows Management Instrumentation (WMI)

                  这是关于它的文章:如何通过友好名称以编程方式查找 COM 端口

                  这篇关于如何知道连接了哪个串口外部设备并从该设备读取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                  Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                  How to store delegates in a List(如何将代表存储在列表中)
                  How delegates work (in the background)?(代表如何工作(在后台)?)
                  C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                  Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)
                  <tfoot id='vDLtv'></tfoot>

                  1. <legend id='vDLtv'><style id='vDLtv'><dir id='vDLtv'><q id='vDLtv'></q></dir></style></legend>
                        <tbody id='vDLtv'></tbody>

                      <small id='vDLtv'></small><noframes id='vDLtv'>

                      <i id='vDLtv'><tr id='vDLtv'><dt id='vDLtv'><q id='vDLtv'><span id='vDLtv'><b id='vDLtv'><form id='vDLtv'><ins id='vDLtv'></ins><ul id='vDLtv'></ul><sub id='vDLtv'></sub></form><legend id='vDLtv'></legend><bdo id='vDLtv'><pre id='vDLtv'><center id='vDLtv'></center></pre></bdo></b><th id='vDLtv'></th></span></q></dt></tr></i><div id='vDLtv'><tfoot id='vDLtv'></tfoot><dl id='vDLtv'><fieldset id='vDLtv'></fieldset></dl></div>
                      • <bdo id='vDLtv'></bdo><ul id='vDLtv'></ul>