这种情况是不是用委托呢? 委托串口接收事件 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 SerialDataReceivedEventHandler这本身就是一个委托,你转到它的定义去看一下是什么类型,你那个方法就传此类型的参数 我就是用委托把方法当参数传进去,然后编译通过,我只要打开串口 然后接收 就说我串口名是null值 说明还有没有注册到该事件。可能是我写错了。我把源码贴一下吧。先是定义了委托 public delegate void DataReceivedRegister(object sender, System.IO.Ports.SerialDataReceivedEventArgs e);然后 在 封装类里 写一个公共方法 public void DataReceived(DataReceivedRegister dataReceived) { this.communication.serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(dataReceived); }然后 主form1下 public partial class MainWindow : Window { private delegate void DelegateUpdateUI(); SPCL.Processing mag_obj = new Processing(); public MainWindow() { InitializeComponent(); this.LoadingWindows(); } private void LoadingWindows() { this.mag_obj.AddPortInfo(comboBox1, PortProperty.PortName); this.mag_obj.AddPortInfo(comboBox2, PortProperty.BaudRate); this.mag_obj.AddPortInfo(comboBox3, PortProperty.DataBit); this.mag_obj.AddPortInfo(comboBox4, PortProperty.ParityBit); this.mag_obj.AddPortInfo(comboBox5, PortProperty.StopBit); this.mag_obj.DataReceived(serial_port_DataReceived); } void serial_port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { // 更新主线程上的控件来显示数据 Dispatcher.Invoke(new DelegateUpdateUI(this.UpdateControl)); // throw new NotImplementedException(); } 串口的OPEN和CLOSE方法在哪里?OPEN之前,你需要设置串口名,波特率,数据位,校验位,停止位,这些在你代码里都没看到. 在click按钮事件下面写了 串口配置 和 打开串口了 private void button1_Click(object sender, RoutedEventArgs e) { this.mag_obj.StartPort( this.comboBox1.Text, this.comboBox2.Text, this.comboBox3.Text, this.comboBox4.Text, this.comboBox5.Text); }然后 info 是一个串口信息的结构体 public void StartPort(string name,string baud,string data,string parity,string stop) { var info =this.communication.portInfo; info.PortName = name; info.BaudRtae = baud; info.DataBits = data; info.ParityBits = parity; info.StopBits = stop; this.communication.ConfigurePort(); this.communication.OpenPort(); } 我可能猜到问题了 问题出在 varvar info =this.communication.portInfo; info.PortName = name;把 var 当 object用了 还是说,你StartPort的方法本身就在类里 原先var info =this.communication.portInfo; info.PortName = name; info.BaudRtae = baud; info.DataBits = data; info.ParityBits = parity; info.StopBits = stop;改成 this.communication.portInfo.PortName = name; this.communication.portInfo.BaudRtae = baud; this.communication.portInfo.DataBits = data; this.communication.portInfo.ParityBits = parity; this.communication.portInfo.StopBits = stop; this.communication.ConfigurePort(); this.communication.OpenPort();顺利读到数据了。 现在 基本上把串口通信功能 封装成一个相对高内聚的dll 了。也非常感谢各位来回答我的问题 结贴了。 这样算是异步才操作吗? 用户‘’登录失败 简单的sql问题 SQL查询时有些字段不能被识别?为什么? 怎样让winform最大化后所有组件调整位置或按比例变化? 谁要招寒假期间的免费临时工??(仅限于福州地区)----给菜鸟一次成长的机会!! datagridview删除时,为什么隔一行删一行? C#中的堆栈溢出问题 窗口的布局和定位问题 @"....."是啥意思 关于C# Socket多次Send的问题 【求助】这个C里的函数“ReadBaseMsgPhoto”,在C#里如何声明呢?
可能是我写错了。我把源码贴一下吧。先是定义了委托 public delegate void DataReceivedRegister(object sender, System.IO.Ports.SerialDataReceivedEventArgs e);然后 在 封装类里 写一个公共方法 public void DataReceived(DataReceivedRegister dataReceived)
{ this.communication.serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(dataReceived);
}
然后 主form1下 public partial class MainWindow : Window
{
private delegate void DelegateUpdateUI(); SPCL.Processing mag_obj = new Processing();
public MainWindow()
{
InitializeComponent();
this.LoadingWindows();
} private void LoadingWindows()
{
this.mag_obj.AddPortInfo(comboBox1, PortProperty.PortName);
this.mag_obj.AddPortInfo(comboBox2, PortProperty.BaudRate);
this.mag_obj.AddPortInfo(comboBox3, PortProperty.DataBit);
this.mag_obj.AddPortInfo(comboBox4, PortProperty.ParityBit);
this.mag_obj.AddPortInfo(comboBox5, PortProperty.StopBit);
this.mag_obj.DataReceived(serial_port_DataReceived); } void serial_port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
// 更新主线程上的控件来显示数据
Dispatcher.Invoke(new DelegateUpdateUI(this.UpdateControl));
// throw new NotImplementedException();
}
OPEN之前,你需要设置串口名,波特率,数据位,校验位,停止位,这些在你代码里都没看到.
private void button1_Click(object sender, RoutedEventArgs e)
{
this.mag_obj.StartPort(
this.comboBox1.Text,
this.comboBox2.Text,
this.comboBox3.Text,
this.comboBox4.Text,
this.comboBox5.Text);
}然后 info 是一个串口信息的结构体 public void StartPort(string name,string baud,string data,string parity,string stop)
{
var info =this.communication.portInfo; info.PortName = name;
info.BaudRtae = baud;
info.DataBits = data;
info.ParityBits = parity;
info.StopBits = stop; this.communication.ConfigurePort();
this.communication.OpenPort();
}
var info =this.communication.portInfo;
info.PortName = name;把 var 当 object用了
var info =this.communication.portInfo;
info.PortName = name;
info.BaudRtae = baud;
info.DataBits = data;
info.ParityBits = parity;
info.StopBits = stop;
改成 this.communication.portInfo.PortName = name;
this.communication.portInfo.BaudRtae = baud;
this.communication.portInfo.DataBits = data;
this.communication.portInfo.ParityBits = parity;
this.communication.portInfo.StopBits = stop; this.communication.ConfigurePort();
this.communication.OpenPort();
顺利读到数据了。
现在 基本上把串口通信功能 封装成一个相对高内聚的dll 了。也非常感谢各位来回答我的问题 结贴了。