Go Down

Topic: Trouble with serial output. (Read 98 times) previous topic - next topic

wildbill

Yesterday I was trying to debug some code long distance and couldn't get the Nano 33 to produce any of the debugging serial data, so I wrote a typical hello world program which also failed. I got nothing until I put in a delay after while(!serial). i.e.
Code: [Select]

void setup()
{
Serial.begin(115200);
while (!Serial)
  ;  // Do nothing
delay(5000);  // Wait for serial to wake up. Not understood why it's needed

Serial.println("Hello world");
}

n.b. This is reconstructed, perhaps not char for char what I was running.

I ran the LED example code for the Nano 33 BLE which does serial output no problem and gradually stripped out the BLE stuff until it too stopped working when it had nothing left but my above example, but two print statements. I assume that because there were objects being constructed which took some time, they took the place of my explicit delay.

Without the delay, it seems that I get no serial output, ever. I haven't experimented to see how long it actually needs to be.

Any thoughts on what's happening?

Klaus_K

#1
Nov 10, 2019, 04:55 am Last Edit: Nov 10, 2019, 05:19 am by Klaus_K
I modified your code a little bit and did some experiment on the Nano 33 BLE. It looks like the print gets stored in some buffer but does not end up in the Serial Monitor window.
This board uses the onboard USB peripheral, which needs to be initialized and connected to the PC anytime you reset the Nano. Looks like this is a bug in the code. How could this be reviewed by the Arduino team?

Here is how to replicate the behavior.

- Compile and download the code.

The next steps can be repeated reliably without recompiling

- Close the Serial Monitor.
- Reset the Nane 33 BLE.
- Make sure the COM port is setup correctly.
- Open the Serial Monitor.
- Wait for the LED to blink.
- Now type T [Enter] to print a string or any symbol key to print the ASCII value

Suddenly Hello World gets printed as well as "Text" or some number.

I tested the same on the Nano 33 IoT which also uses the on board USB peripheral but a different USB stack and not mbed OS. There everything works as expected. As soons as the Serial Monitor is connected Hello World gets printed.

Code: [Select]

int led = 13;

void setup()
{
 pinMode( led, OUTPUT );

 Serial.begin( 115200 );
 //  delay( 5000 ); // Wait for serial to wake up. Not understood why it's needed
 while ( !Serial )
   ;  // Do nothing

 Serial.println( "Hello world" );
}

void loop()
{
 while ( Serial.available() > 0 )
 {
   int incomingByte = Serial.read();

   if ( incomingByte == 'T' )
   {
     Serial.println( "Text" );
   }
   else
   {
     Serial.println( incomingByte );
   }

 }

 digitalWrite( led, HIGH );
 delay( 100 );
 digitalWrite( led, LOW );
 delay( 500 );
}

Grumpy_Mike

Try this
Code: [Select]
Serial.begin( 115200 );
 while ( !Serial ) delay(10);
     

wildbill

I should have one in a few hours. I can try it then.

wildbill

I tried your version Mike, but it gives no output at all, just like my original version without the delay. I put back a version with the delay and it did produce serial output.

I'm not inclined to test further though because in the thirty minutes I've been testing, the upload process has hard crashed my Mac twice.

Grumpy_Mike

This is odd because it works in my Mac and nano 33 BLE combination.

Go Up