在main()中为SerialPort添加事件处理程序

我尝试为接收的数据事件订阅一个事件处理程序。 好像我无法指定事件处理函数名称。 我不明白为什么
myComPort.DataReceived + = new SerialDataReceivedEventHandler(comPort_DataReceived); 给我错误信息。 这是问题所在,希望有人能回答。

一只忙碌的猫http://img827.imageshack.us/img827/5904/20120125102247.png

一只忙碌的猫http://img444.imageshack.us/img444/3855/20120125102202.png

namespace serialport { public class Program { internal List portBuffer = new List(1024); static void Main() { //1. find available COM port string[] nameArray = null; string myComPortName = 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.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived); myComPort.PortName = myComPortName; //the default paramit are 9600,no parity,one stop bit, and no flow control //3.open the port try { myComPort.Open(); } catch (UnauthorizedAccessException ex) { MessageBox.Show(ex.Message); } //Add timeout, p161 //reading Bytes byte[] byteBuffer = new byte[10]; Int32 count; Int32 numberOfReceivedBytes; myComPort.Read(byteBuffer, 0, 9); for (count = 0; count = numberOfBytesToRead) { for (count = 0; count < numberOfBytesToRead; count++) { Console.WriteLine((char)(portBuffer[count])); } portBuffer.RemoveRange(0, numberOfBytesToRead); } } } } 

在您的事件处理程序中,myComPort不在范围内 – 它在main()方法中本地声明。 我建议你将com端口处理提取到一个类中,并使myComPort成为该类的成员变量。

此外,您的注释注意到SerialPort类具有需要使用IDisposable / Using模式处理的托管资源,但您没有使用包装块来访问通信端口。

最后,作为事件处理程序添加的方法作为实例成员而不是静态成员存在; 要从main()方法的静态范围访问它,您需要从类的实例中获取它或使方法静态。

首先,由于方法Main是静态的,因此您只能在同一个类中调用其他静态方法。 实际上, comPort_DataReceived被声明为实例方法,以下代码应该修复事件处理程序的赋值:

 static void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { // ... } 

其次,由于myComPort是在Main定义的,因此它在comPort_DataReceived不可见。 您有两种选择:将myComPort声明为您的类的静态成员,或使用事件处理程序的sender参数:

 static void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { SerialPort port = (SerialPort)sender; // ... } 

Tetsujin no Oni的答案是处理范围问题的理想方式。 另一种方法也是将myComPort声明为程序的静态成员,例如:

 internal List portBuffer = new List(1024); private SerialPort myComPort = new SerialPort(); 

然后只需从main方法中删除myComPort声明即可。