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

发布于 2024-12-28 16:20:06 字数 1944 浏览 2 评论 0原文

我正在为串行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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

七月上 2025-01-04 16:20:07

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.

往日情怀 2025-01-04 16:20:06

privatestatic 在方法范围内无效。
您只能在类级别声明静态项 - 即此处:

public class Program
{
   private static object _memberField;

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

private and static are not valid in the scope of a method.
You can only declare static items at class level - i.e. here:

public class Program
{
   private static object _memberField;

   private static void MemberMethod()
   {
     // not here:
     // private static object _insideMethod; // <- will not work
   }
}
怎樣才叫好 2025-01-04 16:20:06

不需要 private static,只需去 :-

var SerialDataReceivedEventHandler1 =
                   new SerialDataReceivedEventHandler(ComPort.DataReceived);

但如果您希望它成为 Program 类的成员,请将声明移出到 :-

   public class Program
        {

       private static SerialDataReceivedEventHandler;

那么您需要在 Main 中

SerialDataReceivedEventHandler1 =
                       new SerialDataReceivedEventHandler(ComPort.DataReceived);

,但更有可能的是,您确实想开始自己的类,因为“程序”并不是真正最好的地方......并且处理静态类和静态方法有点混乱

dont need private static, just go :-

var SerialDataReceivedEventHandler1 =
                   new SerialDataReceivedEventHandler(ComPort.DataReceived);

but if you are wanting that to be a member of the Program class, Move a declaration out to :-

   public class Program
        {

       private static SerialDataReceivedEventHandler;

then you need in Main

SerialDataReceivedEventHandler1 =
                       new SerialDataReceivedEventHandler(ComPort.DataReceived);

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

滿滿的愛 2025-01-04 16:20:06

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

此外,static 修饰符也无效。它还适用于类型的成员,以指示该成员是否是实例感知的。

你的台词:

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

应该是:

SerialDataReceivedEventHandler SerialDataReceivedEventHandler1 =  
               new SerialDataReceivedEventHandler(ComPort.DataReceived);  

但是,该台词一开始似乎完全没有必要。您可以将其简化为:

myComPort.DataReceived += ComPort.DataReceived;

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:

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

Should be:

SerialDataReceivedEventHandler SerialDataReceivedEventHandler1 =  
               new SerialDataReceivedEventHandler(ComPort.DataReceived);  

However, that line seems entirely unnecessary in the first place. You could simplify it to:

myComPort.DataReceived += ComPort.DataReceived;
笑,眼淚并存 2025-01-04 16:20:06

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

That line is inside the main method. Private and static are only allowed outside of methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文