SerialPort 类和 DataReceived 事件...获取字节.使用 ReadLine 还是 ReadExisting?有什么例子吗?

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

本文介绍了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 展示了这个的简单使用.

There´s 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?有什么例子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:SerialPort 类和 DataReceived 事件...获取字节.使用 ReadLine 还是 ReadExisting?有什么例子吗?

基础教程推荐