Can any one help?
I have an Arduino Mega sending the word "hello" to the computer via COM3.
The C# program I wrote is supposed to display "The variable is set to true." if it received "hello".
If it did not receive hello it will display what it received.
The problem is when I run it, it displays "hello" which means it should have displayed "The variable is set to true."
Thanks
Here is the code:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
namespace SerialTest
{
class Program
{
static void Main(string[] args)
{
string[] names = SerialPort.GetPortNames();
SerialPort p = new SerialPort("COM3");
p.DataReceived += new SerialDataReceivedEventHandler(p_DataReceived);
p.Open();
string line;
do
{
line = Console.ReadLine();
} while (line != "quit");
p.Close();
}
static void p_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string data = ((sender as SerialPort).ReadLine());
string data2 = "hello";
if (data == data2)
{
Console.WriteLine("The variable is set to true.");
}
else
{
Console.WriteLine((data));
}
}
}
}