makes sense in the video? Isn't Serial always different from 0, because it's a static object that has a non-zero address? even if Serial.begin(9600) fails?
makes sense in the video? Isn't Serial always different from 0, because it's a static object that has a non-zero address? even if Serial.begin(9600) fails?
Jan Cumps Apparently, a reference to Serial in an if or while argument context evaluates to true if the specified serial port is available else there is a permanent/temporary error. There must be something hidden in the compilation/interpreter that treats Serial special. An if( Serial.isConnected() ) would be better. Secrets are bad engineering practices but what can you do? I cannot find any documentation of the static Serial object other than what's in the arduino.cc API. Have you?
To guard against a single spurious failure instance, I use this in setup() instead of the if-statement for Serial applications:
int count_down = 3; while( ! Serial ) { // Sleep 1 second if not yet ready. delay( 1000 ); // Tried the maximum allowed times already? if( count_down-- < 1 ) { // Tried 3 seconds to get the serial connection ready - giving up. // Timeout error processing goes here } }
Jan Cumps : @richard_elkins is right. Actually I have only demonstrated how to use that. It's up to the user to put logic whatever he wants like he might reboot or he might stop execution or wait for certain time etc.
So it looks like the bool operator is indeed overridden for the class HardwareSerial (also checked SoftwareSerial, it's the same mechanism there), but it always returns true, whatever the state of the connection
Top Comments
-
Jan Cumps
-
Cancel
-
Vote Up
0
Vote Down
-
-
Sign in to reply
-
More
-
Cancel
Comment-
Jan Cumps
-
Cancel
-
Vote Up
0
Vote Down
-
-
Sign in to reply
-
More
-
Cancel
Children