SerialPort类和DataReceived事件检索...获取字节。使用输入行或ReadExisting?任何的例子吗? [英] SerialPort class and DataReceived event... Getting bytes. Use ReadLine or ReadExisting? Any examples?

查看:146
本文介绍了SerialPort类和DataReceived事件检索...获取字节。使用输入行或ReadExisting?任何的例子吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过串口发送和接收字节。我的接收器应该得到的字节是异步的。我写了下面的小例子似乎工作,但它给我留下了一个不稳定的感觉。


  1. 我确实应该使用的WriteLine,的ReadLine?因为它是现在写的,是有可能,在事件处理程序的code m_port_DataReceived将被要求对每一个字节?那是我的事件DataReceived的认识;也许是我错了。确实的ReadLine块,直到它看到一行字符的结尾?


  2. 我应该以某种方式使用事件处理程序的如果(e.EventType == SerialData.Eof)构造?你可以看到我有注释掉。我试了一下,不能让它的工作。之一,当所期望的SerialData.Eof?我的想法是,我可以等待之前的所有字节都称之为ReadExisting之前present。但是,如果声明从来没有评估为true。强制code去ReadExisting但是没有正确读取所有字节。


  3. 在一般情况下,有什么设置我的code接收过来串口字节的最好方法?发送方将派出每125毫秒字节的小块,但不会发送特殊字符。字节的数据包在时间上隔开,所以我不认为有混合在一起的数据包的问题。更多的问题是,一旦你看到一个字节,去阅读一切,只要你等待一个很短的时间,你会得到一切。鉴于这种情况,有没有preferred方式?


在此先感谢,
戴夫

 公共类SerialPortController
{
    的SerialPort m_port;    公共SerialPortController(布尔服务器)
    {
        如果(服务器)
        {
            m_port =新的SerialPort(COM4);
            m_port.BaudRate = 115200;
            m_port.Open();
            字节[] sillyBytes =新字节[] {0,1,2,3,4,5,6,7};
            ASCIIEncoding编码=新ASCIIEncoding();
            字符串output_string = encoding.GetString(sillyBytes);
            m_port.WriteLine(output_string);
            //m_port.Write(sillyBytes,0,8);
        }
        其他
        {
            m_port =新的SerialPort(COM5);
        m_port.DataReceived + =新SerialDataReceivedEventHandler(m_port_DataReceived);
            m_port.BaudRate = 115200;
            m_port.Open();
        }
        INT字符= Console.Read();
    }    无效m_port_DataReceived(对象发件人,SerialDataReceivedEventArgs E)
    {
        //如果(e.EventType == SerialData.Eof)
        {
           //字符串答案= m_port.ReadExisting();
            串答案= m_port.ReadLine();
            ASCIIEncoding编码=新ASCIIEncoding();
            字节[] = byte_answer encoding.GetBytes(答案);        }
    }
}


解决方案

There's和榜样在的 MSDN 这说明简单的使用了这一点。

和它曾经ReadExisting代替的ReadLine。

另外从文档:


  

该DataReceived事件检索不能保证提高为收到的每个字节。使用BytesToRead属性来确定多少数据被留在缓冲器被读取。


 私有静态无效DataReceviedHandler(
                    对象发件人,
                    SerialDataReceivedEventArgs五)
{
    的SerialPort SP =(的SerialPort)发送;
    字符串INDATA = sp.ReadExisting();
    Console.WriteLine(数据收稿日期:);
    Console.Write(INDATA);
}

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. 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?

  2. 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.

  3. 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?

Thanks in advance, Dave

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

        }
    }
}

解决方案

There´s and example at MSDN which shows simple use of this.

And it used ReadExisting instead of ReadLine.

Also from the docs:

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事件检索...获取字节。使用输入行或ReadExisting?任何的例子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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