串行端口的ReadLine VS ReadExisting或如何正确地读取串口数据 [英] Serial Port ReadLine vs ReadExisting or how to read the data from serial port properly

查看:5522
本文介绍了串行端口的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.

任何帮助吗?

好像串口是一个任性的孩子。

Seems like serial port is a capricious child.

C#的WinCE 5.0,HP瘦客户机,Compact Framework 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()

$ B结束
$ b

有一点要注意我的应用程序是我启动线程(weighSample)当光标跳转到文本框。之所以这是重量也可以在手动(生成req的一部分)输入。所以我不提前知道用户是否会按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 req's). 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.

任何意见/想法表示欢迎。这是我与串行端口第一次体验。

Any comments/ ideas welcome. 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.

这篇关于串行端口的ReadLine VS ReadExisting或如何正确地读取串口数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆