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

        SerialPort 类和 DataReceived 事件...获取字节.使用 ReadLine 还是 ReadExis

        SerialPort class and DataReceived event... Getting bytes. Use ReadLine or ReadExisting? Any examples?(SerialPort 类和 DataReceived 事件...获取字节.使用 ReadLine 还是 ReadExisting?有什么例子吗?)

        <legend id='stJwT'><style id='stJwT'><dir id='stJwT'><q id='stJwT'></q></dir></style></legend>
            <bdo id='stJwT'></bdo><ul id='stJwT'></ul>

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

                • <tfoot id='stJwT'></tfoot>

                  <i id='stJwT'><tr id='stJwT'><dt id='stJwT'><q id='stJwT'><span id='stJwT'><b id='stJwT'><form id='stJwT'><ins id='stJwT'></ins><ul id='stJwT'></ul><sub id='stJwT'></sub></form><legend id='stJwT'></legend><bdo id='stJwT'><pre id='stJwT'><center id='stJwT'></center></pre></bdo></b><th id='stJwT'></th></span></q></dt></tr></i><div id='stJwT'><tfoot id='stJwT'></tfoot><dl id='stJwT'><fieldset id='stJwT'></fieldset></dl></div>
                    <tbody id='stJwT'></tbody>
                • 本文介绍了SerialPort 类和 DataReceived 事件...获取字节.使用 ReadLine 还是 ReadExisting?有什么例子吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想通过串口发送和接收字节.我的接收器应该异步获取字节.我写了下面的小例子,看起来很有效,但它让我有一种不安的感觉.

                  I would like to send and receive bytes over serial. My receiver should get the bytes asynchronously. I've written the small example below that appears to work, but it leaves me with an unsettled feeling.

                  1. 我真的应该使用 WriteLine 还是 ReadLine?正如现在所写的那样,事件处理程序 m_port_DataReceived 中的代码是否可能会为每个字节调用?这就是我对DataReceived"事件的理解;也许我错了.ReadLine 是否会阻塞,直到看到行尾字符?

                  1. Should I really be using WriteLine, ReadLine? As it's written now, is it possible that the code in the event handler m_port_DataReceived is going to be called for each and every byte? That was my understanding of the event "DataReceived"; perhaps I'm wrong. Does ReadLine block until it sees an end of line character?

                  我是否应该在事件处理程序中以某种方式使用if (e.EventType == SerialData.Eof)"构造?你可以看到我把它注释掉了.我试过了,但无法让它工作.什么时候会期望 SerialData.Eof?我的想法是,在调用ReadExisting"之前,我可以等待所有字节都存在.但是,if"语句从未评估为真.但是,强制代码转到 ReadExisting 确实正确读取了所有字节.

                  Should I somehow use the "if (e.EventType == SerialData.Eof)" construct in the event handler? You can see I have it commented out. I tried it and could not get it to work. When would one expect a SerialData.Eof? My idea was that I could wait before all bytes were present before calling "ReadExisting". However, the "if" statement never evaluated to true. Force the code to go to ReadExisting did correctly read all the bytes however.

                  一般来说,设置我的代码以接收来自串行端口的字节的最佳方法是什么?发送方将每 125 毫秒发送一小段字节,但不会发送特殊字符.字节包在时间上是分开的,所以我不认为将包混合在一起有问题.更多的问题是,一旦你看到一个字节,就去读所有的东西,只要你等待很短的时间,你就会得到所有的东西".鉴于这种情况,有没有首选的方法?

                  In general, what's the best way to set up my code to receive bytes coming over the serial port? The sender will be sending small chunks of bytes every 125 msecs, but will not be sending special characters. The packets of bytes are spaced out in time, so I don't think there's a problem of mixing packets together. More the problem is, "once you see one byte, go read everything, as long as you wait a very short time, you'll get everything". Given this scenario, is there a preferred way?

                  提前致谢,戴夫

                  public class SerialPortController
                  {
                      SerialPort m_port;
                  
                      public SerialPortController(bool server)
                      {
                  
                  
                          if (server)
                          {
                              m_port = new SerialPort("COM4"); 
                              m_port.BaudRate = 115200;
                              m_port.Open();
                              byte[] sillyBytes = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };
                              ASCIIEncoding encoding = new ASCIIEncoding();
                              string output_string = encoding.GetString(sillyBytes);
                              m_port.WriteLine(output_string);
                              //m_port.Write(sillyBytes, 0, 8);                  
                          }
                          else
                          {
                              m_port = new SerialPort("COM5"); 
                          m_port.DataReceived += new SerialDataReceivedEventHandler(m_port_DataReceived);
                              m_port.BaudRate = 115200;
                              m_port.Open();
                          }
                          int character = Console.Read();
                      }
                  
                      void m_port_DataReceived(object sender, SerialDataReceivedEventArgs e)
                      {
                          //if (e.EventType == SerialData.Eof)
                          {
                             // string answer = m_port.ReadExisting();
                              string answer = m_port.ReadLine();
                              ASCIIEncoding encoding = new ASCIIEncoding();
                              byte[] byte_answer = encoding.GetBytes(answer);
                  
                          }
                      }
                  }
                  

                  推荐答案

                  MSDN 展示了这个的简单使用.

                  Theres and example at MSDN which shows simple use of this.

                  它使用 ReadExisting 而不是 ReadLine.

                  And it used ReadExisting instead of ReadLine.

                  也来自文档:

                  不保证每收到一个字节都会引发 DataReceived 事件.使用 BytesToRead 属性来确定缓冲区中还有多少数据需要读取.

                  The DataReceived event is not guaranteed to be raised for every byte received. Use the BytesToRead property to determine how much data is left to be read in the buffer.

                  private static void DataReceviedHandler(
                                      object sender,
                                      SerialDataReceivedEventArgs e)
                  {
                      SerialPort sp = (SerialPort)sender;
                      string indata = sp.ReadExisting();
                      Console.WriteLine("Data Received:");
                      Console.Write(indata);
                  }
                  

                  这篇关于SerialPort 类和 DataReceived 事件...获取字节.使用 ReadLine 还是 ReadExisting?有什么例子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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='jJs1v'></bdo><ul id='jJs1v'></ul>
                      <tfoot id='jJs1v'></tfoot>

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

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