我如何使用在C#中的SerialPort端口对象的DataReceived事件检索? [英] How do I use dataReceived event of the SerialPort Port Object in C#?

查看:333
本文介绍了我如何使用在C#中的SerialPort端口对象的DataReceived事件检索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个小的应用程序来收集连接到COM10的外部传感器接收到的数据。我已经成功地创建打开的端口,并使用一个for循环流数据文件的时间固定期限的小C#控制台对象和应用。

我想这个应用程序使用DataReceived事件检索,而不是流转换。看完后的<一个href=\"http://blogs.msdn.com/bclteam/archive/2006/10/10/Top-5-SerialPort-Tips-_5b00_Kim-Hamilton_5D00_.aspx\">Top 5的SerialPort提示,我似乎仍不能以它的工作,不知道我错过了什么。我重写了控制台应用程序,使所有的code是主及以下粘贴。是否有人可以帮助开导我,为什么事件处理程序port_OnReceiveDatazz不会被调用即使我知道,有数据由硬件被发送到端口?

感谢

阿齐姆

PS:感谢<一个href=\"http://stackoverflow.com/questions/466474/how-do-i-use-datareceived-event-of-the-serialport-port-object-in-c#466577\">@Gabe, <一href=\"http://stackoverflow.com/questions/466474/how-do-i-use-datareceived-event-of-the-serialport-port-object-in-c#466510\">@Jason下来和<一个href=\"http://stackoverflow.com/questions/466474/how-do-i-use-datareceived-event-of-the-serialport-port-object-in-c#466518\">@abatishchev所有的建议。我难倒,似乎无法获得事件处理工作。也许它是与该设备。我可以只读取端口中的一个线索和数据流直接到文件住。


code


 命名空间serialPortCollection
{类​​节目
    {
        静态无效的主要(字串[] args)
        {            const int的由bufSize = 2048;
            字节[] buf中=新的字节[由bufSize] //要存储所接收的数据。            的SerialPort SP =新的SerialPort(COM10,115200);
            sp.DataReceived + = port_OnReceiveDatazz; //添加DataReceived事件检索处理程序            sp.Open();
            sp.WriteLine($); //命令启动数据流            //等待数据或用户输入继续。
            到Console.ReadLine();            sp.WriteLine(!); //停止数据流命令
            sp.Close();
        }       //我的事件处理程序方法
        私有静态无效port_OnReceiveDatazz(对象发件人,
                                   SerialDataReceivedEventArgs五)
        {
            SPL的SerialPort =(的SerialPort)发送;
            const int的由bufSize = 12;
            字节[] buf中=新的字节[由bufSize]
            Console.WriteLine(接收完毕!);
            Console.WriteLine(spL.Read(BUF,0,由bufSize));
        }
    }
}


解决方案

我觉得你的问题是该行: **

sp.DataReceived + = port_OnReceiveDatazz;

难道不应该是:

sp.DataReceived + =新SerialDataReceivedEventHandler(port_OnReceiveDatazz);

**没关系,语法是罚款(不知道在我最初回答了这个问题时的快捷方式)。

我也看到了建议,你应该打开下面的选项为您的串行端口:

  sp.DtrEnable = TRUE; //数据终端就绪
sp.RtsEnable = TRUE; //请求到发送

您可能还需要设置握手RequestToSend(通过握手枚举)。​​


更新:

发现了一个建议,说你应该先打开你的端口,然后分配事件处理程序。也许这是一个错误吗?

因此​​,而不是这样的:

  sp.DataReceived + =新SerialDataReceivedEventHandler(port_OnReceiveDatazz);
sp.Open();

做到这一点:

  sp.Open();
sp.DataReceived + =新SerialDataReceivedEventHandler(port_OnReceiveDatazz);

让我知道如何继续下去。

I am attempting to create a small application to collect data received from an external sensor attached to COM10. I have successfully created a small C# console object and application that opens the port and streams data to a file for a fixed period of time using a for-loop.

I would like to convert this application to use the dataReceived event to stream instead. After reading the Top 5 SerialPort Tips, I still can't seem to it to work and don't know what I am missing. I rewrote the console application so that all the code is in Main and is pasted below. Can someone please help enlighten me as to why the event handler port_OnReceiveDatazz is not being called even though I know that there is data being sent to the port by the hardware?

Thanks

Azim

PS: Thanks to @Gabe, @Jason Down, and @abatishchev for all the suggestions. I am stumped and can't seem to get the event handler to work. Perhaps it has something to do with the device. I can live with just reading the port in a thread and streaming the data straight to file.


Code


namespace serialPortCollection
{   class Program
    {
        static void Main(string[] args)
        {

            const int bufSize = 2048;
            Byte[] buf = new Byte[bufSize]; //To store the received data.

            SerialPort sp = new SerialPort("COM10", 115200);
            sp.DataReceived += port_OnReceiveDatazz; // Add DataReceived Event Handler

            sp.Open();
            sp.WriteLine("$"); //Command to start Data Stream

            // Wait for data or user input to continue.
            Console.ReadLine();

            sp.WriteLine("!"); //Stop Data Stream Command
            sp.Close();
        }

       // My Event Handler Method
        private static void port_OnReceiveDatazz(object sender, 
                                   SerialDataReceivedEventArgs e)
        {
            SerialPort spL = (SerialPort) sender;
            const int bufSize = 12;
            Byte[] buf = new Byte[bufSize];
            Console.WriteLine("DATA RECEIVED!");
            Console.WriteLine(spL.Read(buf, 0, bufSize));
        }
    }
}

解决方案

I think your issue is the line:**

sp.DataReceived += port_OnReceiveDatazz;

Shouldn't it be:

sp.DataReceived += new SerialDataReceivedEventHandler (port_OnReceiveDatazz);

**Nevermind, the syntax is fine (didn't realize the shortcut at the time I originally answered this question).

I've also seen suggestions that you should turn the following options on for your serial port:

sp.DtrEnable = true;    // Data-terminal-ready
sp.RtsEnable = true;    // Request-to-send

You may also have to set the handshake to RequestToSend (via the handshake enumeration).


UPDATE:

Found a suggestion that says you should open your port first, then assign the event handler. Maybe it's a bug?

So instead of this:

sp.DataReceived += new SerialDataReceivedEventHandler (port_OnReceiveDatazz);
sp.Open();

Do this:

sp.Open();
sp.DataReceived += new SerialDataReceivedEventHandler (port_OnReceiveDatazz);

Let me know how that goes.

这篇关于我如何使用在C#中的SerialPort端口对象的DataReceived事件检索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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