http://bbs.csdn.net/topics/340184140
/************************************************************************************ * tty_open_port() open the tty port ************************************************************************************/int tty_open_port(const char *dev_name){ int fd; /* File descriptor for the port */ fd = open(dev_name, O_RDWR | O_NOCTTY | O_NDELAY); if (-1 == fd) { perror("open_port: Unable to open tty " ); exit(1); } else { if(DEBUG) printf("The %s is opened
",dev_name); }/* if( (val=fcntl(fd, F_SETFL, 0))< 0) perror("fcntl failed");*/ if ( isatty(fd) == 0 ) perror("This is not a tty device "); return (fd);}/************************************************************************************ * tty_set_port() set the attributes of the tty ************************************************************************************/ int tty_set_port (int fd , int nSpeed , int nBits , char nEvent , int nStop ){ struct termios new_ios,old_ios; if ( tcgetattr ( fd , &new_ios ) !=0 ) perror("Save the terminal error"); bzero( &old_ios , sizeof( struct termios )); old_ios=new_ios; tcflush(fd,TCIOFLUSH) ; new_ios.c_cflag |= CLOCAL |CREAD ; new_ios.c_cflag &= ~CSIZE ; switch (nBits) { case 5: new_ios.c_cflag |=CS5 ; break ; case 6: new_ios.c_cflag |=CS6 ; break ; case 7: new_ios.c_cflag |=CS7 ; break ; case 8: new_ios.c_cflag |=CS8 ; break ; default: perror("Wrong nBits"); break ; } switch (nSpeed ) { case 2400: cfsetispeed(&new_ios , B2400); cfsetospeed(&new_ios , B2400); break; case 4800: cfsetispeed(&new_ios , B4800); cfsetospeed(&new_ios , B4800); break; case 9600: cfsetispeed(&new_ios , B9600); cfsetospeed(&new_ios , B9600); break; case 19200: cfsetispeed(&new_ios , B19200); cfsetospeed(&new_ios , B19200); break; case 115200: cfsetispeed(&new_ios , B115200); cfsetospeed(&new_ios , B115200); break; case 460800: cfsetispeed(&new_ios , B460800); cfsetospeed(&new_ios , B460800); break; default: perror("Wrong nSpeed"); break; } switch (nEvent ) { case 'o': case 'O': new_ios.c_cflag |= PARENB ; new_ios.c_cflag |= PARODD ; new_ios.c_iflag |= (INPCK | ISTRIP); break ; case 'e': case 'E': new_ios.c_iflag |= (INPCK | ISTRIP); new_ios.c_cflag |= PARENB ; new_ios.c_cflag &= ~PARODD ; break ; case 'n': case 'N': new_ios.c_cflag &= ~PARENB ; new_ios.c_iflag &= ~INPCK ; break ; default: perror("Wrong nEvent"); break ; } if ( nStop == 1 ) new_ios.c_cflag &= ~CSTOPB ; else if ( nStop == 2 ) new_ios.c_cflag |= CSTOPB ; /*No hardware control*/ new_ios.c_cflag &= ~CRTSCTS; /*No software control*/ new_ios.c_iflag &= ~(IXON | IXOFF | IXANY); /*delay time set */ new_ios.c_cc[VTIME] = 0 ; new_ios.c_cc[VMIN] = 0 ; /*raw model*/ new_ios.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); new_ios.c_oflag &= ~OPOST; new_ios.c_iflag &= ~(INLCR|IGNCR|ICRNL); new_ios.c_iflag &= ~(ONLCR|OCRNL); new_ios.c_oflag &= ~(INLCR|IGNCR|ICRNL); new_ios.c_oflag &= ~(ONLCR|OCRNL); tcflush(fd,TCIOFLUSH) ; if (tcsetattr(fd,TCSANOW,&new_ios) != 0 ) { perror("Set the terminal error"); tcsetattr(fd,TCSANOW,&old_ios); return -1 ; } return 0;}