You must Sign In to post a response.
  • Category: .NET

    Receiving data from serial port not working?

    I am writing simple communication application between serial port "COM3" and when i am trying send data to port it is working with "send" button , but my receive button cannot able to receive data from port, with receive button.

    Here is my code , please check once if you found some mistake , correct me.
    thank you!

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

    port.Open();

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

    private void Disable_Click(object sender, EventArgs e) // disable connection
    {
    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(serialPort1_DataReceived);
    port.Open();
    }
    catch (Exception es) { MessageBox.Show(es.Message); }
    }

    public void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
    SerialPort sp = (SerialPort)sender;
    string indata = sp.ReadExisting();
    MessageBox.Show(indata);
    }
  • #762439
    Hello Priyanka,

    This is my code for Console Application :

    public static void Main()
    {
    SerialPort sp = new SerialPort("COM1");

    sp.BaudRate = 9600;
    sp.Parity = Parity.None;
    sp.StopBits = StopBits.One;
    sp.DataBits = 8;
    sp.Handshake = Handshake.None;
    sp.RtsEnable = true;

    sp.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

    sp.Open();

    Console.WriteLine("Press any key to continue...");
    Console.WriteLine();
    Console.ReadKey();
    sp.Close();
    }

    private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
    {
    SerialPort spp = (SerialPort)sender;
    string indata = spp.ReadExisting();
    Console.WriteLine("Data Received:");
    Console.Write(indata);
    }

    You used in windows application. It seems same. Can't find any difference.

    But i suggest you to put break point and check whether you are getting value or not.

    Hope this will help you.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #762440

    Hello Priyanka,

    You can go through the below link for reference for the same application code in windows forms :

    http://www.codeproject.com/Articles/75770/Basic-serial-port-listening-application

    Hope this will also work for you.


    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"


Sign In to post your comments