简体   繁体   中英

How to receive data from a port and show in a C# windows form?

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);
    }

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM