Welcome! Log In Create A New Profile

Advanced

C#: Appending the data received from a Serial Port to a list of string?

Posted by arthurmani 
C#: Appending the data received from a Serial Port to a list of string?
October 15, 2012 11:59AM
Hi Guys,

When connecting to a serial port using the code below, the program displays feedback from it. When sending a new command to the serial port, the feedback disapears. How could I append the new data coming from the port to a list of string within `myReceivedLines` instead of loosing it every time a new command is sent?

Many thanks,

Arthur

Code:


//Fields
        List myReceivedLines = new List();
        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);
               
                //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;
                port.WriteTimeout = 500;
                port.ReadTimeout = 500;
            }

            //Event Handling Method
            bool connecttodevice = default(bool);
            DA.GetData(3, ref connecttodevice);

            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);
                    port.Close();
                }
                
                
            if (port.IsOpen)
            {
                DA.SetData(1, "Port Open");
            }

            //If the port is open do all the rest
            if (port.IsOpen)
            {    
                bool homeall = default(bool);
                DA.GetData(5, ref homeall);
               
                //What happens when input is set
                if (homeall == true)
                {
                    port.Write("G28" + "\n");
                }

            }
            else
            {
                DA.SetData(1, "Port Closed");
            }
        }

Edited 1 time(s). Last edit at 10/15/2012 12:17PM by arthurmani.


--
Arthur Mamou-Mani
Architect, AAdipl, RIBA III
Tutor DS10 Westminster University
Mamou-Mani.com ¦ WeWantToLearn.net
Re: C#: Appending the data received from a Serial Port to a list of string?
October 15, 2012 12:09PM
There is no reference to MyReceivedData in your code, so I can't even see what you are referring to.

But this is a very basic C# question, I am sure you would get more help from a C# programming forum.
Re: C#: Appending the data received from a Serial Port to a list of string?
October 15, 2012 12:16PM
Thanks bobc, sorry I meant `myReceivedLines` will edit the question and post it on a programming forum.


--
Arthur Mamou-Mani
Architect, AAdipl, RIBA III
Tutor DS10 Westminster University
Mamou-Mani.com ¦ WeWantToLearn.net
Sorry, only registered users may post in this forum.

Click here to login