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 8711007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:46:17+00:00 2026-06-13T04:46:17+00:00

I am using the SerialPort.ReadLine() method to display the data received from a Serial

  • 0

I am using the SerialPort.ReadLine() method to display the data received from a Serial Port (code below).

Previously the code looked like that and received data but it did not send data. Now it is the other way around:

Since I placed the port.DataReceived event within the if(port==null) statement and added SerialPort port; as field, I don’t receive data anymore. Can placing the event within an if statement change the way data is received and displayed? How can I fix that?

 //Fields
        List<string> myReceivedLines = new List<string>();
        SerialPort port;

        //subscriber method for the port.DataReceived Event
        private void DataReceivedHandler(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            while (sp.BytesToRead > 0)
            {
                try
                {
                    myReceivedLines.Add(sp.ReadLine());
                }
                catch (TimeoutException)
                {
                    break;
                }
            }
        }

        protected override void SolveInstance(IGH_DataAccess DA)
        {
           //Opening the port
            if (port == null)
            {

                string selectedportname = default(string);
                DA.GetData(1, ref selectedportname);
                int selectedbaudrate = default(int);
                DA.GetData(2, ref selectedbaudrate);
                bool connecttodevice = default(bool);
                DA.GetData(3, ref connecttodevice);

                //Assigning an object to the field within the SolveInstance method()
                port = new SerialPort(selectedportname, selectedbaudrate, Parity.None, 8, StopBits.One);

                //Event Handling Method
                if (connecttodevice == true)
                {
                    port.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
                    DA.SetDataList(0, myReceivedLines);
                }
                //Enables the data terminal ready (dtr) signal during serial communication (handshaking)
                port.DtrEnable = true;
                port.Open();
            }

            //Displays if port if opened
            if (port.IsOpen)
            {
                DA.SetData(1, "Port Open");
            }

            //If the port is open do all the rest
            if (port.IsOpen)
            {
                //Assigning the input to variables for the code.
                List<string> gcode = new List<string>();
                DA.GetDataList(0, gcode);
                bool sendtoprint = default(bool);
                DA.GetData(4, ref sendtoprint);
                bool homeall = default(bool);
                DA.GetData(5, ref homeall);

                //What happens when input is set
                if (sendtoprint == true)
                {

                if (homeall == true)
                {
                    port.Write("G28" + "\n");
                }

            }
            else
            {
                DA.SetData(1, "Port Closed");
            }
        }
  • 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-06-13T04:46:18+00:00Added an answer on June 13, 2026 at 4:46 am

    Try something like this, removing the attaching of the eventhandler out of the port creation section

    if (port == null) 
    { 
        string selectedportname = default(string); 
        DA.GetData(1, ref selectedportname); 
        int selectedbaudrate = default(int); 
        DA.GetData(2, ref selectedbaudrate); 
        bool connecttodevice = default(bool); 
        DA.GetData(3, ref connecttodevice); 
    
        //Assigning an object to the field within the SolveInstance method() 
        port = new SerialPort(selectedportname, selectedbaudrate, Parity.None, 8, StopBits.One); 
    
        //Enables the data terminal ready (dtr) signal during serial communication (handshaking) 
        port.DtrEnable = true; 
    
    } 
    
    if (connecttodevice == true) 
    { 
        if(!port.IsOpen)
        {
            port.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler); 
            DA.SetDataList(0, myReceivedLines); 
            port.Open();
        }
    } 
    else
    {
        if(port.IsOpen)
        {
            port.DataReceived -= new SerialDataReceivedEventHandler(DataReceivedHandler); 
           // DA.SetDataList(0, myReceivedLines); // Not sure how you want to remove this
            port.Close();
        } 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm reading data from a serial port using an event listener from the SerialPort
While reading from serial port I am using serialport.read(xyz,0,4) where xyz is a byte
I'm using the below code to receive the messages using serial port event handler.
I am using the below code to receive the message via serial port which
I am using C# to receive data from a serial port but there are
I am reading from serial port using string messaga = _serialPort.ReadLine(); When i Console.WriteLine(messaga);
I'm trying to open and read from a serial port using the System.IO.Ports.SerialPort class.
I am using Serial port to receive the messages. The below function is running
I am writing a data received event For Serial IO port...the following is the
The code below is what I am using to send and receive information from

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.