-1

I am doing two port communication using the XBEE module. I am sending and receiving the value using a C# Windows form.

The sending code is working perfectly:

    private void button2_Click(object sender, EventArgs e)
    {
        try
        {
        SerialPort port1 = new SerialPort("COM28", 9600, Parity.None, 8, StopBits.One);
        port1.Handshake = Handshake.None;
        port1.Open();
        port1.Write("Hello");
        port1.Close();
        }
        catch(Exception ex)
        {
          MessageBox.Show(""+ex);
        }
    }

The receiver side is unable to receive the message. The code is working in a console application; but when I try to code this in a Windows form, it's not working:

    private void button1_Click(object sender, EventArgs e)
    {
        SerialPort mySerialPort = new SerialPort("COM29");

        mySerialPort.BaudRate = 9600;
        mySerialPort.Parity = Parity.None;
        mySerialPort.StopBits = StopBits.One;
        mySerialPort.DataBits = 8;
        mySerialPort.Handshake = Handshake.None;



        mySerialPort.Open();
        mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
        mySerialPort.Close();

    }
    private static void DataReceivedHandler(object sender,SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        string indata = sp.ReadExisting();
        MessageBox.Show("Data received");
        MessageBox.Show(indata);
    }
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
user3494471
  • 371
  • 1
  • 7
  • 21
  • 1
    Try to remove mySerialPort.Close() in button1_Click (receive part) – Dan May 06 '14 at 07:15
  • try to define mySerialPort in class, not in method. – tim May 06 '14 at 07:17
  • 1
    You can try to use ReadExisting in button1_Click without firing DataReceiveHandler event. If you want to use DataReceiveHandler, you should remove it because I think it is not disposed by compiler. Everytime you click the button1, a new event is created without disposing the latest. Anyway, using a breakpoint, have you verified if the code reaches "string indata.."? – Emi987 May 06 '14 at 07:27
  • 1
    No wonder it isn't receiving any data. As soon as you open the COM port, you close it. Perhaps it would be better if you explained what it *should* do. – Mark Bertenshaw May 06 '14 at 07:36
  • @Emi987 I tried using break point.The code doesn't reaches DataReceivedHandler method – user3494471 May 06 '14 at 07:52
  • This question appears to be off-topic because... the OP hasn't bothered to read what they (most likely have not) written! – Sam Axe May 06 '14 at 08:00
  • And... Where does it hang? – Emi987 May 06 '14 at 08:06
  • @Emi987 It goes to "mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);" and suddenly goes to mySerialPort.close();. It doesn't call DataReceivedHandler method. – user3494471 May 06 '14 at 08:13

1 Answers1

2

As far as I can see, you copied the example in msdn page about SerialPort. It works on ConsoleApplication becuase it never goes to mySerialPort.Close(); because the little app is waiting for an input ( Look at Console.ReadKey();).
In this case, the code has the time to handle the DataReceivedHandler and display the string stored in indata. In this link, another user has the same problem as you and he has been suggested to use Delegate. I don't know it is exactly your case, but it can help you.
You can even try to not use DataReceivedHandler. For instance, you can try something like this:

private void button1_Click(object sender, EventArgs e)
{
    SerialPort mySerialPort = new SerialPort("COM29");

    mySerialPort.BaudRate = 9600;
    mySerialPort.Parity = Parity.None;
    mySerialPort.StopBits = StopBits.One;
    mySerialPort.DataBits = 8;
    mySerialPort.Handshake = Handshake.None;

    mySerialPort.Open();
    /*If the read buffer is not empty, indata will not be an empty string*/
    string indata = sp.ReadExisting(); //You can even use sp.ReadLine() and see if it changes something
    MessageBox.Show("Data received");
    MessageBox.Show(indata);
    mySerialPort.Close();

}

Let's see if this may help you!

Community
  • 1
  • 1
Emi987
  • 385
  • 1
  • 12