简体   繁体   中英

Explanation of code

I have written this block of code:

void NTP::setTime() {
    connWithNTP();
    if(!timeNTPReadyToSet) {
        Serial.println("Getting time from RTC");
        timeFromRTC();
        if (year == 2000) {
            while(dhcpTest == false && timeNTPReadyToSet == false) {
                Serial.print("Kolaei apo to DHCP");
                startEthernetAndUdp();
                connWithNTP();
                timeFromNTP();
            }
        }
    }
    else {
        Serial.print("Getting time from NTP");
        timeFromNTP();
    }
    serialPrinting();
}

According to what I did when everything is OK (the Internet connection is OK and NTP servers work properly). The code will go to else print that will get the time from the NTP server (that happens) and my problem is that it does not execute serialPrinting.

This function just prints time in the serial monitor. Functions timeFromNTP and timeFromRTC just set variables that contain the calendar and the time.

My serial monitor prints getting time from the NTP server and then it does not print anything. It just starts from the beginning since I have set this setTime function in a loop.

Is it possible to get serialPrinting() to run? I am concerned about serialPrinting if this function will execute either way when I call setTime().

I get this:

0

0

0

4st NTPServer working

Getting time from NTP

0

0

0

0

4st NTPServer working

Getting time from NTP

The zeros and "4th NTPServer working" comes from connWithNTP.

Well, there was no way serialPrinting not to execute except if any of other functions go into loop or stuck by calling setTime again. I had some subfunctions that it could not be seen from here and they where calling setTime for safety reasons (that now is not needed). So there was a loop. There is no way this code without loops or internal error that could skip serialPrinting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM