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

int HAMLIB_API read_string ( hamlib_port_t p,
char *  rxbuffer,
size_t  rxmax,
const char *  stopset,
int  stopset_len 

Read a string from an fd.

p Hamlib port descriptor
rxbuffer buffer to receive string
rxmax maximum string size + 1
stopset string of recognized end of string characters
stopset_len length of stopset
number of characters read if the operation has been sucessful, otherwise a negative value if an error occured (in which case, cause is set appropriately).
Read a string from "fd" and put result into an array of unsigned char pointed to by "rxbuffer"

Blocks on read until timeout hits.

It then reads characters until one of the characters in "stopset" is found, or until "rxmax-1" characters was copied into rxbuffer. String termination character is added at the end.

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

Assumes rxbuffer!=NULL

Definition at line 430 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.

Referenced by tt565_transaction().

  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 (total_count < rxmax-1) {
      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)    /* Timed out */

      if (retval < 0) {
            dump_hex((unsigned char *) rxbuffer, total_count);
            rig_debug(RIG_DEBUG_ERR, "%s: select error after %d chars: %s\n", 
                                  __FUNCTION__, 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;

             * read 1 character from the rig, (check if in stop set)
       * The file descriptor must have been set up non blocking.
        rd_count = port_read(p, &rxbuffer[total_count], 1);
      if (rd_count < 0) {
            dump_hex((unsigned char *) rxbuffer, total_count);
            rig_debug(RIG_DEBUG_ERR, "%s: read failed - %s\n",__FUNCTION__,
            return -RIG_EIO;
      if (stopset && memchr(stopset, rxbuffer[total_count-1], stopset_len))
   * Doesn't hurt anyway. But be aware, some binary protocols may have
   * null chars within th received buffer.
  rxbuffer[total_count] = '\000';

  if (total_count == 0) {
    rig_debug(RIG_DEBUG_WARN, "%s: timedout without reading a character\n",
    return -RIG_ETIMEOUT;

  rig_debug(RIG_DEBUG_TRACE,"RX %d characters\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