Serial I/O problems

I know I must be doing something wrong here. I just cant seem to get Serial I/O working. Based on the example on the Arduino site I ran this code, and nothing happened.

My method is I Verify the program, then upload it to the board, and then start up the serial monitor.

int serialdata = 0;

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

void loop()
{
  
  if (Serial.available() > 0)
  {
    serialdata = Serial.read();
    
    Serial.print("I received: ");
    Serial.println(serialdata);
  }
}

The above code returned nothing to me.

int serialdata = 0;

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

void loop()
{
  Serial.print("HelloWorld");
  if (Serial.available() > 0)
  {
    serialdata = Serial.read();
    
    Serial.print("I received: ");
    Serial.println(serialdata);
  }
}

Even this code, though there was the continuous Serial Print in the loop, I saw nothing in the Serial monitor.

What am I doing wrong?

Are you saying in the second case, you didn't see "Hello World"?

Nothing - not even garbage?

In the first case once I randomly typed "123456" and then thereafter I got some output...

In the second case once I got a few garbled characters (about 4) after a lot of whitespace but nothing after that. And got no "HelloWorld"

What is the baud rate setting on the serial monitor?

That's 9600. I checked that.

. I checked that.

Sorry, I had to ask - not everyone does.

Running short on ideas. Can you post a screenshot?

It should be:

if (Serial.available())

instead of this:

if (Serial.available() > 0)

SolidSnake31295:
It should be:

if (Serial.available())

instead of this:

if (Serial.available() > 0)

Wrong. The Serial.available() function returns the number of characters available to read, not a true/false value. The original code is correct.

Attached is a screen shot. When I run the second case with the constant "HelloWorld" print in the loop, I noticed the TX LED on the board was on. I started up serial monitor again, and there was garbage.. I then I had to press a few keys cant remember which though. I think I had to press enter, or press "send" a few times until it started printing a whole stream of "HelloWorld"

With the first case, I tried restarting serial monitor, and when I send data, I can see the "RX" LED flicker, but nothing from TX.

I am on Windows 7 64bit. That shouldnt be a prob?

Is there a certain procedure to run serial monitor?

Try this:

char inByte;  // incoming serial byte

void setup() {
  Serial.begin(9600); // serial communication baud rate
Serial.println("hello World");  
  }


void loop() {
// while loop begins here, continous loop:
  if (Serial.available()) {    // check for incoming data --> if available
  inByte = Serial.read();      // store incoming data
  Serial.print(inByte);        // echo back the data
}

}// while loop ends here

It worked for me.

Serial.available() function returns the number of characters available to read, not a true/false value. The original code is correct.

Both are correct, since "available" (unlike "read") does not return negative values.

SolidSnake31295:
Try this:

char inByte;  // incoming serial byte

void setup() {
 Serial.begin(9600); // serial communication baud rate
Serial.println("hello World");  
 }

void loop() {
// while loop begins here, continous loop:
 if (Serial.available()) {    // check for incoming data --> if available
 inByte = Serial.read();      // store incoming data
 Serial.print(inByte);        // echo back the data
}

}// while loop ends here




It worked for me.

thanks for that solidsnake...it works like magic! the weird thing is, after i upload your code, i then start serial monitor...then i have to wait for a few minutes or press enter/send several times until the first "helloworld" comes up..then it works perfect.

whats wrong with my code and why wont that work?its identical, except for the initial serial print after setting it..

yohanevindra:
press enter/send several times until the first "helloworld" comes up..then it works perfect.

Glad it helped..

whats wrong with my code and why wont that work?its identical, except for the initial serial print after setting it..

Dont know man...
Try restarting your pc or reconnecting your USB or use another USB port from ur pc..