Logo Search packages:      
Sourcecode: hamlib version File versions  Download package

int HAMLIB_API read_block ( hamlib_port_t p,
char *  rxbuffer,
size_t  count 

Read bytes from an fd.

p rig port descriptor
rxbuffer buffer to receive text
count number of bytes
count of bytes received
Read "num" bytes from "fd" and put results into an array of unsigned char pointed to by "rxbuffer"

Blocks on read until timeout hits.

It then reads "num" bytes into rxbuffer.

Actually, this function has nothing specific to serial comm, it could work very well also with any file handle, like a socket.

Definition at line 345 of file iofunc.c.

References dump_hex(), hamlib_port_t::fd, rig_debug(), RIG_DEBUG_ERR, RIG_DEBUG_TRACE, RIG_DEBUG_WARN, RIG_EIO, RIG_ETIMEOUT, and hamlib_port_t::timeout.

  fd_set rfds, efds;
  struct timeval tv, tv_timeout;
  int rd_count, total_count = 0;
  int retval;

   * Wait up to timeout ms.
  tv_timeout.tv_sec = p->timeout/1000;
  tv_timeout.tv_usec = (p->timeout%1000)*1000;

  while (count > 0) {
      tv = tv_timeout;  /* select may have updated it */

      FD_SET(p->fd, &rfds);
      efds = rfds;

      retval = port_select(p, p->fd+1, &rfds, NULL, &efds, &tv);
      if (retval == 0) {
            dump_hex((unsigned char *) rxbuffer, total_count);
            rig_debug(RIG_DEBUG_WARN, "read_block: timedout after %d chars\n",
            return -RIG_ETIMEOUT;
      if (retval < 0) {
            dump_hex((unsigned char *) rxbuffer, total_count);
            rig_debug(RIG_DEBUG_ERR,"read_block: select error after %d chars: "
                                    "%s\n", total_count, strerror(errno));
            return -RIG_EIO;
      if (FD_ISSET(p->fd, &efds)) {
            rig_debug(RIG_DEBUG_ERR, "%s: fd error after %d chars\n", 
                                  __FUNCTION__, total_count);
            return -RIG_EIO;

       * grab bytes from the rig
       * The file descriptor must have been set up non blocking.
      rd_count = port_read(p, rxbuffer+total_count, count);
      if (rd_count < 0) {
            rig_debug(RIG_DEBUG_ERR, "read_block: read failed - %s\n",
            return -RIG_EIO;
      total_count += rd_count;
      count -= rd_count;

  rig_debug(RIG_DEBUG_TRACE,"RX %d bytes\n",total_count);
  dump_hex((unsigned char *) rxbuffer, total_count);
  return total_count;               /* return bytes count read */

Generated by  Doxygen 1.6.0   Back to index