Click here to Skip to main content
15,916,835 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to read and write data on serial port (COM3) and my write method works well but read method doesn't ,I showed my code below please have a look their and correct me if i am going wrong.

Thanks.

C#
public partial class Form1 : Form
    {
        private SerialPort port = new SerialPort("COM3");

        public Form1()
        {
            InitializeComponent();
        }

        public void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            string indata = port.ReadExisting();
            textBox1.Text = indata;
        }

        private void Enable_Click(object sender, EventArgs e)// enable button
        {
            port.BaudRate = 9600;

            port.Open();

            if (port.IsOpen)
                {
                    Enable.Enabled = false;
                    Disable.Enabled = true;
                }
        }

        private void Disable_Click(object sender, EventArgs e) // disable button
        {
            if (port.IsOpen)
            {
                port.Close();
                Enable.Enabled = true;
                Disable.Enabled = false;
            }
        }

        private void button1_Click(object sender, EventArgs e) // send button
        {
            try
            {
                string strToSend;
                strToSend = textBox2.Text;

                if (port.IsOpen)
                {
                    port.Write(strToSend);
                    port.Close();
                }
                else
                {
                    port.Open();
                    port.Write(strToSend);
                    port.Close();
                }
            }
            catch(Exception es) { MessageBox.Show(es.Message); }
        }

        private void button2_Click(object sender, EventArgs e) //receive button
        {
            try
            {
                port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
                port.Open();
                Application.Run();
            }
            catch (Exception es) { MessageBox.Show(es.Message); }
        }
    }
Posted
Comments
[no name] 20-Sep-15 9:39am    
Have a look here. It is Basic and easy to understand:
Serial Comms in C# for Beginners[^]

Hello,
I don't have at hands a dev environment to test it,
so I can give you only generic advices.

First of all, why do you write the initialization statements in the button2_click procedure ?

I would move it in the constructor, just after initialize_components.
that is :

C#
public Form1()
{
  InitializeComponent();
  port.BaudRate = 9600;
  port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
}

After that you should remove the Application.run() statement from the button2.click statement.

Application.Run() should be called just after having created the main form,
from the application main procedure so that the application can correctly handle any events that happens to it.

Third, try to understand what the error message say and let us know about it.

Fourth, how can the sender of the data knows it is time to send it ?
There is a kind of 'protocol' between this application and the sender ?

Does the sender reply to a message sent to it by this application or it sends data asynchronously ?

If this is the case, you should leave the port open all the time, that is, open it at program start and close it at program termination.

Best regards.
Maurizio.
 
Share this answer
 
Comments
Member 11543226 21-Sep-15 2:23am    
port.DataReceived does not fires hence i concluded that port doesnt get response that he has to send something to requested party.
ReadExisting[^] will only read whatever is available at the port at that moment. You only try to read when the button is clicked so there is no synchronisation between your application and the port. You need to look for data continuously when you have received an indication that there is some available.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900