clib/src/io/serial.cpp
/*serial setup : serial control cfg*/
serial setup : serial control cfg
serial setup : serial control cfg
int Serial::serial_setup(void)
{
    struct termios newtio, oldtio;
    if(tcgetattr(fd, &oldtio) != 0){
        cout<< "tcgetattr failed: %s\n" << strerror(errno) << endl;
        return -1;
    }
    memset(&newtio, 0, sizeof(newtio));
    memcpy(&newtio, &oldtio, sizeof(struct termios));
    if(this->mode == 0){
        cfmakeraw(&newtio);
    }
    switch(this->bits){
        case 7: newtio.c_cflag |= CS7; break;
        case 8: newtio.c_cflag |= CS8; break;
    }
    switch(this->parity){
        case 'O':
            newtio.c_cflag |= PARENB;
            newtio.c_cflag |= PARODD;
            newtio.c_iflag |= (INPCK | ISTRIP);
            break;
        case 'E':
            newtio.c_iflag |= (INPCK | ISTRIP);
            newtio.c_cflag |= PARENB;
            newtio.c_cflag &= ~PARODD;
            break;
        case 'N':
            newtio.c_cflag &= ~PARENB;
            newtio.c_iflag &= ~INPCK;
            break;
    }
    switch(this->speed){
        case 2400:
            cfsetispeed(&newtio, B2400);
            cfsetospeed(&newtio, B2400);
            break;
        case 4800:
            cfsetispeed(&newtio, B4800);
            cfsetospeed(&newtio, B4800);
            break;
        case 9600:
            cfsetispeed(&newtio, B9600);
            cfsetospeed(&newtio, B9600);
            break;
        case 38400:
            cfsetispeed(&newtio, B38400);
            cfsetospeed(&newtio, B38400);
            break;
        case 57600:
            cfsetispeed(&newtio, B57600);
            cfsetospeed(&newtio, B57600);
            break;
        case 115200:            
            cfsetispeed(&newtio, B115200);
            cfsetospeed(&newtio, B115200);
            break;
        default:
            cfsetispeed(&newtio, B38400);
            cfsetospeed(&newtio, B38400);
            break;
    }
    if(this->stop == 1)         newtio.c_cflag &= ~CSTOPB;
    else if(this->stop == 2)    newtio.c_cflag |= CSTOPB;
    tcflush(this->fd, TCIOFLUSH);
    newtio.c_cc[VTIME] = 0.001;
    newtio.c_cc[VMIN] = 0;
    if(tcsetattr(this->fd, TCSANOW, &newtio) < 0){
        cout<< "tcsetattr failed: " << strerror(errno) << endl;
        return -1;
    }
    return 0;
}
int Serial :: serial_setup ( void ) { struct termios newtio , oldtio ; if ( tcgetattr ( fd , & oldtio ) != 0 ) { cout << " \n " << strerror ( errno ) << endl ; return -1 ; } memset ( & newtio , 0 , sizeof ( newtio ) ) ; memcpy ( & newtio , & oldtio , sizeof ( struct termios ) ) ; if ( this -> mode == 0 ) { cfmakeraw ( & newtio ) ; } switch ( this -> bits ) { case 7 : newtio . c_cflag |= CS7 ; break ; case 8 : newtio . c_cflag |= CS8 ; break ; } switch ( this -> parity ) { case ' ' : newtio . c_cflag |= PARENB ; newtio . c_cflag |= PARODD ; newtio . c_iflag |= ( INPCK | ISTRIP ) ; break ; case ' ' : newtio . c_iflag |= ( INPCK | ISTRIP ) ; newtio . c_cflag |= PARENB ; newtio . c_cflag &= ~ PARODD ; break ; case ' ' : newtio . c_cflag &= ~ PARENB ; newtio . c_iflag &= ~ INPCK ; break ; } switch ( this -> speed ) { case 2400 : cfsetispeed ( & newtio , B2400 ) ; cfsetospeed ( & newtio , B2400 ) ; break ; case 4800 : cfsetispeed ( & newtio , B4800 ) ; cfsetospeed ( & newtio , B4800 ) ; break ; case 9600 : cfsetispeed ( & newtio , B9600 ) ; cfsetospeed ( & newtio , B9600 ) ; break ; case 38400 : cfsetispeed ( & newtio , B38400 ) ; cfsetospeed ( & newtio , B38400 ) ; break ; case 57600 : cfsetispeed ( & newtio , B57600 ) ; cfsetospeed ( & newtio , B57600 ) ; break ; case 115200 : cfsetispeed ( & newtio , B115200 ) ; cfsetospeed ( & newtio , B115200 ) ; break ; default : cfsetispeed ( & newtio , B38400 ) ; cfsetospeed ( & newtio , B38400 ) ; break ; } if ( this -> stop == 1 ) newtio . c_cflag &= ~ CSTOPB ; else if ( this -> stop == 2 ) newtio . c_cflag |= CSTOPB ; tcflush ( this -> fd , TCIOFLUSH ) ; newtio . c_cc [ VTIME ] = 0.001 ; newtio . c_cc [ VMIN ] = 0 ; if ( tcsetattr ( this -> fd , TCSANOW , & newtio ) < 0 ) { cout << " " << strerror ( errno ) << endl ; return -1 ; } return 0 ; }
serial setup : serial control cfg
serial setup : serial control cfg
/* Set raw mode: the remote application will handle all terminal characters*/ /*set timeout of newtio.c_cc[VTIME]/10 s*/