⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serlink.c

📁 开放源码的编译器open watcom 1.6.0版的源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
    }
    return( TRP_ERR_invalid_maximum_baud_rate );
}


/*========================================================================*/
/*========================================================================*/


/* This routine is called by RemoteLink to set max baud rate */

static char *CollectParm( char *parm, char *arg, int *len )
{
    int  i;

    i = 0;
    while( *parm >= '0' && *parm <= '9' ) {
        if( i < 7 ) arg[i] = *parm;
        ++parm;
        ++i;
    }
    *len = i;
    return( parm );
}


static char *SetLinkParms( char **pparm )
{
    char        *parm;
    char        arg1[7];
    int         arg1_len;
    char        *result;        /* result of ParsePortSpec or SetComPort */

    parm = *pparm;
    MaxBaud = 0;                /* default max baud is 115200 */

    /* strip leading white spaces */
    while( *parm == ' ' || *parm == '\t' ) ++parm;

    result = ParsePortSpec( &parm );
    if( result != NULL ) return( result );

    arg1_len = 0;
    if( *parm == '.' ) {
        ++parm;
        parm = CollectParm( parm, arg1, &arg1_len );
        if( arg1_len >= 7 ) return( TRP_ERR_invalid_baud_rate );
    }
    *pparm = parm;
    if( arg1_len == 0 ) return( NULL );
    arg1[ arg1_len ] = '\0';
    if( StrEq( arg1, "0" ) ) {
        MaxBaud = MIN_BAUD;
        return( NULL );
    }
    if( arg1_len < 2 ) return( TRP_ERR_ambiguous_baud_rate );
    return( SetMaxBaud( arg1 ) );
}


/*========================================================================*/
/*========================================================================*/

static void SlowSend( int ch )
{
    SendByte( ch );
    if( WaitByte( 1 ) != NO_DATA ) Wait( 1 ); /* pickup echoed character */
}

static char *SetupModem( char *parm )
{
    char        *start;
    unsigned    wait;
    unsigned    ch;
    int         data;

    Baud( MaxBaud );
    wait = SEC(3);
    while( *parm == ' ' && *parm == '\t' ) ++parm;
    if( *parm == '\0' ) return( NULL );
    for( ;; ) {
        if( *parm == '(' ) {
            start = ++parm;
            for( ;; ) {
                ch = *parm;
                if( ch == '\0' ) goto done;
                ++parm;
                if( ch == ')' ) break;
                if( ch == '\\' ) {
                    ch = *parm++;
                    switch( ch ) {
                    case '\0':
                        return( TRP_ERR_invalid_modem_string );
                    case 'r':
                        ch = '\r';
                        break;
                    case 'n':
                        ch = '\n';
                        break;
                    }
                }
                data = WaitByte( wait );
                if( data == NO_DATA ) {
                    if( wait != SEC(60) ) {
                        wait = SEC(60);
                    } else {
                        return( TRP_ERR_timeout_on_modem_string );
                    }
                    --parm;
                } else {
                    wait = SEC(3);
                    if( data != ch ) parm = start;
                }
            }
        } else {
            Wait( SEC(1)/5 );
            for( ;; ) {
                ch = *parm;
                if( ch == '\0' ) goto done;
                if( ch == '(' ) break;
                ++parm;
                if( ch == '\\' ) {
                    ch = *parm++;
                    switch( ch ) {
                    case '\0':
                        return( TRP_ERR_invalid_modem_string );
                    case '`':
                        Wait( 1 );
                        break;
                    case '~':
                        Wait( SEC(1) );
                        break;
                    case 'r':
                        SlowSend( '\r' );
                        Wait( SEC(1)/2 );
                        break;
                    case 'n':
                        SlowSend( '\n' );
                        break;
                    default:
                        SlowSend( ch );
                        break;
                    }
                } else {
                    SlowSend( ch );
                }
            }
        }
    }
done:
#ifdef SERVER
  #define SEND_CHAR     '='
  #define EXPECT_CHAR   '-'
#else
  #define SEND_CHAR     '-'
  #define EXPECT_CHAR   '='
#endif
    wait = 1;
    for( ;; ) {
        data = WaitByte( wait );
        if( data == EXPECT_CHAR ) break;
        if( data == NO_DATA ) {
            if( wait != SEC(10) ) {
                wait = SEC(10);
                SendByte( SEND_CHAR );
            } else {
                return( TRP_ERR_modem_failed_connection );
            }
        }
    }
    if( wait != SEC(10) ) SendByte( SEND_CHAR );
    return( NULL );
}

/* The format for *parm is "1.9600<modem_connect_string>" */

char *RemoteLink( char *parm, char server )
{
    char *result;

    server = server;
    if( parm == NULL ) parm = "";
    result = SetLinkParms( &parm );  /* set com: port & max baud rate */
    if( result != NULL ) {
        DonePort();
        return( result );
    }
    result = InitSys();
    if( result != NULL ) {
        DonePort();
        return( result );
    }
    result = SetupModem( parm );
    if( result != NULL ) RemoteUnLink();
    return( result );
}


/*========================================================================*/
/*========================================================================*/


void RemoteUnLink( void )
{
    ResetSys();       /* reset system to initial state */
    DonePort();
}


/*========================================================================*/
/*========================================================================*/


/* Returns: SUCCESS or FAIL */

char RemoteConnect( void )
{
    int     baud_limit;     /* maximum baud that BOTH sides can achieve */
    char    dummy;          /* hold values that we don't need here */
    char    MaxBaud2;       /* MaxBaud at the other machine */

    SendBlkNo = ReceiveBlkNo = 0;
    LastResponse = NAK;
    if( !SetSyncTime() ) return( FALSE );
    /* establish baud limit */
#ifdef SERVER
    if( !WaitReceive( &dummy, 1, &MaxBaud2, SEC( 2 ) ) ) {
        return( FALSE );
    }
    if( !BlockSend( 1, (void *)&MaxBaud, SEC( 2 ) ) ) {
        return( FALSE );
    }
#else
    if( !BlockSend( 1, (void *)&MaxBaud, SEC( 2 ) ) ) {
        return( FALSE );
    }
    if( !WaitReceive( &dummy, 1, &MaxBaud2, SEC( 2 ) ) ) {
        return( FALSE );
    }
#endif
    /* MaxBaud2 now contains the other side's baud rate limit */
    if( MaxBaud > MaxBaud2 ) {
        baud_limit = MaxBaud;
    } else {
        baud_limit = MaxBaud2;
    }

    BaudCounter = baud_limit;
    if( !Speed() ) return( FALSE );
#ifdef SERVER
    {
        char    buff[128];

        if( BaudCounter == MIN_BAUD ) {
            strcpy( buff, "pre-set" );
        } else {
            strcpy( buff, BaudTable[BaudCounter].name );
        }
        strcat( buff, " baud" );
        ServMessage( buff );
    }
#endif
    return( TRUE );
}


/*========================================================================*/
/*========================================================================*/


void RemoteDisco( void )
{
    ClearLastChar();
    ClearCom();
}


/*========================================================================*/
/*========================================================================*/

/* Return:  Number of bytes received                                    */

unsigned RemoteGet( char *rec, unsigned max_len )
{
    unsigned timeout;             /* time limit for getting the data */
    char     err;                 /* storing the # of Errors the other side
                                        experience in sending data block */
    int      result;              /* result of WaitReceive() operation */


    timeout = FOREVER;

    /* Get data block */
    result = WaitReceive( &err, max_len, rec, timeout );
    if( !result ) return( 0 );

   if( err > MAX_ERRORS ) {    /* too many Errors */
        ReSync();
        BytesReceived = 0;
    }
    return( BytesReceived );
}


/*========================================================================*/
/*========================================================================*/


unsigned RemotePut( char *send, unsigned len )
{
    unsigned timeout;             /* time limit for getting the data */
    int      result;              /* result of BlockSend() operation */

    timeout = SEC( 10 );


    /* Sending data block */
    result = BlockSend( len, send, timeout );
    if( !result ) return( REQUEST_FAILED );

    /* ReSync if necessary */
    if( PrevErrors > MAX_ERRORS ) {
        ReSync();
    }
    return( len );
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -