none
Serial Communication Beginner question... RRS feed

  • Question

  • Hello Everyone!

    I am a beginner in C# programming and I am in the middle of code realizing Serial Communication.

    The problem is that I never receive the complete data I have sent. For example:

    If I send following string: "Hellow World!" I receive this: "llow world!" or this: "ow World" ext....,but never the whole text being sent. 

    Here is my code:

    namespace Read_Serial_Data
    {
        public partial class Form1 : Form
        {
            private string received = "Wait...";
            public Form1()
            {
                InitializeComponent();
            }

            private void InitButt_Click(object sender, EventArgs e)
            {
                if (!serialPort.IsOpen)
                {
                    serialPort.Open();
                    StatusLable.Text = "Serial Port Open!";
                
                }
                else
                    StatusLable.Text = "Serial Port Bussy!";
            }

            private void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
            {
                received = serialPort.ReadExisting();
                this.Invoke(new EventHandler(DisplayText));
               
            }

            private void DisplayText(object o, EventArgs e)
            {
                textBox1.Text = received;
            }

            private void Form1_FormClosed(object sender, FormClosedEventArgs e)
            {
                serialPort.Close();
            }
        }
    }

    Some hints would be very helpful!

    Excuse My Poor English :)!

    • Moved by Bob Shen Thursday, February 7, 2013 8:38 AM
    Sunday, February 3, 2013 5:03 PM

All replies

  • Change your DisplayText method. Assign the recieved string as parameter. E.g. private void DisplayText(string recieved). Then Add the result as textBox1.Text += received;
    Monday, February 4, 2013 12:13 AM

  • Would you write an example code. When I try to declare private void DisplayText(string recieved)

    I get only errors. If you can fit your suggestion to my code (upper post) would be very helpful.

    Monday, February 4, 2013 8:02 PM