From e61e1fb8e8aeaadfae274f01cb261e5c2a818492 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Fillod=2C=20F8CFE?= Date: Sun, 8 Oct 2000 21:46:09 +0000 Subject: [PATCH] added fread_block for file buffered access git-svn-id: https://hamlib.svn.sourceforge.net/svnroot/hamlib/trunk@197 7ae35d74-ebe9-4afe-98af-79ac388436b8 --- src/serial.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/src/serial.c b/src/serial.c index a27c72fe6..ed05a4af6 100644 --- a/src/serial.c +++ b/src/serial.c @@ -6,7 +6,7 @@ * Provides useful routines for read/write serial data for communicating * via serial interface. * - * $Id: serial.c,v 1.1 2000-10-01 14:24:47 f4cfe Exp $ + * $Id: serial.c,v 1.2 2000-10-08 21:46:09 f4cfe Exp $ * * * This program is free software; you can redistribute it and/or @@ -37,7 +37,7 @@ #include #include -#include +#include #include "serial.h" #include "misc.h" @@ -405,3 +405,65 @@ int read_block(int fd, unsigned char *rxbuffer, size_t count, int timeout ) return total_count; /* return bytes count read */ } +int fread_block(FILE *stream, unsigned char *rxbuffer, size_t count, int timeout ) +{ + fd_set rfds; + struct timeval tv, tv_timeout; + int rd_count, total_count = 0; + int retval; + int fd; + + fd = fileno(stream); + + FD_ZERO(&rfds); + FD_SET(fd, &rfds); + + /* + * Wait up to timeout ms. + */ + tv_timeout.tv_sec = timeout/1000; + tv_timeout.tv_usec = (timeout%1000)*1000; + + + /* + * grab bytes from the rig + * The file descriptor must have been set up non blocking. + */ + rd_count = fread(rxbuffer, 1, count, stream); + if (rd_count < 0) { + rig_debug(RIG_DEBUG_ERR, "read_block: read failed - %s\n", + strerror(errno)); + return -RIG_EIO; + } + total_count += rd_count; + count -= rd_count; + + while (count > 0) { + tv = tv_timeout; /* select may have updated it */ + + retval = select(fd+1, &rfds, NULL, NULL, &tv); + if (!retval) { + rig_debug(RIG_DEBUG_ERR,"rig timeout after %d chars or select error - %s!\n", + total_count, strerror(errno)); + return -RIG_ETIMEOUT; + } + + /* + * grab bytes from the rig + * The file descriptor must have been set up non blocking. + */ + rd_count = fread(rxbuffer+total_count, 1, count, stream); + if (rd_count < 0) { + rig_debug(RIG_DEBUG_ERR, "read_block: read failed - %s\n", + strerror(errno)); + return -RIG_EIO; + } + total_count += rd_count; + count -= rd_count; + } + + rig_debug(RIG_DEBUG_TRACE,"RX %d bytes\n",total_count); + dump_hex(rxbuffer, total_count); + return total_count; /* return bytes count read */ +} +