I am running 1.6.5 on a Mac and an Uno, but cannot seem to get Serial.available() to ever recognize that there is input coming from the computer. I'm also not entirely sure the computer is sending anything out. Is there a way to get the serial monitor to just echo locally, without listening for data from the Arduino?
Anyway, I have written and run several large Arduino programs and can very successfully use the Serial.println() kind of commands and get all kinds of good info from the Uno to the serial monitor. I simply can't get it to work the other way. Even if I run this teeny script and tap on every key on the keyboard, nothing ever displays. I would expect that as soon as I pressed a key, the message would start spewing out...but alas: nothing. The same things happens at 9600.
void setup()
{
Serial.begin(115200);
}
void loop()
{
while (Serial.available() > 0) {
Serial.println ( "Serial data is waiting to be collected.");
}
}
I've looked through all kinds of web resources (including this one), but am having no luck. Does anyone have any pointers for what I might try?
If something is received, the Serial.available() returns the number of bytes in the receive buffer. Your while loop will forever stuck in the while loop as soon as something is in the receive buffer.
Try the example : Serial.available() - Arduino Reference
When you press a key, nothing happens. You have to enter data in the input text field and press enter.
You may of course use another terminal program, that sends characters as soon as they are typed.
The baudrate in the sketch must be the same as the baudrate in the serial monitor.
Delta_G: I feel like quite the nug. Would you believe that I never noticed the top line on the serial monitor before? The world is once again back in balance. Karma has been judiciously doled out.
void setup()
{
Serial.begin(115200);
}
void loop()
{
while (Serial.available() > 0) {
Serial.println ( "Serial data is waiting to be collected.");
}
}
Since you never read serial chars from the input buffer, once there is a char it will stay in that loop.
Change that while to if and make the next line Serial.read() then change your message and you will have something that works in a more interesting way and might have some use.
You don't need a while loop to get all the Serial. Serial even a 115200 is slower than your loop().
You don't need to do everything in a single pass of loop() like you would in main() on a PC.
Here is a sketch that shows how many passes through loop() what you send takes to get.
Only one thing... don't feed it more than 79 characters or it will likely crash, I didn't put bounds checks into the sketch.
unsigned int loopCounter; // starts as 0 by default
char buf[ 80 ]; // text buffer, since printing is slow
byte index;
void setup()
{
Serial.begin(115200);
Serial.print( "\n\n\n" ); // 3 newlines to space past the startup garbage
}
void loop()
{
if ( Serial.available() > 0 )
{
if ( index == 0 )
{
loopCounter = 0; // don't count until after the 1st char is read
}
char data = Serial.read();
if ( data >= ' ' )
{
buf[ index++ ] = data; // making a C string
}
else if ( data == '\n' ) // if newline -- serial monitor must send newline!
{
Serial.print( buf );
Serial.print( " took " );
Serial.print( loopCounter );
Serial.println( " passes through loop." );
for ( index = 0; index < 80; index++ )
{
buf[ index ] = 0;
}
index = 0; // loopCounter will be reset above
}
}
loopCounter++;
}
Output, I entered "How about this?" and it gave me;
How about this? took 272 passes through loop.
16 chars including the newline at the end took 272 times through loop().
Your sketch could be doing other things and what is does now at the same time.
But put in a while loop that waits for the whole message to arrive and no multitasking for you!