Using same serial port data received event on two different forms(在两种不同的形式上使用相同的串口数据接收事件)
问题描述
我的 SerialDataReceivedEventHandler 出现问题,无法响应 serail 端口中的数据.
I have a problem with my SerialDataReceivedEventHandler whitch fails to respond to data in serail port.
我有一个主要形式,我打开端口并做其他事情需要完成正确的串行端口通信(发送和接收工作)!然后我在同一个项目中打开另一个表单,需要相同的串行端口进行读写!问题是我在 form2 中的 SerialDataReceivedEventHandler 无法正常工作,它与 mainform 中的第一个完全相同.(如果我在主窗体中调用 serial.close() 应用程序冻结或导致巨大延迟)
I have one main form in whitch i open port and do other stuf whitch need to be done for proper serial port communication (sending and receiving work)! Then i open another form in same project whitch need same serial port for reading and writing! Problem is that my SerialDataReceivedEventHandler in form2 not working jet it is completely identical to the first in mainform. (if i call serial.close() in main form app freezes or cause huge delay)
可能我必须将此事件的主要内容公开,但我仍然不知道如何使我的自定义事件或其他会触发表单 2 中的事件的事件,即数据到达端口
probably i have to make in my main from this event public, but i still don't konow how to make my custom event or something else that will trigger the event in form 2 that data is arrived on port
我找到此链接寻求帮助,但不适用于我的应用程序.
I found this link for help but does not work with my app.
http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/7efccf0e-b412-4869-b942-a006773a833f
我正在使用VS2008,framework3.5(智能设备项目)
i'm using VS2008,framework3.5 (smart device project)
有人可以帮我吗?,请!
can somebody help me with that? , Please!
推荐答案
将您的 SerialPort 消耗移动到单独的静态(或单例)类中.在该类中创建一个 DataReceived 事件,并在每次接收到数据时触发它.让两个表单都订阅 DataReceived 事件 - 这样两个表单都将接收数据.
Move your SerialPort consumption into a separate static (or a singleton) class. Create a DataReceived event in that class and fire it every time data is received. Have both forms subscribe to the DataReceived event - this way both forms will receive the data.
编辑 1:伪代码示例
public static class Serial {
public static delegate void DataReceivedEventHandler(object sender, ReceivedEventArgs e);
public static event DataReceivedEventHandler DataReceived;
static SerialPort serialPort = new SerialPort();
static Serial() {
serialPort = new SerialPort();
serialPort.DataReceived += Incoming;
serialPort.Open();
}
private static void Incoming(object sender, SerialDataReceivedEventHandler args) {
if (DataReceived != null) {
ReceivedEventArgs rea = new ReceivedEventArgs {Data = args.Data};
DataReceived(this, rea);
}
}
}
public class ReceivedEventArgs : EventArgs {
public string Data { get; set;}
}
public class Form1: Form {
public Form1() {
Serial.DataReceived += Incoming;
}
private void Incoming(object sender, ReceivedEventArgs e) {
// you receive the data here
Debug.WriteLine(e.Data);
}
}
public class Form2: Form {
public Form2() {
Serial.DataReceived += Incoming;
}
private void Incoming(object sender, ReceivedEventArgs e) {
// you receive the data here
Debug.WriteLine(e.Data);
}
}
同样,这是伪代码,附近没有编译器.希望这会有所帮助.
Again, this is pseudo code, without compiler nearby. Hope this helps.
这篇关于在两种不同的形式上使用相同的串口数据接收事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在两种不同的形式上使用相同的串口数据接收事件
基础教程推荐
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- rabbitmq 的 REST API 2022-01-01