关于linux serial的硬流控(RTS)
linux的串口编程中,想要设置PC的串口的硬流控工作模式,设置了termios.c_cflag |= CRTSCTS,通过串口向外发数(两台PC通过串口互连,TX/RX, RTS/CTS都已经交叉没有问题),另一台收数。
问题是这样:
1, 向外发的数是从0到0xff递增循环的数,而另一台收到的都是0.
2, 而如果我要是同时把minicom打开,还是通过原来那个测试程序发数,则另一台收到的都是正确的数。
我在开minicom情况下和不开minicom情况下比较了termios整个结构体的值,发现都没有任何差异。不知道串口的硬流控是受什么影响了,愿闻路过的高见!
附我的配置如下:
=========
tcgetattr(fd, &_sd->old_tty); // backup old configure
bzero(&_sd->tty, sizeof(_sd->tty));
struct termios* ptty = &_sd->tty;
switch (baudrate) {
case 38400: spd = B38400; break;
case 57600: spd = B57600; break;
case 115200: spd = B115200; break;
case 230400: spd = B230400; break;
case 460800: spd = B460800; break;
case 500000: spd = B500000; break;
case 576000: spd = B576000; break;
case 921600: spd = B921600; break;
case 1000000: spd = B1000000; break;
case 1152000: spd = B1152000; break;
case 1500000: spd = B1500000; break;
case 2000000: spd = B2000000; break;
case 2500000: spd = B2500000; break;
case 3000000: spd = B3000000; break;
case 3500000: spd = B3500000; break;
case 4000000: spd = B4000000; break;
}
if (spd != -1) {
cfsetispeed(ptty, (speed_t)spd);
cfsetospeed(ptty, (speed_t)spd);
}
switch (databits) {
case 5:
ptty->c_cflag = (ptty->c_cflag & ~CSIZE) | CS5;
break;
case 6:
ptty->c_cflag = (ptty->c_cflag & ~CSIZE) | CS6;
break;
case 7:
ptty->c_cflag = (ptty->c_cflag & ~CSIZE) | CS7;
break;
case 8:
ptty->c_cflag = (ptty->c_cflag & ~CSIZE) | CS8;
break;
}
/* set into raw, no echo mode */
ptty->c_iflag = IGNBRK;
ptty->c_lflag = 0;
ptty->c_oflag = 0;
ptty->c_cflag |= CLOCAL | CREAD;
ptty->c_cc[VMIN] = 1;
ptty->c_cc[VTIME] = 5; // unit : 100ms
if (0) // software flow
ptty->c_iflag |= IXON | IXOFF;
else
ptty->c_iflag &= ~(IXON | IXOFF | IXANY);
ptty->c_cflag &= ~(PARENB | PARODD);
ptty->c_cflag &= ~CMSPAR;
switch (parity) {
case 'e':
case 'E':
ptty->c_cflag |= PARENB;
break;
case 'o':
case 'O':
ptty->c_cflag |= (PARENB | PARODD);
break;
case 's':
case 'S':
ptty->c_cflag |= (PARENB | CMSPAR);
break;
case 'm':
case 'M':
ptty->c_cflag |= (PARENB | PARODD | CMSPAR);
break;
case 'n':
case 'N':
default:
break;
}
switch (stopbits) {
case 2:
ptty->c_cflag |= CSTOPB;
break;
case 1:
default:
ptty->c_cflag &= ~CSTOPB;
break;
}
if (hwflow) {
ptty->c_cflag |= CRTSCTS;
} else {
ptty->c_cflag &= ~CRTSCTS;
}
tcflush(fd, TCIOFLUSH);
tcsetattr(fd, TCSANOW, ptty);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找着原因了,是我的测试代码有问题, write完了之后,我就close串口,并程序结束了.而write返回仅表示数据写到文件缓冲区,别没真正从串口的FIFO中发送出去。因此我在write之后,调用tcdrain(fd)函数,等待数据发送结束。这样问题就解决了.