コード例 #1
0
static int WaitReceive( byte *err, trap_elen max_len, byte *p, unsigned timeout )
{
    unsigned wait_time;               /* timer */
    int      data;                    /* data from other machine */
    int      result;                  /* result of BlockReceive() */

    ZeroWaitCount();
    wait_time = WaitCount() + timeout;
    for( ;; ) {
        data = WaitByte( 1 );
        if( data == SDATA_STX ) {           /* STX received, get block */
            result = BlockReceive( err, max_len, p );
            if( result ) return( result );
        } else if( data == SDATA_RLR ) {    /* RLR received */
            SendByte( SDATA_NAK );          /* tell the other end to resend block */
        } else if( (timeout != FOREVER) && (WaitCount() >= wait_time) ) {
            return( FAIL );           /* time-out */
        }
    }
}
コード例 #2
0
bool MarchToTheSameDrummer( void )
{
    int got;

#ifdef SERVER
    SendByte( SDATA_LOK );
#else
    if( ( got = WaitByte( SEC( 2 ) ) ) != SDATA_LOK ) {
        return( FAIL );
    }
#endif
#ifndef SERVER
    SendByte( SDATA_ACK );
#else
    if( ( got = WaitByte( SEC(1)/2 ) ) != SDATA_ACK ) {
        return( FAIL );
    }
#endif
    Wait( 1 );  /* don't go till the timer hits exactly on the tick */
    ZeroWaitCount();
    return( SUCCESS );
}
コード例 #3
0
static int BlockReceive( byte *err, trap_elen max_len, byte *p )
{
    byte            buffer[8];     /* storing bytes other than actual data from blocks */
    trap_elen       i;             /* loop index */
    trap_elen       len;
    word            crc_val;
    unsigned        blkno;
    int             c;
    int             wait;

    ZeroWaitCount();
    BytesReceived = 0;
    wait = (MaxBaud == MIN_BAUD) ? SEC(1)/2 : SEC(1)/4;
    /* Receiving bytes before actual data (up to err byte) */
    for( i = 1; i <= 7; ++i ) {
        c = WaitByte( wait );
        if( c == SDATA_NO_DATA ) {    /* time-out error */
            LastResponse = SDATA_NAK;
            SendByte( SDATA_NAK );  /* send NAK to request resending of block */
            return( FAIL );
        }
        buffer[i] = c;
    }

    len = (buffer[MLEN+1] << 8) | buffer[MLEN];   /* number of data bytes */
    if( len > max_len ) {        /* not enough buffer space to store data */
        ClearCom();
        return( FAIL );
    }

    /* Receiving actual data bytes */
    for( i = 0; i < len; ++i ) {
        c = WaitByte( wait );
        if( c == SDATA_NO_DATA ) {    /* time-out error */
            LastResponse = SDATA_NAK;
            SendByte( SDATA_NAK );  /* send NAK to request resending of block */
            return( FAIL );
        }
        p[i] = c;
    }

    /* Receiving the last byte: ETX */
    buffer[0] = WaitByte( wait );
    if( buffer[0] != SDATA_ETX ) {    /* time-out error */
        LastResponse = SDATA_NAK;
        SendByte( SDATA_NAK );  /* send NAK to request resending of block */
        return( FAIL );
    }

    *err = buffer[MERR];            /* storing command byte */
    blkno = (buffer[MBLKNO+1] << 8) | buffer[MBLKNO];  /* blk# received */
    crc_val = (buffer[MCRC+1] << 8) | buffer[MCRC];  /* crc received */

    if( CRC( &buffer[MBLKNO], len, p ) != crc_val ) {   /* CRC error */
        LastResponse = SDATA_NAK;
        SendByte( SDATA_NAK );  /* send NAK to request resending of block */
        return( FAIL );
    }

    if( ReceiveBlkNo != blkno ) {      /* incorrect block */
        ClearCom();
        LastResponse = SDATA_ACK;
        SendByte( SDATA_ACK );
        return( FAIL );
    }

    /* Block received successfully */
    LastResponse = SDATA_ACK;
    SendByte( SDATA_ACK );
    ++ReceiveBlkNo;
    BytesReceived = len;
    return( SUCCESS );
}
コード例 #4
0
static int BlockSend( trap_elen num, byte *p, unsigned timeout )
{
    word            crc_value;          /* crc value of block */
    unsigned        wait_time;          /* timer for testing time-out */
    trap_elen       i;                  /* loop index */
    int             reply;              /* reply message from other machine */
    unsigned char   crc_low, crc_hi;
    unsigned char   len_low, len_hi;
    byte            extra[3];           /* ..[0]=blkno_low, ..[1]=blkno_hi, ..[2]=err */
    unsigned        wait;

    ZeroWaitCount();
    extra[2] = PrevErrors = Errors;
    if( Errors > 255 ) {
        extra[2] = 255;       /* because it is a char, not an int */
    }
    Errors = 0;

    ClearCom();
    /* compose send buffer contents */
    len_low = num & 0xff;              /* low 8 bits of data block length */
    len_hi  = num >> 8;                /* high 8 bits of data block length */
    extra[0] = SendBlkNo & 0xff;      /* low 8 bits of send block no */
    extra[1] = SendBlkNo >> 8;        /* high 8 bits of send block no */
    crc_value = CRC( extra, num, p );  /* calculate crc for (blk#+err+data) */
    crc_low = crc_value & 0xff;        /* low 8 bits of crc_value */
    crc_hi  = crc_value >> 8;          /* high 8 bits of crc_value */

    wait = (MaxBaud == MIN_BAUD) ? SEC(2) : SEC(1);
    for( ;; ) {                 /* send block loop */
        /* send the block */
        StartBlockTrans();
        SendByte( SDATA_STX );
        SendByte( crc_low );
        SendByte( crc_hi );
        SendByte( len_low );
        SendByte( len_hi );
        SendByte( extra[0] );      /* blkno_low */
        SendByte( extra[1] );      /* blkno_hi */
        SendByte( extra[2] );      /* err */
        for( i = 0; i < num; ++i ) {
            SendByte( p[i] );
        }
        SendByte( SDATA_ETX );
        StopBlockTrans();

        wait_time = WaitCount() + timeout;
        for( ;; ) {                 /* wait proper acknowledgement loop */
            reply = WaitByte( wait );          /* receive reply */
            if( reply == SDATA_NO_DATA ) {
                if( (timeout != FOREVER) && (WaitCount() >= wait_time) ) {
                    return( FAIL );    /* time-out */
                } else {
                    SendByte( SDATA_RLR );     /* request last response */
                    ++Errors;            /* increment error counter */
                }
            } else {
                if( reply == SDATA_ACK ) {
                    ++SendBlkNo;
                    return( SUCCESS );    /* done, exit from BlockSend() */
                } else if( reply == SDATA_NAK ) {  /* unsuccessful, re-send block */
                    ++Errors;
                    break;     /* break out of acknowledgement loop;
                                  i.e. back to send block loop */
                } else if( reply == SDATA_RLR ) {  /* request last response */
                    SendByte( LastResponse );
                    break;     /* break out ackno loop; re-send block */
                } else {       /* things are totally messed up */
                    while( WaitByte( SEC(3)/4 ) != SDATA_NO_DATA )
                        ;             /* discard all characters sent */
                    SendByte( SDATA_RLR );     /* request last response */
                    ++Errors;
                }
            }
        }
    }
}
コード例 #5
0
ファイル: sernt.c プロジェクト: ABratovic/open-watcom-v2
char *InitSys( void )
{
    DCB             devCon;
    char            deviceFileName[64];
    COMMTIMEOUTS    timeouts = { MAXDWORD, MAXDWORD, 1, 0, 0 };

    if( comPortNumber )
        sprintf( deviceFileName, "\\\\.\\COM%d", comPortNumber );
    else
        strncpy( deviceFileName, comPortName, sizeof( deviceFileName ) );

    Trace( "InitSys: '%s'\n", deviceFileName );

    currentBaudRateIndex = -1;

    ZeroWaitCount();

    hSerial = CreateFile( deviceFileName,
        GENERIC_READ | GENERIC_WRITE,
        0,
        NULL,
        OPEN_EXISTING,
        0L,
        NULL );

    if( INVALID_HANDLE_VALUE == hSerial ) {
        Trace( "InitSys: CreateFile failed '%s'\n", deviceFileName );
        return NULL;
    }

    // Set up comm parameters if it's a real port and not a pipe
    if( comPortNumber ) {
        // Set up a big RX buffer
        if( !SetupComm( hSerial, 1000, 1000 ) ) {
            // This odd circumstance seems to occur if the port has been
            // assigned to a printer
            Trace( "InitSys: Setupcom failed '%s'\n", deviceFileName );
            CloseHandle( hSerial );
            hSerial = INVALID_HANDLE_VALUE;
            return NULL;
        }
        
        // Configure the serial port
        GetCommState(hSerial, &devCon);
        
        devCon.BaudRate = 9600;
        devCon.ByteSize = 8;
        devCon.Parity = NOPARITY;
        devCon.StopBits = ONESTOPBIT;
        devCon.fParity = FALSE;
        
        devCon.fDsrSensitivity = FALSE;
        devCon.fDtrControl = FALSE;
        devCon.fRtsControl = RTS_CONTROL_DISABLE;
        devCon.fOutxCtsFlow = FALSE;
        devCon.fOutxDsrFlow = FALSE;
        devCon.fInX = FALSE;
        devCon.fOutX = FALSE;
        SetCommState(hSerial, &devCon);
        
        SetCommTimeouts(hSerial, &timeouts);
        
        EscapeCommFunction(hSerial, SETDTR);
        EscapeCommFunction(hSerial, SETRTS);
    }

    return( NULL );
}