本文介绍了在SerialPort的main()中添加事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我尝试订阅事件处理程序到数据接收事件。好像我不能指定事件处理函数的名称。我不明白为什么 myComPort.DataReceived + = new SerialDataReceivedEventHandler(comPort_DataReceived);给我错误信息。 这是问题,希望有人可以回答。 一只忙猫http://img827.imageshack.us/img827/5904/20120125102247.png 一只忙猫http://img444.imageshack.us/img444/3855/20120125102202.png 命名空间serialport { public class Program { 内部列表< Byte> portBuffer = new List< Byte>(1024); static void Main() { // 1。找到可用的COM端口 string [] nameArray = null; string myComPortName = null; nameArray = SerialPort.GetPortNames(); if(nameArray.GetUpperBound(0)> = 0) { myComPortName = nameArray [0]; } else { Console.WriteLine(Error); return; } // 2。创建一个serialport对象 //端口对象使用using()自动关闭SerialPort myComPort = new SerialPort(); myComPort.DataReceived + = new SerialDataReceivedEventHandler(comPort_DataReceived); myComPort.PortName = myComPortName; //默认的paramit是9600,没有奇偶校验,一个停止位,没有流量控制 //3.open port 尝试 { myComPort.Open(); } catch(UnauthorizedAccessException ex) { MessageBox.Show(ex.Message); } //添加超时,p161 //读取字节字节[] byteBuffer =新字节[10]; Int32计数; Int32 numberOfReceivedBytes; myComPort.Read(byteBuffer,0,9); for(count = 0; count< = 3; count ++) { Console.WriteLine(byteBuffer [count] .ToString()); } } //事件处理程序应该是静态的? void comPort_DataReceived(object sender,SerialDataReceivedEventArgs e) { int numberOfBytesToRead; numberOfBytesToRead = myComPort.BytesToRead; byte [] newReceivedData = new byte [numberOfBytesToRead]; myComPort.Read(newReceivedData,0,numberOfBytesToRead); portBuffer.AddRange(newReceivedData); ProcessData(); } private void ProcessData() { //当8个字节到达时,显示然后从缓冲区中删除 int count; int numberOfBytesToRead = 8; if(portBuffer.Count> = numberOfBytesToRead) { for(count = 0; count< numberOfBytesToRead; count ++) {控制台.WriteLine((char)的(portBuffer第[count])); } portBuffer.RemoveRange(0,numberOfBytesToRead); } } } } 在您的事件处理程序中,myComPort不在范围内 - 它在main()方法中本地声明。我建议您将COM端口处理提取到一个类中,并使myComPort成为该类的成员变量。 另外,您的意见注意到SerialPort类有一个管理资源,它需要处理使用IDisposable /使用模式,但你没有使用块包装访问通信端口。 最后,该方法您作为事件处理程序作为实例成员而不是作为静态成员添加;要从main()方法的静态范围访问它,您需要从该类的一个实例中获取它或使该方法成为静态。 I try to subscribe a event handler to the data received event. Seems like I cant specify the event handler function name. I dont understand whymyComPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived); is giving me error message.Here is the problem, hope anyone can answer it.a busy cat http://img827.imageshack.us/img827/5904/20120125102247.pnga busy cat http://img444.imageshack.us/img444/3855/20120125102202.pngnamespace serialport{ public class Program { internal List<Byte> portBuffer = new List<Byte>(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 <= 3; count++) { Console.WriteLine(byteBuffer[count].ToString()); } } //The event handler should be static?? void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { int numberOfBytesToRead; numberOfBytesToRead = myComPort.BytesToRead; byte[] newReceivedData = new byte[numberOfBytesToRead]; myComPort.Read(newReceivedData, 0, numberOfBytesToRead); portBuffer.AddRange(newReceivedData); ProcessData(); } private void ProcessData() { //when 8 bytes have arrived, display then and remove them from the buffer int count; int numberOfBytesToRead = 8; if (portBuffer.Count >= numberOfBytesToRead) { for (count = 0; count < numberOfBytesToRead; count++) { Console.WriteLine((char)(portBuffer[count])); } portBuffer.RemoveRange(0, numberOfBytesToRead); } } } } 解决方案 In your event handler, myComPort isn't in scope - it's declared locally in your main() method. I would suggest that you extract the com port handling into a class and make myComPort a member variable of that class.Also, your comments note that the SerialPort class has a managed resource that it needs to dispose of using the IDisposable / Using pattern, but you don't have a using block wrapping the access to the comm port.Last, the method you are adding as the event handler exists as an instance member rather than as a static member; to access it from the main() method's static scope, you need to either grab it from an instance of the class or make the method static. 这篇关于在SerialPort的main()中添加事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-03 15:19