linux中设置串口的系统调用tcgetattr,tcsetattr,tcflush的使用总结
头文件:#include <termios.h>函数原型:int tcsetattr(int fd, int optional_actions, const struct termios *termios_p);//设置串口属性int tcgetattr(int fd, struct termios *termios_p);//获得串口属性int tcflush(int
头文件:
#include <termios.h>
函数原型:
int tcsetattr(int fd, int optional_actions, const struct termios *termios_p);//设置串口属性
int tcgetattr(int fd, struct termios *termios_p);//获得串口属性
int tcflush(int fd, int queue_selector);//刷新串口的缓冲区
speed_t cfgetispeed(struct termios *termios_p);//获得输入速度
speed_t cfgetospeed(struct termios *termios_p);//获得输出速度
int cfsetispeed(struct termios *termios_p, speed_t speed);
int cfsetospeed(struct termios *temios_p, speed_t speed);
参数:
int fd:是打开的串口终端的文件描述符。如:/dev/ttyS0
struct termios *termios_p:存放参数的结构体的指针。
具体参见:https://baike.baidu.com/item/Termios/1467529?fr=aladdin
int optional_actions:用于控制修改起作用的时间,可以使用如下的取值:
TCSANOW:不等数据传输完毕立即改变属性。
TCSADRAIN:等所有的数据传输完毕才改变属性。
TCSAFLUSH:等所有的数据传输完毕,并且清空输入输出的缓冲区才改变属性。
int queue_selector:代表刷新的具体的缓冲区,可以有如下的取值:
TCIFLUSH:刷新输入缓冲区。
TCOFLUSH:刷新输出缓冲区。
TCIOFLUSH:刷新输入输出缓冲区。
如下为,实例;
int Uart0_Init(void)
{
const char *path_utf = "/dev/ttyS0";
printf("Open Uart0Init!! \n");
if (uart0_fd == -1)
{
uart0_fd = open(path_utf, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
if(uart0_fd == -1)
{
printf("Failed to open serial port %s, %s\n", path_utf, strerror(errno));
return -1;
}
printf("Open Uart0 OK!! \n");
}
/* Configure device */
struct termios newtio, oldtio;
if ( tcgetattr(uart0_fd, &oldtio ) != 0) {
perror("tcgetattr error");
return -1;
}
//tcflush(fd, TCIOFLUSH);
bzero( &newtio, sizeof(newtio) );
newtio.c_cflag |= CLOCAL | CREAD;
newtio.c_cflag &= ~CSIZE;
newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
newtio.c_oflag &= ~OPOST;
newtio.c_oflag &= ~(ONLCR | INLCR);
newtio.c_iflag &= ~(ICRNL | INLCR);
newtio.c_iflag &= ~(IXON | IXOFF | IXANY);
//newtio.c_lflag &= ~(ICANON | ISIG | ECHO | ECHOE | IEXTEN);
//newtio.c_oflag &= ~OPOST;
//newtio.c_iflag &= ~(IXON | IXOFF | IXANY | ICRNL | BRKINT | INPCK | ISTRIP);
//newtio.c_cflag &= ~CRTSCTS;
newtio.c_cflag |= CS8;
newtio.c_cflag &= ~PARENB;
// cfsetispeed(&newtio, B115200);
// cfsetospeed(&newtio, B115200);
cfsetispeed(&newtio, B230400);
cfsetospeed(&newtio, B230400);
newtio.c_cflag &= ~CSTOPB;
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 0;
tcflush(uart0_fd, TCIOFLUSH);
if ((tcsetattr(uart0_fd, TCSANOW, &newtio)) != 0) {
perror("set opt error");
return -1;
}
return uart0_fd;
}
int Uart1Init(void)
{
const char *path_utf = "/dev/ttyS1";
int fd = -1;
// speed_t speed = B115200;
//speed_t speed = B230400;
//fd = open(path_utf, O_RDWR | O_NOCTTY);
fd = open(path_utf, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
if(fd == -1)
{
printf("Failed to open serial --1-- port %s,\n", path_utf);
return -1;
}
printf("Open Uart1 OK!! \n");
/* Configure device */
struct termios newtio, oldtio;
if ( tcgetattr( fd, &oldtio ) != 0) {
perror("tcgetattr error");
return -1;
}
//tcflush(fd, TCIOFLUSH);
bzero( &newtio, sizeof(newtio) );
newtio.c_cflag |= CLOCAL | CREAD;
newtio.c_cflag &= ~CSIZE;
newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
newtio.c_oflag &= ~OPOST;
newtio.c_oflag &= ~(ONLCR | INLCR);
newtio.c_iflag &= ~(ICRNL | INLCR);
newtio.c_iflag &= ~(IXON | IXOFF | IXANY);
//newtio.c_lflag &= ~(ICANON | ISIG | ECHO | ECHOE | IEXTEN);
//newtio.c_oflag &= ~OPOST;
//newtio.c_iflag &= ~(IXON | IXOFF | IXANY | ICRNL | BRKINT | INPCK | ISTRIP);
//newtio.c_cflag &= ~CRTSCTS;
newtio.c_cflag |= CS8;
newtio.c_cflag &= ~PARENB;
// cfsetispeed(&newtio, B115200);
// cfsetospeed(&newtio, B115200);
cfsetispeed(&newtio, B230400);
cfsetospeed(&newtio, B230400);
newtio.c_cflag &= ~CSTOPB;
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 0;
tcflush(fd, TCIOFLUSH);
if ((tcsetattr(fd, TCSANOW, &newtio)) != 0) {
perror("set opt error");
return -1;
}
uart_fd = fd;
printf("fd-open=%d\n",fd);
// InitSocket();
/**************************************************************************/
return fd;
}
更多推荐
所有评论(0)