C#,串行端口的事件处理程序错误



我正在为串行IO端口写入数据接收事件…以下是代码

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;   //add namespaces
using System.IO.Ports;
     public class Program
        {
            //define a delegate class to handle DataReceived events
            internal delegate void SerialDataReceivedEventHanderDelegate(object sender,SerialDataReceivedEventArgs e);
            internal void DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                //place code to read and process data here
            }
            static void Main()
            {
                string myComPortName=null;
                //1. find available COM port
                string[] nameArray = null;
                nameArray = SerialPort.GetPortNames();
                if(nameArray.GetUpperBound(0)>0) {
                myComPortName = nameArray[0];  
                }
                else {
                    Console.WriteLine("Error");
                    return;
                }    
                //2. create a serialport object
                // the port object is closed automatically by use using()
                SerialPort myComPort = new SerialPort();
                myComPort.PortName = myComPortName; //the default paramit are 9600,no parity,one stop bit, and no flow control
                   private static SerialDataReceivedEventHandler SerialDataReceivedEventHandler1 =
                   new SerialDataReceivedEventHandler(ComPort.DataReceived);
                   myComPort.DataReceived+=SerialDataReceivedEventHandler1;

            }
        }

为什么在VS2010中,行:private static SerialDataReceivedEventHandler SerialDataReceivedPEventHandler1=新的SerialDataReceived EventHandler(ComPort.DataReceived);

正在向我显示:1.表达式术语"private"无效2.修饰符"static"对此术语无效3.我应该在这里使用Comport吗?或者只是接收到数据。。。因为它的功能名称

谢谢你的回复。

privatestatic在方法的作用域中无效。您只能在类级别声明静态项,即此处:

public class Program
{
   private static object _memberField;
   private static void MemberMethod()
   {
     // not here:
     // private static object _insideMethod; // <- will not work
   }
}

不需要私有静态,只需转到:-

var SerialDataReceivedEventHandler1 =
                   new SerialDataReceivedEventHandler(ComPort.DataReceived);

但是,如果你想让它成为Program类的成员,请将声明移到:-

   public class Program
        {
       private static SerialDataReceivedEventHandler;

然后你需要在主

SerialDataReceivedEventHandler1 =
                       new SerialDataReceivedEventHandler(ComPort.DataReceived);

但更有可能的是,你真的想开始自己的课程,因为"程序"并不是最好的地方。。。。处理静态类和静态方法有点混乱

private用于控制类型成员或类型本身的可访问性。它不适用于作为方法一部分的变量,因为方法本身已经最容易访问它了。

此外,静态修饰符也无效。它也适用于某个类型的成员,以指示该成员是否具有实例意识。

您的线路:

private static SerialDataReceivedEventHandler SerialDataReceivedEventHandler1 =  
               new SerialDataReceivedEventHandler(ComPort.DataReceived);

应为:

SerialDataReceivedEventHandler SerialDataReceivedEventHandler1 =  
               new SerialDataReceivedEventHandler(ComPort.DataReceived);  

然而,这句话一开始似乎完全没有必要。您可以将其简化为:

myComPort.DataReceived += ComPort.DataReceived;

该行位于主方法内部。私有和静态只允许在方法之外。

私有限定符仅适用于类定义中的成员(成员变量/方法)。SerialDataReceivedEventHandler只是方法中的一个变量。

相关内容

  • 没有找到相关文章

最新更新