Article # 143, added by Geoworks, historical record
| first | previous | index | next | last |

Timeout with SerialRead(STREAM_BLOCK)



SerialRead by itself can only block or not block. You will need
to create the block with timeout functionality yourself. This is 
fairly easy to do. Simply create a routine that checks the serial
port then sleeps for a bit, then checks again.  

For example:

StreamError
SerialReadTimeout( Handle driver,
                   StreamToken stream,
                   word timeout,
                   word buffSize,
                   byte *buffer,
                   word *numBytesRead )
{
    word bytesAvail;

    /*
     * loop until the reqested number of bytes is 
     * available or timeout is reached (timeout==0).
     */
    SerialQuery( driver, stream, STREAM_ROLES_READER, &bytesAvail );
    while ( ( bytesAvail < *numBytesRead ) && ( --timeout ) ) {
        if ( bytesAvail < *numBytesRead ) {
            /* sleep one tick (1/60 of second) */
            ThreadSleep( 1 );
        }
        SerialQuery( driver, stream, STREAM_ROLES_READER, &bytesAvail );
    };

    /*
     * at this point, we've either timed out or the
     * requested number of bytes are waiting for us.
     * if timeout, then return a TIMEOUT error (0xff)
     * otherwise call SerialRead().
     */
    if ( timeout == 0 ) {
        /* arbitrarily chose 0xFF to represent timeout on return.
         * It could be whatever you want, though */
        return 0xFF;  
    }
    else {
        return( SerialRead( driver, stream, STREAM_NO_BLOCK,
                            buffSize, buffer, numBytesRead ) );
    }
}