From d5231ec0da6a3f5af1644fd98007602ac3f914f3 Mon Sep 17 00:00:00 2001 From: Chuck Ritola Date: Tue, 13 Jul 2021 00:07:23 -0400 Subject: [PATCH] Use loop to ensure RS-HFIQ hears first command. Closes #730 --- rigs/kit/rs_hfiq.c | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/rigs/kit/rs_hfiq.c b/rigs/kit/rs_hfiq.c index 42921180e..2e677e4fc 100644 --- a/rigs/kit/rs_hfiq.c +++ b/rigs/kit/rs_hfiq.c @@ -36,6 +36,7 @@ #include "hamlib/rig.h" #include "serial.h" +#define RSHFIQ_INIT_RETRY 5 static int rshfiq_open(RIG *rig) { @@ -79,37 +80,29 @@ static int rshfiq_open(RIG *rig) } } - rig_flush(&rig->state.rigport); - - snprintf(versionstr, sizeof(versionstr), "*w\r"); - rig_debug(RIG_DEBUG_TRACE, "%s: cmdstr = %s\n", __func__, versionstr); - retval = write_block(&rig->state.rigport, versionstr, strlen(versionstr)); - - if (retval != RIG_OK) + //There is a delay between when the port is open and the RS-HFIQ can receive and respond. + //Make a few attempts at getting the version string just in case the RS-HFIQ has to catch up first. + retval = -RIG_ETIMEOUT; + for (int init_retry_count = 0; (init_retry_count < RSHFIQ_INIT_RETRY) && (retval == -RIG_ETIMEOUT); init_retry_count++) { - return retval; - } - - retval = read_string(&rig->state.rigport, versionstr, 20, stopset, 2); - - if (retval <= 0) - { - // Just one retry if the arduino is just booting + rig_flush(&rig->state.rigport); + snprintf(versionstr, sizeof(versionstr), "*w\r"); + rig_debug(RIG_DEBUG_TRACE, "%s: cmdstr = %s\n", __func__, versionstr); retval = write_block(&rig->state.rigport, versionstr, strlen(versionstr)); - if (retval != RIG_OK) { return retval; } - retval = read_string(&rig->state.rigport, versionstr, 20, stopset, 2); - - if (retval <= 0) - { - return retval; - } } + if (retval <= 0) + { + return retval; + } + + rig_flush(&rig->state.rigport); + versionstr[retval] = 0; rig_debug(RIG_DEBUG_TRACE, "%s: Rigversion = %s\n", __func__, versionstr);