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中,行: 私有静态 SerialDataReceivedEventHandler SerialDataReceivedEventHandler1 = new SerialDataReceivedEventHandler(ComPort.DataReceived);
正在向我展示: 1. 无效的表达术语“私人” 2. 修饰语“static”对该术语无效 3.我应该在这里使用Comport吗?或者只是 DataReceived...因为它的函数名称
感谢您的回复。
I am writing a data received event For Serial IO port...the following is the code
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;
}
}
Why in VS2010, Line:
private static SerialDataReceivedEventHandler SerialDataReceivedEventHandler1 = new SerialDataReceivedEventHandler(ComPort.DataReceived);
is showing me:
1. invalid expression term 'private'
2. the modifier 'static' is not valid for this term
3. should I use Comport here? or just DataReceived...since its the function name
Thanks for your reply.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
private 限定符仅适用于类定义中的成员(成员变量/方法)。 SerialDataReceivedEventHandler 只是方法内的一个变量。
private qualifier applies only for a member (member variables/methods) in your class definition. The SerialDataReceivedEventHandler is just a variable which you have inside a method.
private
和static
在方法范围内无效。您只能在类级别声明静态项 - 即此处:
private
andstatic
are not valid in the scope of a method.You can only declare static items at class level - i.e. here:
不需要 private static,只需去 :-
但如果您希望它成为 Program 类的成员,请将声明移出到 :-
那么您需要在 Main 中
,但更有可能的是,您确实想开始自己的类,因为“程序”并不是真正最好的地方......并且处理静态类和静态方法有点混乱
dont need private static, just go :-
but if you are wanting that to be a member of the Program class, Move a declaration out to :-
then you need in Main
but more likely, you really want to start your own class, because "Program" isn't really the best place.... and dealing with static classes and static methods is a bit messy
private
用于控制类型成员或类型本身的可访问性。它不适用于属于方法一部分的变量,因为方法本身已经最容易访问它。此外,static 修饰符也无效。它还适用于类型的成员,以指示该成员是否是实例感知的。
你的台词:
应该是:
但是,该台词一开始似乎完全没有必要。您可以将其简化为:
private
is used to control the accessibility of members of a type, or types themselves. It is not applicable to a variable that is part of a method, since it's already most accessible to the method itself.Additionally, the static modifier is not valid as well. It also applies to members of a type to indicate whether or not that member is instance aware.
Your line:
Should be:
However, that line seems entirely unnecessary in the first place. You could simplify it to:
该行位于 main 方法内部。私有和静态仅允许在方法之外。
That line is inside the main method. Private and static are only allowed outside of methods.