From 181169f5990036f09ff474ab4edf1994e990b0e7 Mon Sep 17 00:00:00 2001 From: neekBruh <42756461+neekBruh@users.noreply.github.com> Date: Tue, 18 Nov 2025 23:22:42 -0300 Subject: [PATCH 1/2] Fix --get-archive=arc not waiting for ACK After the DMPAFT command has been sent and the ACK has been received, the driver was sending the date and was waiting for the ACK only for a few CPU cycles, not enough for the davis to process the request. A separate ReadBufferWithTimeout function was added to block the execution and wait for the incomming bytes before timing out. --- main.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 83 insertions(+), 13 deletions(-) mode change 100644 => 100755 main.c diff --git a/main.c b/main.c old mode 100644 new mode 100755 index d5b28d7..5df2880 --- a/main.c +++ b/main.c @@ -373,12 +373,16 @@ int main(int argc, char *argv[]) } - + /* + * int runCommand(char* command, int commandLength, int expectedLength, char* dataLabel, bool expectingAck, bool expectingCrc) + * */ if (bArchive) { if (runCommand("DMPAFT\n", -1, 0, "archive download initiation", true, false)) { + exit(2); } + printf("Sending time for archive download\n"); datetimeString[6] = '\n'; if (runCommand(datetimeString, 6, 6, "archive download initiation", true, true)) { exit(2); @@ -542,18 +546,28 @@ int GetParms(int argc, char *argv[]) if (optarg == NULL) { archiveRecords = 0; } else { - // Try and parse a full date time string - if (strptime(optarg, "%Y-%m-%dT%H:%M", archiveTime) == NULL - && strptime(optarg, "%Y-%m-%d", archiveTime) == NULL) { - free(archiveTime); - archiveTime = NULL; - archiveRecords = strtol(optarg, &endptr, 10); - if (*endptr != '\0' || endptr == optarg || archiveRecords < 0) { - fprintf(stderr, "vproweather: Illegal date or number of archive records to download specified.\n"); - return 0; - } - } + char* lastChar1; + char* lastChar2; + + + lastChar1 = strptime(optarg, "%Y-%m-%dT%H:%M", archiveTime); + lastChar2 = strptime(optarg, "%Y-%m-%d", archiveTime); + + + // Try and parse a full date time string + if (lastChar1 == NULL && lastChar2 == NULL) { + free(archiveTime); + archiveTime = NULL; + archiveRecords = strtol(optarg, &endptr, 10); + + if (*endptr != '\0' || endptr == optarg || archiveRecords < 0) { + fprintf(stderr, "vproweather: Illegal date or number of archive records to download specified.\n"); + return 0; + } + } + + MakeVantageDatetime(archiveTime, datetimeString); GenerateCRC(4, datetimeString, &datetimeString[4]); @@ -699,6 +713,54 @@ int ReadToBuffer(int nfd, char *pszBuffer, int nBufSize) } +/** + * Reads up to nBufSize bytes or until no new data arrives for timeout_ms. + * Returns number of bytes read, or -1 on error. + */ +int ReadToBufferTimeout(int nfd, char *pszBuffer, int nBufSize, int timeout_ms) +{ + int nPos = 0; + + while (nPos < nBufSize) { + fd_set rfds; + struct timeval tv; + int ret; + + FD_ZERO(&rfds); + FD_SET(nfd, &rfds); + + tv.tv_sec = timeout_ms / 1000; + tv.tv_usec = (timeout_ms % 1000) * 1000; + + /* Wait for data or timeout */ + ret = select(nfd + 1, &rfds, NULL, NULL, &tv); + if (ret < 0) { + perror("vproweather: select() failed"); + return -1; + } + if (ret == 0) { + /* timeout: no more characters within timeout_ms */ + break; + } + + /* Data is available */ + int nRead = read(nfd, pszBuffer + nPos, nBufSize - nPos); + if (nRead < 0) { + perror("vproweather: Problem reading serial device."); + return -1; + } + if (nRead == 0) { + /* Shouldn't usually happen for a serial fd, but just in case */ + break; + } + + nPos += nRead; + } + + return nPos; +} + + /** @@ -750,7 +812,15 @@ int runCommand(char* command, int commandLength, int expectedLength, char* dataL exit(2); } tcdrain(fdser); - nCnt = ReadToBuffer(fdser, szSerBuffer, sizeof(szSerBuffer)); + //nCnt = ReadToBuffer(fdser, szSerBuffer, sizeof(szSerBuffer)); + + /* Wait up to (yDelay * 100 ms) for data to stop arriving */ + int timeout_ms = yDelay * 100; /* keep same semantics as your VTIME*/ + nCnt = ReadToBufferTimeout(fdser, szSerBuffer, sizeof(szSerBuffer), timeout_ms); + if (nCnt < 0) { + return -1; + } + if (expectingAck) { totalLength = totalLength + 1; From 1e770f44b229b94e948a892e7fe6f92f3eb0fb2d Mon Sep 17 00:00:00 2001 From: neekBruh <42756461+neekBruh@users.noreply.github.com> Date: Tue, 18 Nov 2025 23:53:12 -0300 Subject: [PATCH 2/2] Move print inside bArchive branch to verbose only --- main.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/main.c b/main.c index 5df2880..afa7cef 100755 --- a/main.c +++ b/main.c @@ -382,7 +382,9 @@ int main(int argc, char *argv[]) exit(2); } - printf("Sending time for archive download\n"); + if (bVerbose) { + printf("Sending time for archive download\n"); + } datetimeString[6] = '\n'; if (runCommand(datetimeString, 6, 6, "archive download initiation", true, true)) { exit(2);