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

        <legend id='5ojro'><style id='5ojro'><dir id='5ojro'><q id='5ojro'></q></dir></style></legend>

          <bdo id='5ojro'></bdo><ul id='5ojro'></ul>

      1. 使用 Async await 方法从串口异步读取

        Reading from serial port asynchronously using Async await method(使用 Async await 方法从串口异步读取)

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

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

              • <bdo id='H4rC6'></bdo><ul id='H4rC6'></ul>
                  <tbody id='H4rC6'></tbody>
                <legend id='H4rC6'><style id='H4rC6'><dir id='H4rC6'><q id='H4rC6'></q></dir></style></legend>
                1. 本文介绍了使用 Async await 方法从串口异步读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用这种方式从串口读取:

                  I'm using this way for reading from serial port :

                  public static void Main()
                  {
                      SerialPort mySerialPort = new SerialPort("COM1");
                  
                      mySerialPort.BaudRate = 9600;
                      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);
                  }
                  

                  我们知道这种API称为基于事件的异步模式(EAP),我想使用Async Await方法编写上面的代码.

                  as we know This kind of API is called the Event-based Asynchronous Pattern (EAP), I want to write above code using Async Await method.

                  PS:使用上面的代码有时我会得到错误的数据
                  提前致谢

                  PS : with above code sometime I'm getting wrong data
                  Thanks in Advance

                  推荐答案

                  您也可以从 SerialPort.BaseStream 读取数据.它属于 Stream 类型,因此支持可等待的 ReadAsync() 方法.将其转换为字符串取决于您,请使用正确的编码.SerialPort 的默认值为 ASCIIEncoding.

                  You can also read data from SerialPort.BaseStream. Which is of type Stream so supports the awaitable ReadAsync() method. Converting it to a string is up to you, use the proper Encoding. The default for SerialPort is ASCIIEncoding.

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

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

                  相关文档推荐

                  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() 和泛型:错误绑定到目标方法)
                    <bdo id='Kc336'></bdo><ul id='Kc336'></ul>

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

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

                        <tfoot id='Kc336'></tfoot>