使用 Readline() 串行端口删除数据

发布于 2024-07-15 00:26:28 字数 1258 浏览 11 评论 0 原文

我正在使用串行端口从连接到瘦客户端的秤上读取数据。 在 99% 的情况下,数据可以正确读取 - 即秤上的任何内容都是应用程序捕获的内容。 然而,有时,数据看起来像是被丢弃了。 例如,它将被读取为 0.007,而不是 90.007。 我正在使用 ReadLine 函数:

private void CaptureWeight()
    {
         globalCounter++;
         string value = "";
         _sp.DiscardInBuffer();

          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()

I am using serial port to read the data off the scale that is attached to the thin client. In 99% of cases the data is read correctly - ie whatever is on the scale is what is captured by the application.
However, sometimes, it looks like data is dropped. For instance instead of 90.007 it will be read as 0.007. I am using ReadLine function:

private void CaptureWeight()
    {
         globalCounter++;
         string value = "";
         _sp.DiscardInBuffer();

          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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半﹌身腐败 2024-07-22 00:26:29

“ES”什么时候来? 理论上,“ES”之后的值可能无法正确读取,因为您调用了 DiscardInBuffer()。 如果此时缓冲区包含下一个读数的一部分,例如 90.007 中的 9,则 9 将被丢弃,您将读取 0.007。

尝试仅丢弃最后一个 CR LF 之前的所有内容。 但留下不完整的线条。

When does "ES" come? It is theory possible that the value immediately after "ES", is not read correctly, because you call DiscardInBuffer(). If in that time the buffer contains part of the next reading, e.g. the 9 in 90.007, the 9 gets discarded and you read 0.007.

Try discarding only everything before the last CR LF. But leave incomplete lines.

零度℉ 2024-07-22 00:26:28

不要调用 DiscardInBuffer。 当数据通过 异步被填充://en.wikipedia.org/wiki/UART" rel="nofollow noreferrer">UART。 读取所有数据并对其进行相应操作,因为在丢弃缓冲区时您无法知道缓冲区中有什么!

Don't call DiscardInBuffer. The operating system buffer is filled asynchronously as data is shifted in through the UART. Read all of the data and act on it accordingly because you have no way of knowing what is in the buffer at the time you discard it!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文