November 09, 2024, 05:13:40 AM

while(!Serial); doesn't work

Started by danielrleinad, June 18, 2014, 07:26:00 PM

Previous topic - Next topic

danielrleinad

Hey everyone,

I want to send text to the serial monitor, but while(!Serial); doesn't work in the setup method. I tried it with while(Serial.available() == 0); and it works (after my input of course).

This is the complete code:
int num = 0;

void setup() {
  Serial.begin(9600);
 
  while(!Serial); //wait for serial port to connect - needed for Leonardo only
  //wait_for_serial();   
}


void loop() {
 
  delay(250);
  num += 1;
  Serial.println(num);
 
  if (num >= 23){
   stop();
  }
 
}

void stop() {

  while(1 == 1);
 
}

void wait_for_serial(void)

  while(Serial.available() == 0);
}


Is there a workaround for the Olimexino 32-U4 without sending input?

Thanks,
daniel

Insomniac10102

#1
I'm assuming your reason for not wanting to send input is that you want it to be automated. If that's not the case, you can ignore this post.

As much as I'd hate to do this to you, it would technically work.. So I'm only suggesting it as an absolute worst case scenario. You could start sending data from your board via Serial and waiting for a response from something like a Java program. Once you get a response, your serial port is ready.

Here's a tutorial on Java/arduino serial communication: http://playground.arduino.cc/Interfacing/Java. You can copy & paste this code, just change the serial event to do this instead of printing the input it gets from the olimex board:


output.write(1);


now from your olimex board, you can use Serial.available() like so:


while(Serial.available() == 0)
{

Serial.write(1);

}

Serial.flush();


Note that the output from the devices is completely ignored; it doesn't matter what you send to either side.

This is definitely way overkill, but if you never find anything better or just need a solution ASAP, then this should work for you. Hope it helps.

EDIT: This would likely be much simpler to achieve in python, but the concept is the same.