简体   繁体   中英

C#, event handler error with Serial Port

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.

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

public class Program
{
   private static object _memberField;

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

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

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;

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM