串口读取C#控制台 [英] Serial Port Read C# Console

查看:313
本文介绍了串口读取C#控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我是新来的,我听说过很多有关该网站的信息,它确实可以为您提供帮助。希望您能对我有所帮助!。

Hi to all i am new here and i have heard a lot about this website that it really helps you out. Hope you will be able to help me out!.

我有一个非常简单的程序,它的唯一目的是从串行端口读取并将其打印在控制台窗口上在C#中运行了2000次。

I have a very simple program which its only aim is to read from a serial port and prints it on the console window in C# for 2000 times.

我只是在微控制器上打开一个可变电阻器

I am just turning a variable resistor on a micro-controller that's all

下面是使用系统的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;

namespace Testing_serial_v3._0
{
    class Program
    {
        static void Main(string[] args)
        {

            string buff;


            SerialPort port = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);
            port.Open();

            for (int i = 0; i < 2000; i++)
            {


                buff = port.ReadLine();
                Console.WriteLine(buff);
                //Console.ReadLine();
            }
        }
    }
}

但是这段代码中发生了一件有趣的事情。如上面的代码所示,对控制台readline进行注释时,当我旋转可变电阻器的旋钮时,端口的值就会更改。因此,这意味着它工作正常。
另一方面,如果我使readline发生,以便在每个值后我必须按一个键,端口将读取当前值,即使我改变了旋钮并再次按Enter,该值仍将保持第一个值

But there is a funny thing happening in this code. When the console readline is commented as shown in the code above the value from the port changes as i turn the knob of the variable resistor. So this means it is working good. On the other hand if i make the readline happen so that after each value i have to press a key the port reads the current value and even though i change the knob and press enter again the value will remain the first as if it is not resetting at all?

您是否必须包括其他任何命令行以使端口重置?

Do you have to include any other command lines so that the port will reset?

希望您了解我的问题以及您需要知道的其他任何问题,请不要犹豫,我真的需要尽快解决此问题。
非常感谢和问候

Hope you understand my problem and any other questions you need to know please don't hesitate i really need this problem fixed ASAP. Many thanks and regards

推荐答案

通过端口传输的数据是流-阅读时,您正在逐渐消耗流。您没有看到最新值,而看到了流中的下一个值。添加读取线时,会添加延迟,这意味着存在大量积压数据。并不是说它保持不变 ...只是您还没有读到最后(以及更新的值)。

The data coming through the port is a stream - when you read, you are gradually consuming the stream. You are not seeing "the most recent value", you are seeing "the next value in the stream". When you add the read-line, you add a delay which means there is a large backlog of data. It isn't that "it stayed the same"... Simply that you haven't read to the end (and the more recent values) yet.

在许多情况下在这种情况下,最好通过异步回调处理网络IO,以使从流中读取值不会像人类数据输入那样陷入延迟。不过,这可能涉及一些线程知识。

In many cases, it would be preferable to deal with the network IO via an async callback so that reading values from the stream is not tied into delays like human data entry. That may involve some knowledge of threading, though.

这篇关于串口读取C#控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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