/* Function: serialDataFlush =================================================
 * Abstract:
 *  Utility function to flush a port
 */
static int serialDataFlush(
    SerialCommsData *sd)
{
    char tmpBuf[TMP_BUF_SIZ];
    size_t numRecvd = 0;
    int pending = 0;
    int error;
    static const char *fnName = "serialDataFlush:";

    do {
        error = serialDataPending( sd, &pending);
        if ( (pending > 0) && (error==RTIOSTREAM_NO_ERROR) ) {
            if (sd->verbosity) {
                printf("serialDataFlush: pending = %d\n", pending);
            }
            error = serialDataGet( sd, tmpBuf, sizeof( tmpBuf), &numRecvd);
            if (sd->verbosity) {
                size_t currElement;
                printf("serialDataFlush: sizeRecvd = %lu: ", (unsigned long) numRecvd);
                for (currElement = 0; currElement < numRecvd; currElement++) {
                    printf("%u ", (unsigned char) tmpBuf[currElement]);
                }
                printf("\n");
            }
        }
    }  while (  (pending > 0) && (error==RTIOSTREAM_NO_ERROR) && numRecvd > 0);
    if (error == RTIOSTREAM_ERROR) {
        printf( "%s Flushing returned RTIOSTREAM_ERROR\n", fnName);
    }
    return error;
}
/* Function: serialDataGet =====================================================
 * Abstract:
 *  Attempts to gets the specified number of bytes from the specified serial.
 *  The number of bytes read is returned via the 'sizeRecvd' parameter.
 *  RTIOSTREAM_NO_ERROR is returned on success, RTIOSTREAM_ERROR is returned on
 *  failure.
 *
 * NOTES:
 *  o it is not an error for 'sizeRecvd' to be returned as 0
 *  o this function waits for at most READ_FILE_TIMEOUT
 */
static int serialDataGet(
    SerialCommsData *sd,
    char          *dst,
    const size_t   size,
    size_t        *sizeRecvd)
{
    static const char *fnName = "serialDataGet:";
    int retVal = RTIOSTREAM_NO_ERROR;
    int avail = 0;

#ifdef _WIN32
    DWORD sizeRecvdTemp = 0;
    assert( sd->serialHandle != NULL);
#else
    ssize_t sizeRecvdTemp = 0;
    assert(sd->serialHandle >= 0);
#endif

    *sizeRecvd = 0;
    if (size == 0) {
        /* return immediately if caller requested to read 0 bytes */
        return retVal;
    }

    retVal = serialDataPending( sd, &avail);
    if (retVal == RTIOSTREAM_ERROR || avail == 0) {
        if (retVal == RTIOSTREAM_ERROR) {
            printf( "%s Pending returned RTIOSTREAM_ERROR\n", fnName);
        }
        return retVal;
    }

#ifdef _WIN32
    if (!ReadFile( sd->serialHandle, dst, (DWORD) size, &sizeRecvdTemp, NULL))/*Error Condition check*/
#else /*UNIX*/
    sizeRecvdTemp = read(sd->serialHandle,dst,size);
    if(sizeRecvdTemp < 0) /*Error Condition check*/
#endif

    {
        printf( "%s ReadFile returned ERROR\n", fnName);
        retVal = RTIOSTREAM_ERROR;
        return retVal;
    }

    *sizeRecvd = (size_t) sizeRecvdTemp;

    return retVal;
} /* end serialDataGet */
Exemplo n.º 3
0
/* Function: serialDataFlush =================================================
 * Abstract:
 *  Utility function to flush a port
 */
static int serialDataFlush( 
    SerialCommsData *sd)
{
    char tmpBuf[TMP_BUF_SIZ];
    int numRecvd;
    int pending = 0;
    int error;
    static const char *fnName = "serialDataFlush:";

    do {
        error = serialDataPending( sd, &pending);

        if ( (pending > 0) && (error==RTIOSTREAM_NO_ERROR) ) {
            error = serialDataGet( sd, tmpBuf, sizeof( tmpBuf), (size_t *) (&numRecvd));
        }
    }  while (  (pending > 0) && (error==RTIOSTREAM_NO_ERROR) && numRecvd > 0);
    if (error == RTIOSTREAM_ERROR) {
        printf( "%s Flushing returned RTIOSTREAM_ERROR\n", fnName);
    }
    return error;
}
Exemplo n.º 4
0
/* Function: serialDataGet =====================================================
 * Abstract:
 *  Attempts to gets the specified number of bytes from the specified serial.
 *  The number of bytes read is returned via the 'sizeRecvd' parameter.
 *  RTIOSTREAM_NO_ERROR is returned on success, RTIOSTREAM_ERROR is returned on
 *  failure.
 *
 * NOTES:
 *  o it is not an error for 'sizeRecvd' to be returned as 0
 *  o this function does NOT block if no data is available
 */
static int serialDataGet(
    SerialCommsData *sd,
    char          *dst,
    const size_t   size,
    size_t        *sizeRecvd)
{
    static const char *fnName = "serialDataGet:";
    int nRead;
    int retVal = RTIOSTREAM_NO_ERROR;
    int avail;

    *sizeRecvd = 0;     /* init for safety */

    assert( sd->serialHandle != NULL);

    retVal = serialDataPending( sd, &avail);

    if (retVal == RTIOSTREAM_ERROR || avail == 0) {
        if (retVal == RTIOSTREAM_ERROR) {
            printf( "%s Pending returned RTIOSTREAM_ERROR\n", fnName);
        }
        return retVal;
    }

    SetCommTimeouts( sd->serialHandle, &cto_timeout );

    if (!ReadFile( sd->serialHandle, dst, (DWORD) size, &nRead, NULL)) {
        printf( "%s ReadFile returned ERROR\n", fnName);
        retVal = RTIOSTREAM_ERROR;
        return retVal;
    }

    *sizeRecvd = nRead;

    return retVal;
} /* end serialDataGet */