解决方案 »

  1.   

    SerialDataReceivedEventHandler这本身就是一个委托,你转到它的定义去看一下是什么类型,你那个方法就传此类型的参数
      

  2.   

    我就是用委托把方法当参数传进去,然后编译通过,我只要打开串口 然后接收 就说我串口名是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();
            }
      

  3.   

    串口的OPEN和CLOSE方法在哪里?
    OPEN之前,你需要设置串口名,波特率,数据位,校验位,停止位,这些在你代码里都没看到.
      

  4.   

    在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();
            }
      

  5.   

    我可能猜到问题了 问题出在 var
    var info =this.communication.portInfo;
                info.PortName = name;把 var 当 object用了
      

  6.   

    还是说,你StartPort的方法本身就在类里
      

  7.   

    原先
    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 了。也非常感谢各位来回答我的问题 结贴了。