Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7168773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:50:57+00:00 2026-05-28T14:50:57+00:00

Relates to the problem Adding event handler in main() for SerialPort After I change

  • 0

Relates to the problem Adding event handler in main() for SerialPort

After I change the mycomport to one of the member of the class, why I cant use it in main?

a busy cat http://img11.imageshack.us/img11/3664/49512217.png

namespace serialport
{
public class Program
{


    #region Manager Variables
    //property variables
    private string _baudRate = string.Empty;
    private string _parity = string.Empty;
    private string _stopBits = string.Empty;
    private string _dataBits = string.Empty;
    private string _portName = string.Empty;
    private RichTextBox _displayWindow;
    //global manager variables
    //private Color[] MessageColor = { Color.Blue, Color.Green, Color.Black, Color.Orange, Color.Red };
    internal List<Byte> portBuffer = new List<Byte>(1024);
    private SerialPort myComPort = new SerialPort();
    #endregion



    #region Manager Constructors
    /// <summary>
    /// Comstructor to set the properties of our
    /// serial port communicator to nothing
    /// </summary>
    public Program()
    {
        _baudRate = string.Empty;
        _parity = string.Empty;
        _stopBits = string.Empty;
        _dataBits = string.Empty;
        _portName = "COM1";
        _displayWindow = null;
        //add event handler
        myComPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);
    }
    #endregion

    static void Main()
    {

        Program myProgram = new Program();
        //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()
        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??
    internal 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);
        }
    }

}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:50:58+00:00Added an answer on May 28, 2026 at 2:50 pm

    Because Main() is a static method. Instance fields cannot be accessed directly by static methods. A static method would have to create an instance of the object or get an instance of the object passed to it for that to be possible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a memory problem related to UIImageView. After adding this view to my
User relates to Employee as one-to-many. The problem is the list of Employee s
This question relates closely to the stack overflow question " window.resize event firing in
I'm adding some custom enhancements to a jQuery-UI tabbed widget. One of the things
The problem described below relates to an inventory tracking Java program. There are multiple
Problem: When adding list item the Value field is being set to the Text
After posting this: Custom Header in GridView ...I have a related problem. I have
This issue is somewhat related: Problem with Code Generated by XSD.EXE: Sequence of Elements
I am currently working on a programming related problem where I am attempted to
some time ago I happend to resolve a PG related problem with this SO

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.