Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 9 of 9

Thread: If (Serial.avaialble() ) never becomes true

  1. #1
    Senior Member
    Join Date
    Jun 2015
    Posts
    289

    If (Serial.avaialble() ) never becomes true

    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.

  2. #2
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    6,998
    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) ...

  3. #3
    Senior Member
    Join Date
    Jun 2015
    Posts
    289
    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?

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,980
    Quote Originally Posted by bossredman View Post
    Will it only resync teh RTC if I "send" something on the Serial port?
    Yes. It's expecting this Processing program on your PC to send a message with the current time.

    https://github.com/PaulStoffregen/Ti...duinoClock.pde

  5. #5
    Member
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    90
    [QUOTE=bossredman;257707]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".

  6. #6
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,980
    Quote Originally Posted by BriComp View Post
    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.

  7. #7
    Member
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    90
    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");
        }

  8. #8
    Senior Member
    Join Date
    Jul 2020
    Posts
    459
    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.

  9. #9
    Senior Member
    Join Date
    Jun 2015
    Posts
    289
    Thanks for all the responses.
    I now understand what's happening and why.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •