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

    <tfoot id='pBLuU'></tfoot>

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

        • <bdo id='pBLuU'></bdo><ul id='pBLuU'></ul>
      1. Serial Port ReadLine vs ReadExisting 或如何正确地从串口读取数据

        Serial Port ReadLine vs ReadExisting or how to read the data from serial port properly(Serial Port ReadLine vs ReadExisting 或如何正确地从串口读取数据)
        <tfoot id='WZFob'></tfoot>

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

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

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

                    <tbody id='WZFob'></tbody>

                • 本文介绍了Serial Port ReadLine vs ReadExisting 或如何正确地从串口读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在从串口读取数据.数据超出规模.我现在正在使用 Readline() 并且即使在我删除 DiscardInBuffer() 之后也会删除数据.

                  I am reading data from serial port. The data comes off the scale. I am now using Readline() and getting data dropped even after I removed DiscardInBuffer().

                  从串口读取数据的正确方法是什么?网上的例子太少了,我觉得这就像一个没有人想出来的圣杯.

                  What is the proper way to read the data from the serial port? There are so few examples online that I feel it's like some holy grail that no one has figured out.

                  C#、WinCE 5.0、HP 瘦客户端、Compact 框架 2.0

                  C#, WinCE 5.0, HP thin client, Compact framework 2.0

                   private void WeighSample()
                      {
                          this._processingDone = false;
                          this._workerThread = new Thread(CaptureWeight);
                          this._workerThread.IsBackground = true;
                          this._workerThread.Start();
                      } //end of WeighSample()
                  
                  
                      private void CaptureWeight()
                      {
                           globalCounter++;
                           string value = "";
                  
                  
                            while (!this._processingDone)
                            {
                                try
                                {
                  
                                   value = this._sp.ReadLine();
                  
                                    if (value != "")
                                    {
                                        if (value == "ES")
                                        {
                                            _sp.DiscardInBuffer();
                                            value = "";
                                        }
                                        else
                                        {
                                            this.Invoke(this.OnDataAcquiredEvent, new object[] { value });
                                        }
                                    }
                                }
                                catch (TimeoutException)
                                {
                                    //catch it but do nothing
                                }
                                catch
                                {
                                    //reset the port here?
                                    MessageBox.Show("some other than timeout exception thrown while reading serial port");
                                }
                            }
                  
                  
                      } //end of CaptureWeight()
                  

                  关于我的应用程序需要注意的一点是,当光标跳到文本框时,我会启动线程 (weighSample).原因是重量也可以手动输入(部分要求).所以我事先不知道用户是要按天平上的 PRINT 还是输入重量.无论哪种情况,在获取数据后,我都会退出工作线程.另外,请注意我没有使用串行端口事件 DataReceived,因为我被告知它不可靠.

                  One thing to note about my application is that I start the thread (weighSample) when the cursor jumps onto the textbox. The reason to this is that the weight can also be typed in manually (part of the requirements). So I don't know in advance whether a user is going to press PRINT on the balance or type the weight. In either case after the data is acquired, I exit the worker thread. Also, note that I am not using serial port event DataReceived, since I have been told it's not reliable.

                  这是我第一次使用串口.

                  This is my first experience with serial ports.

                  推荐答案

                  从来没有在 ReadLine 工作时遇到过运气.只需在数据可用时读取本地缓冲区,然后使用单独的线程扫描数据并自行查找换行符.

                  I have never had luck with ReadLine working. Just do a Read into a local buffer whenever data is available and then use a separate thread to scan the data and find line breaks yourself.

                  这篇关于Serial Port ReadLine vs 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() 和泛型:错误绑定到目标方法)
                    <tbody id='gFioK'></tbody>

                • <legend id='gFioK'><style id='gFioK'><dir id='gFioK'><q id='gFioK'></q></dir></style></legend>
                • <small id='gFioK'></small><noframes id='gFioK'>

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

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