Logo Search packages:      
Sourcecode: libserial version File versions  Download package

const SerialStreamBuf::CharSizeEnum SerialStreamBuf::SetCharSize ( const CharSizeEnum  char_size  ) 

Set the character size to be used during serial communication. It returns the character size on success and CHAR_SIZE_INVALID on failure.

Definition at line 361 of file SerialStreamBuf.cc.

References CHAR_SIZE_5, CHAR_SIZE_6, CHAR_SIZE_7, CHAR_SIZE_8, CHAR_SIZE_INVALID, CharSize(), and mFileDescriptor.

Referenced by LibSerial::SerialStream::SetCharSize(), and SetParametersToDefault().

                                                         {
    if( -1 == mFileDescriptor ) {
        return CHAR_SIZE_INVALID ;
    }
    switch(char_size) {
    case CHAR_SIZE_5:
    case CHAR_SIZE_6:
    case CHAR_SIZE_7:
    case CHAR_SIZE_8:
        //
        // Get the current terminal settings. 
        //
        struct termios term_setting ;
        if( -1 == tcgetattr(mFileDescriptor, &term_setting) ) {
            return CHAR_SIZE_INVALID ;
        }
        //
        // Set the character size to the specified value. If the character
        // size is not 8 then it is also important to set ISTRIP. Setting
        // ISTRIP causes all but the 7 low-order bits to be set to
        // zero. Otherwise they are set to unspecified values and may
        // cause problems. At the same time, we should clear the ISTRIP
        // flag when the character size is 8 otherwise the MSB will always
        // be set to zero (ISTRIP does not check the character size
        // setting; it just sets every bit above the low 7 bits to zero).
        //
        if( char_size == CHAR_SIZE_8 ) {
            term_setting.c_iflag &= ~ISTRIP ; // clear the ISTRIP flag.
        } else {
            term_setting.c_iflag |= ISTRIP ;  // set the ISTRIP flag.
        }
        term_setting.c_cflag &= ~CSIZE ;     // clear all the CSIZE bits.
        term_setting.c_cflag |= char_size ;  // set the character size. 
        //
        // Set the new settings for the serial port. 
        //
        if( -1 == tcsetattr(mFileDescriptor, TCSANOW, &term_setting) ) {
            return CHAR_SIZE_INVALID ;
        } 
        break ;
    default:
        return CHAR_SIZE_INVALID ;
        break ;
    }
    return this->CharSize() ;
}


Generated by  Doxygen 1.6.0   Back to index