Another string problem...

I have written a Visual Basic program that passes data to the serial port. I am able to read and write integers, but I'm apparently missing something when it comes to dealing with strings in C++.

The VB program is simply writing "Hello" to the port...

Dim myPort As New Ports.SerialPort("COM4")
myPort.BaudRate = 9600
myPort.Open()
myPort.Write("Hello")

The code running on the Arduino...

void loop()
{
   if(Serial.available() > 0)
   {
     for(i = 0; i < Serial.available(); i++)
     {
       myData[i] = Serial.read();
     }
     
     myData[sizeof(myData)] = 0;
     
     if(strcmp("Hello", myData) == 0)
     {
       for(i = 0; i <= 20; i++)
       {
         digitalWrite(ledPin, HIGH);
         delay(50);
         digitalWrite(ledPin, LOW);
         delay(50);
       }
     }
   }
}

The code is never validating what is passed to the serial port as "Hello". I've read most of the posts on the forum, but still seem to be missing something...

Trace what happens in your program flow when you have received the first byte ('H') in the serial buffer... then what happens when you receive the 'e'....

Part of your problem is here:
myData[sizeof(myData)] = 0;

sizeof cannot be used like this - it always returns a constant.

Edit: I had another thought. Imagine you had declared "char myData [10];", so "myData" has ten elements with subscripts zero to nine.
"sizeof(myData)" is ten, so "myData[sizeof(myData)] = 0;" would write past the end of "myData".

You have to remember that serial lines are really sloooow.

At 9600 bps, one character takes over 1ms to transmit.
In this time, "loop" could execute maybe hundreds of times.

So:

   if(Serial.available() > 0)
   {
     for(i = 0; i < Serial.available(); i++)
     {
       myData[i] = Serial.read();
     }

if you've just received the 'H' in "Hello", the first "Serial.available" returns 1, so you execute "for(i = 0; i < 1; i++) myData [0] = 'H'.

The rest of the string, 'ello', still hasn't left the transmitter, so "loop" repeats until 'e' arrives.
"Serial.available" returns 1, and you overwrite the 'H' in "myData[0]" with 'e'.
See where this is going?

(and what Ty Breizh said about not using "sizeof").

this code will type hello world

void setup() {
Serial.begin(9600);
}

void loop() {
Serial.println("Hello World!");
}

@jbbiz - do you know what a non sequitur is?

um no why?

Figures.

AWOL,

LOL!

P.S. Do you believe in coincidence? Me, too!!