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

PComm::Read ( char *  submesg  )  [private]

Internal function to read from the socket.

Parameters:
submesg a character string long enough to hold data
It reset's the read counter, and loopbreaker. The read counter keeps track of the number of bytes read in. The loop breaker makes sure that read doesnt get stuck reading New Lines from the radio for ever and ever. Since we are ignoring newlines when we read. The radio has a tendency of sending newlines even when it has nothing to say.

it executes read() into the internal variable read buffer, and increments the loop breaker. If the number of bytes read is greater than 1, then some useful data was read in... no need to re-loop. When good data is read, it is copied into the submesg string from the read buffer. Select() will determine if the socket in FDSet is ready to read data.

Returns:
If after five times, the read() read in anything less than 1, then break, and set errRead to true, while returning the number of bytes read. If after five seconds fd says that it's still not ready, then it will return, setting the read counter to a negative value.
See also:
Write()

Definition at line 416 of file pcomm.cpp.

References countCall(), errRead, fd, FDSet, readBuf, readCtr, and timeOut.

Referenced by PCHear().

{
    readCtr=1;
    int loopBreaker=0;

#ifdef DEBUG_VER_2
    assert(submesg!=0);
    fprintf(stderr, "PComm: READ");
#endif /* DEBUG_VER_ */

    bzero(readBuf, sizeof(readBuf));
    bzero(submesg, sizeof(submesg));

    while((readCtr==1) && (loopBreaker<10)) {
      if (select(fd+1, FDSet, NULL, NULL, timeOut)) {
            readCtr=read(fd, readBuf, sizeof(readBuf));
            // call Count keeps track of the number of
            // read()/writes() to the serial object
                countCall();
            loopBreaker++;
      } else {
#ifdef DEBUG_VER_
            fprintf(stderr, "Read() Time Out on socket %d\n", fd);
#endif // DEBUG_VER_ //
                readCtr=1;
            errRead=true;
            loopBreaker++;
      }
    }

    /* 
       Was there an error reading? ie: after five
       loops, did we still read in new lines (in other
       words, did the radio have nothing to tell us?)
       Then we should have never been called, and set
       errRead appropriately.
     */
    (readCtr==1)?(errRead=true):(errRead=false);

    strcpy(submesg, readBuf);

    #ifdef DEBUG_VER_2
    fprintf(stderr, "\nRead: %i -  %s\n", readCtr, submesg);
    #endif /* DEBUG_VER_ */

    return readCtr;

}


Generated by  Doxygen 1.6.0   Back to index