none
How to clear the serial port buffer in visual studio RRS feed

  • Question

  • Hi gays,

    I've written a program in visual studio as below. all things are OK in first run, but second or and third run are incorrect and the characters are repeated again and are not cleared in each run. please help me. I've tried DiscardIn/Out buffer and so on, but I had no luck. 

    thanks.

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    using System.Windows.Forms;

    using System.IO.Ports;

    using System.Threading;

    using System.IO;

    using Excel = Microsoft.Office.Interop.Excel;

    using Microsoft.Office.Interop.Excel;

    using System.Configuration;

    namespace mehdimashayekhi2

    {

        public partial class Form1 : Form

        {

            string Strbaud;

            public Form1()

            {

                InitializeComponent();

            }

            private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)

            {

                Strbaud = comboBox1.Text;

                serialPort1.Close();

                try

                {

                    serialPort1.DataBits = 8;

                    serialPort1.Parity = System.IO.Ports.Parity.None;

                    serialPort1.StopBits = System.IO.Ports.StopBits.One;

                    serialPort1.BaudRate = 9600;

                    serialPort1.PortName = comboBox1.Text;

                    serialPort1.Open();

                    serialPort1.DiscardInBuffer();

                    label6.BackColor = System.Drawing.Color.Green;

                    label6.ForeColor = System.Drawing.Color.White;

                    label6.Text = "(" + comboBox1.Text + ")" + "connected";

                }

                catch

                {

                    label6.Text = "disconected";

                    label6.BackColor = System.Drawing.Color.Red;

                    label6.ForeColor = System.Drawing.Color.White;

                    MessageBox.Show("No available" + "(" + comboBox1.Text + ")", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                }

            }

            char[] charArray = new char[12];

            private void DisplayText(object sender, EventArgs e)

            {

                int i = 0;

                for (i = 1; i < 5; i++)

                {

                    textBox1.AppendText(charArray[i].ToString());

                }

                string s;

                s = textBox1.Text;

                int n;

                int.TryParse(s, out n);

                if (n >= 3000)

                {

                    textBox7.BackColor = System.Drawing.Color.White;

                    textBox7.ForeColor = System.Drawing.Color.Green;

                    textBox7.Text = "Passed";

                }

                else

                {

                    textBox7.BackColor = System.Drawing.Color.White;

                    textBox7.ForeColor = System.Drawing.Color.Red;

                    textBox7.Text = "failed";

                }

                for (i = 7; i < 11; i++)

                {

                    textBox2.AppendText(charArray[i].ToString());

                }

                dataGridView1.Rows.Add("", "", textBox4.Text, textBox5.Text, textBox6.Text, n, textBox2.Text);

            }

            private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)

            {

                dataGridView1.Rows[e.RowIndex].Cells[0].Value = e.RowIndex + 1;

                dataGridView1.Rows[e.RowIndex].Cells[1].Value = "" + textBox3.Text + "-" + (e.RowIndex + 1);

            }

            private void serialPort1_DataReceived(object sender,

     System.IO.Ports.SerialDataReceivedEventArgs e)

            {

                serialPort1.Read(charArray, 0, 12);

                this.Invoke(new EventHandler(DisplayText));

            }

         

     
    Thursday, March 5, 2020 7:00 AM

All replies

  • Hi Mehdi25800,
    In SerialPort Class,the DiscardInBuffer() method is used to discard data from the serial driver's receive buffer,and the DiscardOutBuffer() method is used to discard data from the serial driver's transmit buffer.
    So you can use DiscardInBuffer and DiscardOutBuffer method to flush the buffers.
    And you can simply ignore the data received when you read the data from a serial port.
    More details you can refe to Simon'a answer.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 6, 2020 5:09 AM
  • Hi Mehdi25800,
    Has your problem been solved? If it is resolved, we suggest that you mark it as the answer. So it can help other people who have the same problem find a solution quickly. If not solved, what problem did you encounter?
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 16, 2020 6:11 AM
  • hello friend try the bellow code may help

     private void serialPort1_DataReceived(object sender,
    
     System.IO.Ports.SerialDataReceivedEventArgs e)
    
            {
    
    		SerialPort sp = (SerialPort)sender;
            string indata = sp.ReadExisting();
    
              
            }
    
    

    Monday, March 16, 2020 1:38 PM