From 8500754b995df5f226e09e98d2d6dff321ca0e6f Mon Sep 17 00:00:00 2001 From: Mark Haslinghuis Date: Wed, 11 Jun 2025 00:41:38 +0200 Subject: [PATCH] Improve timeout optimization --- src/js/msp.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/js/msp.js b/src/js/msp.js index 32f17217..586aad7c 100644 --- a/src/js/msp.js +++ b/src/js/msp.js @@ -404,14 +404,16 @@ const MSP = { // Send message if it has data or is a new request if (data || !isDuplicateRequest) { - // Optimize timeout for frequent requests - if (this.timeout > this.MIN_TIMEOUT) { - this.timeout--; - } - + // Simple adaptive timeout - decrease on success, increase on timeout serial.send(bufferOut, (sendInfo) => { - if (sendInfo.bytesSent === bufferOut.byteLength && callback_sent) { - callback_sent(); + if (sendInfo.bytesSent === bufferOut.byteLength) { + // Success: gradually decrease timeout for faster response + if (this.timeout > this.MIN_TIMEOUT) { + this.timeout = Math.max(this.MIN_TIMEOUT, this.timeout - 5); + } + if (callback_sent) { + callback_sent(); + } } }); } @@ -426,6 +428,9 @@ const MSP = { }, _handleTimeout(requestObj, bufferOut) { + // Increase timeout on failure for better reliability + this.timeout = Math.min(this.MAX_TIMEOUT, this.timeout + 50); + // Increment retry attempts requestObj.attempts++;