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

📄 rs232.cpp

📁 DOS下采用中断接收数据的串口通讯的例子,很难找到的好东西!
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    return RS232_SUCCESS;
}

// This non-virtual member function of class RS232 writes out
// a buffer using the virtual write_buffer() routine.  It has
// two additional parameters beyond those used by write_buffer(),
// which are a timeout value and a terminator.  The terminator
// can be used to automatically append a CR/LF pair to output
// strings.

int RS232::Write( void *buffer,
                        unsigned int count,
                        long milliseconds,
                        char *terminator )
{
    char *b = ( char * ) buffer;
    long start_time;
    unsigned int byte_count;
    int idle_status = RS232_SUCCESS;
    int write_status;

    ElapsedTime = 0;
    ByteCount = 0;
    if ( error_status < 0 )
        return error_status;

    byte_count = 0;
    start_time = ReadTime();
    if ( count == 0 )
        count = strlen( b );
    for ( ; ; ) {
        write_status = write_buffer( b, count );
        byte_count += ByteCount;
        b += ByteCount;
        count -= ByteCount;
        if ( count == 0 && terminator != 0 ) {
            count += strlen( terminator );
            b = terminator;
            terminator = 0;
            continue;
        }
        if ( write_status != RS232_TIMEOUT || count == 0 )
            break;
        if ( milliseconds != FOREVER &&
             ( ReadTime() - start_time ) >= milliseconds )
            break;
        if ( ( idle_status = IdleFunction() ) < RS232_SUCCESS )
            break;
    }
    ElapsedTime = ReadTime() - start_time;
    ByteCount = byte_count;
    if ( idle_status < RS232_SUCCESS )
        return idle_status;
    if ( write_status < RS232_SUCCESS )
        return write_status;
    else if ( count > 0 )
        return RS232_TIMEOUT;
    else
        return RS232_SUCCESS;
}

// There are two versions of the non-virtual ReadBuffer()
// function defined for the base class.  They differ only in what
// causes their normal termination.  This version terminates only
// when it reads in a full buffer of data, or times out.  The
// next version stops when it sees the terminator string
// specified as a parameter.

int RS232::Read( void *buffer, unsigned int count, long milliseconds )
{
    long start_time;
    unsigned int byte_count;
    char *b = (char *) buffer;
    int read_status;
    int idle_status = RS232_SUCCESS;

    ElapsedTime = 0;
    ByteCount = 0;
    if ( error_status < 0 )
        return error_status;
    start_time = ReadTime();
    byte_count = 0;
    for ( ; ; ) {
        read_status = read_buffer( b, count );
        byte_count += ByteCount;
        count -= ByteCount;
        b += ByteCount;
        if ( read_status != RS232_TIMEOUT || count == 0 )
            break;
        if ( milliseconds != FOREVER &&
            ( ReadTime() - start_time ) >= milliseconds )
            break;
        if ( ( idle_status = IdleFunction() ) < RS232_SUCCESS )
            break;
    }
    *b = '\0';
    ElapsedTime = ReadTime() - start_time;
    ByteCount = byte_count;
    if ( idle_status < RS232_SUCCESS )
        return idle_status;
    else
        return read_status;
}

// This version of ReadBuffer() looks for a termination string
// in the incoming data stream.  Because of this, it has to read
// in characters one at a time instead of in blocks.  It looks
// for the terminator by doing a strncmp() after every new
// character is read in, which is probably not the most efficient
// way of doing it.

int RS232::Read( void *buffer,
                 unsigned int count,
                 long milliseconds,
                 char *terminator )
{
    long start_time;
    unsigned int byte_count;
    char *b = (char *) buffer;
    int idle_status = RS232_SUCCESS;
    int c;
    int term_len;

    term_len = strlen( terminator );
    ElapsedTime = 0;
    ByteCount = 0;
    if ( error_status < 0 )
        return error_status;
    start_time = ReadTime();
    byte_count = 0;
    for ( ; ; ) {
        c = read_byte();
        if ( c >= 0 ) {
            byte_count++;
            count--;
            *b++ = (char) c;
            if ( byte_count >= (unsigned int) term_len ) {
                if ( strncmp( b - term_len, terminator, term_len ) == 0 ) {
                    b -= term_len;
                    c = RS232_SUCCESS;
                    byte_count -= term_len;
                    break;
                }
            }
            if ( count == 0 )
                break;
        } else {
            if ( c != RS232_TIMEOUT )
                break;
            if ( milliseconds != FOREVER &&
                 ( ReadTime() - start_time ) >= milliseconds )
                break;
            if ( ( idle_status = IdleFunction() ) < RS232_SUCCESS )
                break;
        }
    }
    *b = '\0';
    ElapsedTime = ReadTime() - start_time;
    ByteCount = byte_count;
    if ( idle_status < RS232_SUCCESS )
        return idle_status;
    else if ( c < RS232_SUCCESS )
        return c;
    else
        return RS232_SUCCESS;
}

// All of the remaining functions defined here are optional
// functions that won't be defined for every class.  The default
// versions of these virtual functions just return an error
// message.

int RS232::Break( long )
{
    return RS232_FUNCTION_NOT_SUPPORTED;
}

int RS232::SoftwareOverrunError( int )
{
    return RS232_FUNCTION_NOT_SUPPORTED;
}

int RS232::FlushTXBuffer( void )
{
    return RS232_FUNCTION_NOT_SUPPORTED;
}

int RS232::RXSpaceFree( void )
{
    return RS232_FUNCTION_NOT_SUPPORTED;
}

int RS232::TXSpaceUsed( void )
{
    return RS232_FUNCTION_NOT_SUPPORTED;
}

int RS232::XonXoffHandshaking( int )
{
    return RS232_FUNCTION_NOT_SUPPORTED;
}

int RS232::RtsCtsHandshaking( int )
{
    return RS232_FUNCTION_NOT_SUPPORTED;
}

int RS232::DtrDsrHandshaking( int )
{
    return RS232_FUNCTION_NOT_SUPPORTED;
}

int RS232::Dtr( int )
{
    return RS232_FUNCTION_NOT_SUPPORTED;
}

int RS232::Rts( int )
{
    return RS232_FUNCTION_NOT_SUPPORTED;
}

int RS232::Peek( void *, unsigned int )
{
    return RS232_FUNCTION_NOT_SUPPORTED;
}

void Settings::Adjust( long baud_rate,
                       int parity,
                       int word_length,
                       int stop_bits,
                       int dtr,
                       int rts,
                       int xon_xoff,
                       int rts_cts,
                       int dtr_dsr )
{
    if ( baud_rate != UNCHANGED )
            BaudRate = baud_rate;
    if ( parity != UNCHANGED )
        Parity = (char) toupper( parity );
    if ( word_length != UNCHANGED )
        WordLength = word_length;
    if ( stop_bits != UNCHANGED )
        StopBits = stop_bits;
    if ( dtr != UNCHANGED )
        Dtr = dtr;
    if ( rts != UNCHANGED )
        Rts = rts;
    if ( xon_xoff != UNCHANGED )
        XonXoff = xon_xoff;
    if ( rts_cts != UNCHANGED )
        RtsCts = rts_cts;
    if ( dtr_dsr != UNCHANGED )
        DtrDsr = dtr_dsr;
}

// ************************** END OF RS232.CPP **************************

⌨️ 快捷键说明

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