If (Serial.avaialble() ) never becomes true

bossredman

Well-known member
I've been adding a RTC to my Temp Sensor project.

I pulled code from the various availbe example sketches.

I have the following in the main Void Loop()

Code:
if (Serial.available()) 
  {
    Serial.println("Serial Available");
    time_t t = processSyncMessage();
    
    if (t != 0) 
    {
      Teensy3Clock.set(t); // set the RTC
      setTime(t);
      Serial.println("Re-sync'ing RTC");
    }
  }

Yet I've just noticed that the Serial Prints never run.
So I'm guessing the Serial Port is literally NOT available.
Would that be correct.?
If Yes - then what could be possible reasons for this pls?

I have a
Code:
Serial.begin(115200);
statment in my Void Setup & am also able to view many other Serial Print items in the Serial monitor.
 
Serial.available() returns the number of characters in the rx-buffer.
If it is zero you just sent nothing :)


Perhaps you want something like ... if (Serial) ...
 
OK.

So ref the RTC re-sync code snip I included in the OP?

What is this trying to achieve?
Will it only resync teh RTC if I "send" something on the Serial port?
 
I've been adding a RTC to my Temp Sensor project.

I pulled code from the various availbe example sketches.

I have the following in the main Void Loop()

Code:
if (Serial.available()) 
  {
    Serial.println("Serial Available");
    time_t t = processSyncMessage();
    
    if (t != 0) 
    {
      Teensy3Clock.set(t); // set the RTC
      setTime(t);
      Serial.println("Re-sync'ing RTC");
    }
  }

You should change your code to:-

Code:
if (Serial()) 
  {
    Serial.println("Serial Available");
    time_t t = processSyncMessage();
    
    if (t != 0) 
    {
      Teensy3Clock.set(t); // set the RTC
      setTime(t);
      Serial.println("Re-sync'ing RTC");
    }
  }

if(Serial()) returns whether a serial connection is open. You should also put your code in "setup" otherwise it will keep on running every time around "loop".
 
You should also put your code in "setup"

Maybe not such good advice.

The setup() function runs only once at startup, usually before your PC is even able to complete detecting which type of USB you've just plugged in. If you only check for an incoming message with info about the current time at that very early moment, before the USB port is even up and running, you'll never be able to receive the message.

Checking "if (Serial)" tells only whether the USB connection is only and a program on your PC has opened the port. In setup() that will pretty much always be false, since the drivers on all PCs take hundreds of milliseconds to query newly connected USB devices.

You really should be checking Serial.available(), which returns a non-zero number when your PC has actually transmitted data.
 
Yes but why would you want to constantly update the time every time you go round "loop". If in "setup" you check that Serial is up with something like
Code:
     Serial.begin(9600);
     while ( !Serial && (millis() < 3000)) ;

then follow with the code to synch/set time. This is then only done once.
Code:
    Serial.println("Serial Available");
    time_t t = processSyncMessage();
    
    if (t != 0) 
    {
      Teensy3Clock.set(t); // set the RTC
      setTime(t);
      Serial.println("Re-sync'ing RTC");
    }
 
Personally I'm not a fan of treating int's implicitly as booleans as its a C-specific hack, so I'd always code:
Code:
if (Serial.available () > 0)
   ...
This then reminds you it returns an integer which is sometimes useful.
 
Back
Top