From d049b90aa9041ea6821f88ce92617425e9efffad Mon Sep 17 00:00:00 2001 From: torque Date: Tue, 22 Aug 2023 17:24:29 -0700 Subject: [PATCH] spid: flush serial input buffer before command send This seems to take care of the log data pretty much entirely and is possibly a much simpler alternative solution to the previous two commits. However, for full robustness, I think it makes sense to keep all three of these changes together. Also, it's entirely possible that this approach introduces a performance regression: I haven't particularly looked at how the buffer flushing is implemented, but if it ends up doing looped reads with a timeout, this could slow down command processing for the SPID object significantly. Since I've only tested this through the command line interface, I have not taken a close look at performance. --- rotators/spid/spid.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/rotators/spid/spid.c b/rotators/spid/spid.c index 7c4100bd7..c2f68a9a7 100644 --- a/rotators/spid/spid.c +++ b/rotators/spid/spid.c @@ -168,6 +168,14 @@ static int read_r2p_frame(hamlib_port_t *port, unsigned char *rxbuffer, } } +static int spid_write(hamlib_port_t *p, const unsigned char *txbuffer, + size_t count) +{ + int ret = rig_flush(p); + if (ret < 0) return ret; + return write_block(p, txbuffer, count); +} + static int spid_rot_init(ROT *rot) { rig_debug(RIG_DEBUG_TRACE, "%s called\n", __func__); @@ -310,7 +318,7 @@ static int spid_rot1prog_rot_set_position(ROT *rot, azimuth_t az, cmdstr[11] = 0x2F; /* K */ cmdstr[12] = 0x20; /* END */ - retval = write_block(&rs->rotport, (unsigned char *) cmdstr, 13); + retval = spid_write(&rs->rotport, (unsigned char *) cmdstr, 13); if (retval != RIG_OK) { @@ -337,7 +345,7 @@ static int spid_rot2prog_rot_set_position(ROT *rot, azimuth_t az, { do { - retval = write_block(&rs->rotport, + retval = spid_write(&rs->rotport, (unsigned char *) "\x57\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1F\x20", 13); if (retval != RIG_OK) @@ -378,7 +386,7 @@ static int spid_rot2prog_rot_set_position(ROT *rot, azimuth_t az, cmdstr[11] = 0x2F; /* K */ cmdstr[12] = 0x20; /* END */ - retval = write_block(&rs->rotport, (unsigned char *) cmdstr, 13); + retval = spid_write(&rs->rotport, (unsigned char *) cmdstr, 13); if (retval != RIG_OK) { @@ -412,7 +420,7 @@ static int spid_rot_get_position(ROT *rot, azimuth_t *az, elevation_t *el) do { - retval = write_block(&rs->rotport, + retval = spid_write(&rs->rotport, (unsigned char *) "\x57\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1F\x20", 13); if (retval != RIG_OK) @@ -484,7 +492,7 @@ static int spid_rot_stop(ROT *rot) do { - retval = write_block(&rs->rotport, + retval = spid_write(&rs->rotport, (unsigned char *) "\x57\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0F\x20", 13); if (retval != RIG_OK) @@ -559,7 +567,7 @@ static int spid_md01_rot2prog_rot_move(ROT *rot, int direction, int speed) moving at all), always send the stop command first. */ spid_rot_stop(rot); - retval = write_block(&rs->rotport, (unsigned char *) cmdstr, 13); + retval = spid_write(&rs->rotport, (unsigned char *) cmdstr, 13); return retval; }