I'm trying to make the program respond to certain words sent by a microcontroller, but when I compare the received word with a pre-defined word, it always returns false.
private void ReadData()
{
if (serialPort1.IsOpen == true)
{
if (serialPort1.BytesToRead > 0)
{
string readBuffer = serialPort1.ReadLine();
textBox2.Text = readBuffer;
if (readBuffer.Equals("A")) //MY MAIN PROBLEM
{
textBox2.Text += "YEP";
}
else
{
textBox2.Text += "NOPE";
}
}
}
}
Basically when the microcontroller sends letter "A", it reads it and stores it into the readBuffer string, and even prints it out in a textbox(textBox2). My result is always ANOPE in the textbox (A is what the microcontroller sent and NOPE is always there because the if failed).
I started C# recently and lost several days trying to figure this one out, but I really can't seem to find a solution to an apparently simple issue.
readBufferstring?\nor some other byte after theAin the string. You did aReadLineso there may be some new line character that your text box does not display.readBuffer.StartsWith("A")