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

📄 parlink.c

📁 开放源码的编译器open watcom 1.6.0版的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
        while( FM_Ctl1Hi() ) {          /* Wait till he heard us */
            TWIDDLE_THUMBS;
        }
        /*get the high nibble */
        RaiseCtl2();                    /* ready to read */
        while( FM_Ctl1Lo() ) {          /* wait for data */
            TWIDDLE_THUMBS;
        }
        data |= ( ReadData() << 4 );    /* bag the bits */
        LowerCtl2();                    /* Hey you! I got the bits */
        while( FM_Ctl1Hi() ) {          /* Wait till he heard us */
            TWIDDLE_THUMBS;
        }
        break;
    case LAPLINK_VAL:
        /* get the low nibble */
        LL_RaiseCtl2();                 /* ready to read */
        while( LL_Ctl1Lo() ) {          /* wait for data */
            TWIDDLE_THUMBS;
        }
        data = LL_ReadData();           /* bag the bits */
        LL_LowerCtl2();                 /* Hey you! I got the bits */
        while( LL_Ctl1Hi() ) {          /* Wait till he heard us */
            TWIDDLE_THUMBS;
        }
        /*get the high nibble */
        LL_RaiseCtl2();                 /* ready to read */
        while( LL_Ctl1Lo() ) {          /* wait for data */
            TWIDDLE_THUMBS;
        }
        data |= ( LL_ReadData() << 4 ); /* bag the bits */
        LL_LowerCtl2();                 /* Hey you! I got the bits */
        while( LL_Ctl1Hi() ) {          /* Wait till he heard us */
            TWIDDLE_THUMBS;
        }
        break;
    case DUTCHMAN_VAL:
        /* get the low nibble */
        FD_RaiseCtl2();                 /* ready to read */
        while( FD_Ctl1Lo() ) {          /* wait for data */
            TWIDDLE_THUMBS;
        }
        data = FD_ReadData();           /* bag the bits */
        FD_LowerCtl2();                 /* Hey you! I got the bits */
        while( FD_Ctl1Hi() ) {          /* Wait till he heard us */
            TWIDDLE_THUMBS;
        }
        /*get the high nibble */
        FD_RaiseCtl2();                 /* ready to read */
        while( FD_Ctl1Lo() ) {          /* wait for data */
            TWIDDLE_THUMBS;
        }
        data |= ( FD_ReadData() << 4 ); /* bag the bits */
        FD_LowerCtl2();                 /* Hey you! I got the bits */
        while( FD_Ctl1Hi() ) {          /* Wait till he heard us */
            TWIDDLE_THUMBS;
        }
        break;
    }
    return( data );
}


/* if wait is not KEEP or RELINQUISH it is the latest time that this
 * operation should take before it times out */

static int DataPut( char data, unsigned long wait )
{
    dbgrtn( "\r\n-DataPut-" );
    switch( CableType ) {
    case WATCOM_VAL:
        while( Ctl2Lo() ) {             /* wait till he's ready to read */
            TWIDDLE_THUMBS;
        }
        WriteData( data );              /* write the data */
        RaiseCtl1();                    /* tell him the data's there */
        while( Ctl2Hi() ) {             /* wait till he got the bits */
            TWIDDLE_THUMBS;
        }
        LowerCtl1();                    /* clear control line */
        break;
    case FMR_VAL:
        /* We're talking to the FMR which can RaiseCtl2/LowerCtl2 */
        while( Ctl2Lo() ) {             /* wait till he's ready to read */
            TWIDDLE_THUMBS;
        }
        WriteData( data );              /* write the data */
        RaiseCtl1();                    /* tell him the data's there */
        while( Ctl2Hi() ) {             /* wait till he got the bits */
            TWIDDLE_THUMBS;
        }
        LowerCtl1();                    /* clear control line */
        break;
   case LAPLINK_VAL:
        /* send low nibble */
        while( LL_Ctl2Lo() ) {          /* wait till he's ready to read */
            TWIDDLE_THUMBS;
        }
        LL_WriteData( data & 0x0f );    /* write the data */
                                        /* and tell him the data's there */
        while( LL_Ctl2Hi() ) {          /* wait till he got the bits */
            TWIDDLE_THUMBS;
        }
        LL_LowerCtl1();                 /* clear control line */
        /* send high nibble */
        while( LL_Ctl2Lo() ) {          /* wait till he's ready to read */
            TWIDDLE_THUMBS;
        }
        LL_WriteData( data >> 4 );      /* write the data */
                                        /* and tell him the data's there */
        while( LL_Ctl2Hi() ) {          /* wait till he got the bits */
            TWIDDLE_THUMBS;
        }
        LL_LowerCtl1();                 /* clear control line */
        break;
    case DUTCHMAN_VAL:
        /* send low nibble */
        while( FD_Ctl2Lo() ) {          /* wait till he's ready to read */
            TWIDDLE_THUMBS;
        }
        FD_WriteData( data & 0x0f );    /* write the data */
        FD_RaiseCtl1();                 /* tell him the data's there */
        while( FD_Ctl2Hi() ) {          /* wait till he got the bits */
            TWIDDLE_THUMBS;
        }
        FD_LowerCtl1();                 /* clear control line */
        /* send high nibble */
        while( FD_Ctl2Lo() ) {          /* wait till he's ready to read */
            TWIDDLE_THUMBS;
        }
        FD_WriteData( data >> 4 );      /* write the data */
        FD_RaiseCtl1();                 /* tell him the data's there */
        while( FD_Ctl2Hi() ) {          /* wait till he got the bits */
            TWIDDLE_THUMBS;
        }
        FD_LowerCtl1();                 /* clear control line */
        break;
    }
    return( 0 );
}

#pragma off(unreferenced);
unsigned RemoteGet( char *rec, unsigned len )
#pragma on(unreferenced);
{
    unsigned    get_len;
    unsigned    i;

    get_len = DataGet( RELINQUISH );
    if( get_len & 0x80 ) {
        get_len = ((get_len & 0x7f) << 8) | DataGet( KEEP );
    }
    i = get_len;
    for( ; i != 0; --i, ++rec ) {
        *rec = DataGet( KEEP );
    }
    return( get_len );
}

unsigned RemotePut( char *snd, unsigned len )
{
    unsigned    count;

    if( len >= 0x80 ) {
        DataPut( (len >> 8) | 0x80, RELINQUISH );
    }
    DataPut( len & 0xff, RELINQUISH );
    for( count = len; count != 0; --count, ++snd ) {
        DataPut( *snd, KEEP );
    }
    return( len );
}

/*
 * Synch - see if server and client are ready
 */

static bool Synch( void )
{
    dbgrtn( "\r\n-Synch-" );
    switch( CableType ) {
    case WATCOM_VAL:
    case FMR_VAL:
        if( Ctl2Lo() ) {
            return( TRUE );
        }
        break;
    case LAPLINK_VAL:
        if( LL_Ctl1Lo() ) return( TRUE );
        break;
    case DUTCHMAN_VAL:
        if( FD_Ctl1Lo() ) return( TRUE );
        break;
    }
    return( FALSE );
}




static bool CountTwidle( void )
{
    char                type;

    dbgrtn( "\r\n-CountTwidle-" );
    type = ReadData();
#if defined(_DBG)
#ifdef SERVER
    printf( "Type %2.2x ", type );
#else
    {
    char buf[10];

    itoa( type, buf, 16 );
    cputs( " Type " ); cputs( buf );
    }
#endif
#endif
    if( !TwidleOn ) {
        if( type == WATCOM_VAL ||
            type == FMR_VAL ||
            type == LAPLINK_VAL ||
            type == DUTCHMAN_VAL ) {
            TwidleOn = TRUE;
            if( type != CableType ) {
                TwidleCount = 0;
                CableType = type;
            }
        }
    } else {
        if( type != CableType )  {
            TwidleCount ++;
            TwidleOn = FALSE;
            if( TwidleCount == TWIDLE_NUM ) return( TRUE );
        }
    }
    return( FALSE );
}

/*
 * Twidle - send an intermittent pulse over a line to let the person
 *          at the other end know you're there
 */

static bool Twidle( bool check ) {

    unsigned            i;
    unsigned long       time;

    dbgrtn( "\r\n-Twidle-" );
    for( i = 20; i != 0; i-- ) {
        WriteData( TWIDLE_ON );
        time = Ticks() + TWIDLE_TIME;
        while( time > Ticks() ){
            if( check ) {
                if( CountTwidle() ) {
                    return( TRUE );
                }
            } else {
                if( Synch() ) {
                    return( TRUE );
                }
            }
        }
        WriteData( TWIDLE_OFF );
        time = Ticks() + TWIDLE_TIME;
        while( time > Ticks() ){
            if( check ) {
                if( CountTwidle() ) {
                    return( TRUE );
                }
            } else {
                if( Synch() ) {
                    return( TRUE );
                }
            }
        }
    }
    return( FALSE );
}

/*
 * LineTest - make sure that all lines are working
 */

static bool LineTest( void )
{
    unsigned            send;
    unsigned long       time;
#ifdef SERVER
    unsigned            ret;

    dbgrtn( "\r\n-LineTest-" );
    for( send = 1; send != 256; send *= 2 ) {
        time = Ticks() + LINE_TEST_WAIT;
        if( time == RELINQUISH ) time ++;
        if( time == KEEP ) time ++;
        ret = DataPut( send, time );
        if( ret == TIMEOUT ) return( FALSE );
        time = Ticks() + LINE_TEST_WAIT;
        if( time == RELINQUISH ) time ++;
        if( time == KEEP ) time ++;
        ret = DataGet( time );
        if( ret == TIMEOUT ) return( FALSE );
        if( ret != send ) {
            return( FALSE );
        }
    }
    time = Ticks() + LINE_TEST_WAIT;
    if( time == RELINQUISH ) time ++;
    if( time == KEEP ) time ++;
    ret = DataPut( DONE_LINE_TEST, time );
    if( ret == TIMEOUT ) return( FALSE );
#else
    dbgrtn( "\r\n-LineTest-" );
    send = 0;
    for( ;; ) {
        time = Ticks() + LINE_TEST_WAIT;
        if( time == RELINQUISH ) time ++;
        if( time == KEEP ) time ++;
        send = DataGet( time );
        if( send == TIMEOUT ) return( FALSE );
        if( send == DONE_LINE_TEST ) break;
        time = Ticks() + LINE_TEST_WAIT;
        if( time == RELINQUISH ) time ++;
        if( time == KEEP ) time ++;
        DataPut( send, time );
        if( send == TIMEOUT ) return( FALSE );
    }
#endif
    return( TRUE );
}

char RemoteConnect( void )
{
    unsigned long       time;

#ifdef SERVER
    bool                got_twidles;

    dbgrtn( "\r\n-RemoteConnect-" );
    if( !CountTwidle() ) return( 0 );
    got_twidles = Twidle( FALSE );
#else
    dbgrtn( "\r\n-RemoteConnect-" );
    if( !Twidle( TRUE ) ) return( FALSE );
#endif

    switch( CableType ) {
    case WATCOM_VAL:
        LowerCtl1();
        LowerCtl2();
    case FMR_VAL:
        LowerCtl1();
        LowerCtl2();
    case LAPLINK_VAL:
        LL_LowerCtl1();
        break;
    case DUTCHMAN_VAL:
        FD_LowerCtl1();
        break;
    }
#ifdef SERVER
    if( !got_twidles ) {
#endif
        time = Ticks() + SYNCH_WAIT;
        for( ;; ) {
            if( Synch() ) {
                break;
            } else if( time < Ticks() ) {
                return( 0 );
            }
        }
#ifdef SERVER
    }
#endif
    if( !LineTest() ) return( FALSE );
    return( TRUE );
}

void RemoteDisco( void )
{
    unsigned long       time;

    dbgrtn( "\r\n-RemoteDisco-" );
    time = Ticks() + TWIDLE_TIME;
    while( time > Ticks() ) { /* delay while other side catches up */ }
    WriteData( TWIDLE_OFF );            /* initialize control ports */
    XX_RaiseCtl1();
    TwidleCount = 0;
    CableType = NULL_VAL;
    TwidleOn = FALSE;

}

static char InvalidPort[] = TRP_ERR_invalid_parallel_port_number;

char *RemoteLink( char *name, char server )
{
    unsigned    printer;
    unsigned    ch;
    char        *err;

    dbgrtn( "\r\n-RemoteLink-" );
    server = server;

    err = InitSys();
    if( err != NULL ) {
        return( err );
    }
    if( name == NULL || name[0] == '\0' ) {
        printer = 0;
    } else if( name[0] >= '1' && name[0] <= '3' && name[1] == '\0' ) {
        printer = name[0] - '1';
    } else if( name[0] == 'p' || name[0] == 'P' ) {
        printer = 0;
        for( ;; ) {
            ++name;
            ch = *name;
            if( ch == 0 ) break;
            if( ch == ' ' ) break;
            if( ch == '\t' ) break;
            if( ch >= 'A' && ch <= 'F' ) {
                ch = ch - 'A' + 0x0a;
            } else if( ch >= 'a' && ch <= 'f' ) {
                ch = ch - 'a' + 0x0a;
            } else if( ch >= '0' && ch <= '9' ) {
                ch = ch - '0';
            } else {
                return( InvalidPort );
            }
            printer <<= 4;
            printer += ch;
        }
        DataPort = printer;
    } else {
        return( InvalidPort );
    }
    if( DataPort == 0 ) {
        if( NumPrinters() <= printer ) {
            return( TRP_ERR_parallel_port_not_present );
        }
        DataPort = PrnAddress( printer );
    }
    CtlPort1 = DataPort + 1;
    CtlPort2 = CtlPort1 + 1;

    if( !AccessPorts( DataPort, CtlPort2 ) ) {
        return( TRP_ERR_cannot_access_parallel_ports );
    }
    WriteData( TWIDLE_OFF );            /* initialize the control ports */
    XX_RaiseCtl1();
    TwidleOn = FALSE;
    return( NULL );
}

void RemoteUnLink( void )
{
    FiniSys();
    FreePorts( DataPort, CtlPort2 );
    DataPort = 0;
}

⌨️ 快捷键说明

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