Basit
Basit

Reputation: 890

c# serialport read functions not reading data

I dont know where is the problem , I open the serial port and send message it work fine , but when I click the receive button , it does not receive any thing i use all these functions Read() , ReadLine() , ReadExisting() ,ReadTo() none of them works for me.

private void sendBtn_Click(object sender, EventArgs e)
{
   if (!serialPort1.IsOpen)
    {
        try
        {
            serialPort1.Open();
            serialPort1.Write(textBox1.Text);
        }
        catch (Exception ez )
        {
            MessageBox.Show(ez.Message);
        }    
    }
    else
    {
        try
        {
            serialPort1.Write(textBox1.Text+"\n");
            textBox1.Text = "";             
        }
        catch (Exception ez)
        {
            MessageBox.Show(ez.Message);
        }        
    }
}

Then this is the receive data button code

private void receiveBtn_Click(object sender, EventArgs e)
{
    try
    {
        textBox2.Text = serialPort1.ReadExisting();
    }
    catch (TimeoutException z)
    {
        MessageBox.Show(z.Message)      
    } 
}

diagram 1

Upvotes: 0

Views: 738

Answers (1)

BWA
BWA

Reputation: 5764

Serial port send data imediatly. So you ned to read at same moment at different port which recevied this data. If you need to test it locally you need some serial port loopback software, which can add virtual ports and make virtual connection between them.

You must have 2 connected serial ports.

You can use this software to create 2 serial ports and connect them. And your app cans write to one port and read second.

Upvotes: 1

Related Questions