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

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

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

        C#串口检查设备是否已连接

        时间:2023-11-10
      1. <tfoot id='jA5JR'></tfoot>

          • <bdo id='jA5JR'></bdo><ul id='jA5JR'></ul>

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

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

                  <legend id='jA5JR'><style id='jA5JR'><dir id='jA5JR'><q id='jA5JR'></q></dir></style></legend>
                    <tbody id='jA5JR'></tbody>
                  本文介绍了C#串口检查设备是否已连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我最近一直在使用 SerialPort 类.目前我正在尝试找出检查设备是否连接到我的应用程序使用的通信端口的正确方法.是否有任何正确的方法来检查设备是否连接到通信端口?我目前的方法如下:

                  I've been working with the SerialPort class a lot lately. Currently I'm trying to figure out the proper way to check if a device is connected to the comm port my application uses. Is there any proper way to check if a device is connected to the comm port? My current method is as follows:

                              while (isReading == true)
                              {
                                  try
                                  {
                                      received += serialPort.ReadExisting();
                  
                                      if (received.Contains('>'))
                                          isReading = false;
                                  }
                                  catch (Exception e)
                                  {
                  
                                  }
                                  if (tick == 10000)
                                      if (received == "")
                                      {
                                          Console.WriteLine("No Data Received. Device isn't connected.");
                                          isReading = false;
                                      }
                                  tick++;
                              }
                              Console.WriteLine(received);
                  

                  它有效,但我觉得它有点老套和不可靠.如果需要,我可以保留它,但如果有合适的替代方法,我会喜欢它.

                  It works but I feel it's a little hacky and unreliable. I can keep it if need be but I'd like it if there's a proper alternative to doing this.

                  我实际上必须将刻度值设置为大约 10,000 以确保它是可靠的.否则我有时无法接收数据.即使将其设置为 1000 或 5000 也不可靠.即使这样,也不能保证在多台机器上可靠.

                  I actually have to set the tick value to about 10,000 to ensure it's reliable. Otherwise I fail to receive data on occasion. Even setting it to 1000 or 5000 is unreliable. Even then, it's not guaranteed to be reliable across multiple machines.

                  推荐答案

                  我也需要使用串口,相信我他们很痛苦.我检查设备是否连接的方法通常围绕发出轮询命令.虽然您的方法可能有效,但当事件足够时,我不情愿使用 while 循环.

                  I too need to work with serial ports, and believe me they are a pain. My method to check if a device is connected usually revolves around issuing a polling command. While you method may work, I cant help but be reluctant to use a while loop when an event will suffice.

                  .NET 串行端口类提供了一些有用的事件:

                  The .NET serial port class offers some useful events:

                  Serial.DataReceived Serial.ErrorReceivedSerial.Write

                  通常我会在指定的时间间隔发出轮询命令以确保设备已连接.当设备响应时,它将触发 DataReceived 事件,您可以相应地处理响应(以及任何其他必要的数据).这可以与简单的 Timer 或递增变量结合使用,以对响应进行计时.请注意,您需要适当地设置 ReadTimeoutWriteTimeout 值.这与 ReadExisting 和/或 ReadLine 方法一起可能在您的 DataReceived 事件处理程序中使用.

                  Usually I would issue a polling command at a specified interval to ensure the device is connected. When the device responds it will fire the DataReceived event, and you can deal with the response accordingly (along with any other neccessary data). This can be used in conjunction with a simple Timer or incremented variable to time the response. Note you will need to set the ReadTimeout and WriteTimeout value appropriately. This, along with the ReadExisting and/or ReadLine method may be of use in your DataReceived event handler.

                  总而言之,(在伪代码中)

                  So, to summarize, (in pseudo code)

                  Send Polling command, Start Timer
                  Timer to CountDown for a specified time
                  If Timer fires, then assume no response
                  If DataRecieved fires (and expected response) assume connection
                  (of course handle any specific Exceptions (e.g TimeOutException, InvalidOperationException)
                  

                  这篇关于C#串口检查设备是否已连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:指定的参数超出了有效值的范围.参数名称:size &amp;串口通讯 下一篇:SerialPort 编码 - 我如何获得 8 位 ASCII?

                  相关文章

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

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