• <legend id='o9aZP'><style id='o9aZP'><dir id='o9aZP'><q id='o9aZP'></q></dir></style></legend>

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

      <tfoot id='o9aZP'></tfoot>

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

      1. C# 异步串口读取

        时间:2023-11-10
          <bdo id='pu69b'></bdo><ul id='pu69b'></ul>

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

            <tbody id='pu69b'></tbody>
          <legend id='pu69b'><style id='pu69b'><dir id='pu69b'><q id='pu69b'></q></dir></style></legend>
            <tfoot id='pu69b'></tfoot>

                • <i id='pu69b'><tr id='pu69b'><dt id='pu69b'><q id='pu69b'><span id='pu69b'><b id='pu69b'><form id='pu69b'><ins id='pu69b'></ins><ul id='pu69b'></ul><sub id='pu69b'></sub></form><legend id='pu69b'></legend><bdo id='pu69b'><pre id='pu69b'><center id='pu69b'></center></pre></bdo></b><th id='pu69b'></th></span></q></dt></tr></i><div id='pu69b'><tfoot id='pu69b'></tfoot><dl id='pu69b'><fieldset id='pu69b'></fieldset></dl></div>
                  本文介绍了C# 异步串口读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个类,它使用 C# 中的 DataReceived 事件处理程序从串行端口读取.当我收到数据时,我知道标头将有 5 个字节,所以我不想对数据做任何事情,直到我至少有这个.我当前的代码如下:

                  I have a class which reads from the serial port using the DataReceived event handler in C#. When I receive data, I know the header will have 5 bytes, so I don't want to do anything with the data until I have at least that. My current code is below:

                  while (serialPort.BytesToRead<5)
                  {
                  //Do nothing while we have less bytes than the header size
                  }
                  
                  //Once at least 5 bytes are received, process header
                  

                  据我了解,这段代码是阻塞的,需要改进.我正在寻找有关如何执行此操作的建议.DataReceived 事件处理程序中的另一个事件处理程序是否合适?

                  As I understand it, this code is blocking and needs to be improved. I'm looking for suggestions on how to do this. Would another event handler inside the DataReceived event handler be appropriate?

                  推荐答案

                  使用异步编程(不要忘记首先将您的应用程序定位到 .NET Framework 4.5).

                  Use async programming (don't forget to target first your application to .NET Framework 4.5).

                  这里你有我的实现作为 SerialPort 的扩展方法.

                  Here you've my implementation as extension methods for SerialPort.

                  using System;
                  using System.IO.Ports;
                  using System.Threading.Tasks;
                  
                  namespace ExtensionMethods.SerialPort
                  {
                      public static class SerialPortExtensions
                      {
                          public async static Task ReadAsync(this SerialPort serialPort, byte[] buffer, int offset, int count)
                          {
                              var bytesToRead = count;
                              var temp = new byte[count];
                  
                              while (bytesToRead > 0)
                              {
                                  var readBytes = await serialPort.BaseStream.ReadAsync(temp, 0, bytesToRead);
                                  Array.Copy(temp, 0, buffer, offset + count - bytesToRead, readBytes);
                                  bytesToRead -= readBytes;
                              }
                          }
                  
                          public async static Task<byte[]> ReadAsync(this SerialPort serialPort, int count)
                          {
                              var buffer = new byte[count];
                              await serialPort.ReadAsync(buffer, 0, count);
                              return buffer;
                          }
                      }
                  }
                  

                  这里是如何阅读的:

                  public async void Work()
                  {
                     try
                     {
                         var data = await serialPort.ReadAsync(5);
                         DoStuff(data);
                     }
                     catch(Exception excepcion)
                     {
                         Trace.WriteLine(exception.Message);
                     }
                  }
                  

                  这篇关于C# 异步串口读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何显示在串口的 DataReceived 事件处理程序中读取的数据 下一篇:为什么访问com端口被拒绝?

                  相关文章

                  <legend id='zx9Ju'><style id='zx9Ju'><dir id='zx9Ju'><q id='zx9Ju'></q></dir></style></legend>
                    <bdo id='zx9Ju'></bdo><ul id='zx9Ju'></ul>
                  1. <tfoot id='zx9Ju'></tfoot>
                  2. <small id='zx9Ju'></small><noframes id='zx9Ju'>

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