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