From bde339cab6b764069686263bf7835ca508c30ab7 Mon Sep 17 00:00:00 2001 From: JC-000 <3798556+JC-000@users.noreply.github.com> Date: Thu, 12 Nov 2026 08:13:15 -0600 Subject: [PATCH] Retry the UDP read until the NTP reply arrives The program sent its NTP request and then did a single socket READ. That only works if the reply is already queued when the READ runs. Newer Ultimate firmware answers an empty READ at once (status 02, count -1), so the program stopped with no data. Ask again until data arrives or 5 seconds have passed on clock(), and report "No reply" on timeout. Co-Authored-By: Claude Sonnet 5.5 --- u-time.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/u-time.c b/u-time.c index b00b24c..df54972 100644 --- a/u-time.c +++ b/u-time.c @@ -15,6 +15,7 @@ #include "./ultimate-dos-lib/src/lib/ultimate_lib.h" #define NTP_TIMESTAMP_DELTA 2208988800ul //0x83AA7E80 +#define NTP_TIMEOUT_SECONDS 5 /* //the data in the ntp packet typedef struct { // https://github.com/lettier/ntpclient/blob/master/source/c/main.c unsigned char li_vn_mode; // Eight bits. li, vn, and mode. @@ -64,6 +65,8 @@ void main(void) 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; unsigned char socket = 0; time_t t; + clock_t start; + int got; char res[32]; uii_get_time(); printf("\nUltimate datetime: %s", uii_data); @@ -81,9 +84,19 @@ void main(void) if(CheckStatus()) return; printf("\nReading result"); - uii_socketread(socket, 50);// 2 + sizeof( ntp_packet )); - if(CheckStatus()) return; + // The reply may not have arrived yet. An empty READ answers at once with + // status 02 and count -1, so ask again until data arrives or the time + // limit is up. Any other status that is not 00 is a real error. + start = clock(); + do { + got = uii_socketread(socket, 50);// 2 + sizeof( ntp_packet )); + if((uii_status[0] != '0' || uii_status[1] != '2') && CheckStatus()) return; + } while (got <= 0 && (clock() - start) < NTP_TIMEOUT_SECONDS * CLOCKS_PER_SEC); uii_socketclose(socket); + if (got < 38) { + printf("\nNo reply from %s", host); + return; + } t = uii_data[37] | (((unsigned long)uii_data[36])<<8)| (((unsigned long)uii_data[35])<<16)| (((unsigned long)uii_data[34])<<24); t -= NTP_TIMESTAMP_DELTA;