linux 下串行口超时设置
这是我的实现代码,关键是fcntl(fd, F_SETFL, O_NDELAY) 这一行不能要,有了这一行,一切都会搞乱。 struct termios options; fd = open("/devttyS0", O_RDWR | O_NOCTTY ); if (fd == -1) { //Could not ope
·
这是我的实现代码,关键是fcntl(fd, F_SETFL, O_NDELAY) 这一行不能要,有了这一行,一切都会搞乱。
struct termios options;
fd = open("/devttyS0", O_RDWR | O_NOCTTY );
if (fd == -1)
{
//Could not open the port.
return FAIL;
}
//fcntl(fd, F_SETFL, O_NDELAY);
//Get the current options for the port...
tcgetattr(fd, &options);
//Set the baud rates
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
//Enable the receiver and set local mode...
options.c_cflag |= (CLOCAL | CREAD);
//8 data bits
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
//CSTOPB -- 2 stop bits (1 otherwise)
//1 stop bit
options.c_cflag &= ~CSTOPB;
//no parity
options.c_cflag &= ~PARENB;
//use rts/cts flow control
if(flowControl)
{
options.c_cflag |= CRTSCTS;
}
else
{
options.c_cflag &= (~CRTSCTS);
}
//disable software flow control
options.c_iflag &= ~(IXON | IXOFF | IXANY);
//raw type input
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
//raw type output
options.c_oflag &= ~OPOST;
//disable 0d->0xa mapping
options.c_iflag &= ~ (INLCR | ICRNL | IGNCR);
options.c_oflag &= ~(ONLCR | OCRNL);
options.c_cc[VTIME] = 40; /* Set timeout of 4.0 seconds */
options.c_cc[VMIN] = 0;
//Set the new options for the port...
//tcsetattr(fd, TCSANOW, &options);
tcsetattr(fd, TCSAFLUSH, &options);
tcflush(fd, TCIOFLUSH);
更多推荐
已为社区贡献2条内容
所有评论(0)