From d453ab3d1946631eaddd166249ecf1e504d7834e Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Sat, 18 Oct 2025 14:10:42 -0400 Subject: [PATCH] serialqueue: Simplify command_event() There's no reason to attempt to handle multiple buffer transmissions in a single command_event() call. Handle the transmit case outside of the command building loop. If data is transmitted, then get a new timestamp from the pollreactor and retry before sleeping. Signed-off-by: Kevin O'Connor --- klippy/chelper/serialqueue.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/klippy/chelper/serialqueue.c b/klippy/chelper/serialqueue.c index 2c941d4c8..ed5dafdc3 100644 --- a/klippy/chelper/serialqueue.c +++ b/klippy/chelper/serialqueue.c @@ -1,6 +1,6 @@ // Serial port command queuing // -// Copyright (C) 2016-2021 Kevin O'Connor +// Copyright (C) 2016-2025 Kevin O'Connor // // This file may be distributed under the terms of the GNU GPLv3 license. @@ -626,20 +626,19 @@ command_event(struct serialqueue *sq, double eventtime) double waketime; for (;;) { waketime = check_send_command(sq, buflen, eventtime); - if (waketime != PR_NOW || buflen + MESSAGE_MAX > sizeof(buf)) { - if (buflen) { - // Write message blocks - do_write(sq, buf, buflen); - sq->bytes_write += buflen; - double idletime = (eventtime > sq->idle_time - ? eventtime : sq->idle_time); - sq->idle_time = idletime + calculate_bittime(sq, buflen); - buflen = 0; - } - if (waketime != PR_NOW) - break; - } + if (waketime != PR_NOW) + break; buflen += build_and_send_command(sq, &buf[buflen], buflen, eventtime); + if (buflen + MESSAGE_MAX > sizeof(buf)) + break; + } + if (buflen) { + // Write message blocks + do_write(sq, buf, buflen); + sq->bytes_write += buflen; + double idletime = eventtime > sq->idle_time ? eventtime : sq->idle_time; + sq->idle_time = idletime + calculate_bittime(sq, buflen); + waketime = PR_NOW; } pthread_mutex_unlock(&sq->lock); return waketime;