深入探究Linux下的termios.h库函数 (linux termios.h)

termios.h是一个系统头文件,用于Linux系统中的串口通信操作。这个库函数提供了一系列的命令和函数调用,用于设置终端I/O接口的控制特性。

本文将,理解它的作用,掌握使用方法以及实例应用等内容。

一、termios.h库函数的作用

在Linux中,串口的输入和输出是通过终端I/O设备文件进行操作的,而termios.h库提供了一系列的函数调用,用于定义和操作串口的特性和行为。这里列举几个termios.h库函数的作用:

1. 设置串口的波特率、奇偶校验位、数据位和停止位等。

2. 使能或禁用串口的本地回显和输入模式等。

3. 对终端I/O进行控制和管理,以及对串口缓冲区进行数据输入和输出。

4. 监控串口的状态和错误信息,如输入/输出错误、数据帧错误、奇偶校验错误等。

二、termios.h库函数的使用方法

使用termios.h库函数可以分为以下几个步骤:

1. 打开串口

打开串口用open()函数,需要传入串口设备文件名称和打开模式参数,例如:

int fd = open(“/dev/ttyS0”, O_RDWR | O_NOCTTY | O_NDELAY);

2. 配置串口特性和行为

使用tcgetattr()和tcsetattr()函数可以获取和设置串口的属性,例如:

struct termios options;

tcgetattr(fd, &options); // 获取当前终端属性

options.c_cflag |= (CLOCAL | CREAD);

options.c_cflag &= ~CSIZE;

options.c_cflag |= CS8;

options.c_cflag &= ~PARENB;

options.c_cflag &= ~CSTOPB;

options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);

options.c_oflag &= ~OPOST;

options.c_cc[VMIN] = 1;

options.c_cc[VTIME] = 0;

tcsetattr(fd, TCSANOW, &options); //应用新的终端属性

这些代码段将串口属性定义为8位无奇偶校验、1位停止位、无本地回显和输入模式等。

3. 读/写串口数据

使用read()和write()函数可以从串口读取和写入数据,例如:

char buf[100];

read(fd, buf, sizeof(buf));

write(fd, “hello”, 5);

4. 关闭串口

使用close()函数可以关闭串口,例如:

close(fd);

三、实例应用

根据上述使用方法,可以编写实例应用:

#include

#include

#include

#include

#include

#include

int mn()

{

int fd;

char buf[100];

fd = open(“/dev/ttyS0”, O_RDWR | O_NOCTTY | O_NDELAY);

if (fd

perror(“open”);

exit(1);

}

fcntl(fd, F_SETFL, 0);

struct termios options;

tcgetattr(fd, &options);

cfsetispeed(&options, B9600);

cfsetospeed(&options, B9600);

options.c_cflag |= (CLOCAL | CREAD);

options.c_cflag &= ~CSIZE;

options.c_cflag |= CS8;

options.c_cflag &= ~PARENB;

options.c_cflag &= ~CSTOPB;

options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);

options.c_oflag &= ~OPOST;

options.c_cc[VMIN] = 1;

options.c_cc[VTIME] = 0;

if (tcsetattr(fd, TCSANOW, &options)

perror(“tcsetattr”);

exit(1);

}

while(1) {

read(fd, buf, sizeof(buf));

printf(“recv => %s\n”, buf);

}

close(fd);

return 0;

}

通过以上代码,可以打开串口/dev/ttyS0,定义波特率为9600,接收并打印数据。当然,更多实际应用可以参考实际需求编写。

相关问题拓展阅读:

ARM9里的LINUX系统的串口设备节点ttys#用fd open打不开是怎么回事,有没有知道的错误提示

你先做凯TELNET到ARM上,用纯饥唤命令echo hello > /dev/ttyS0,看肢缓看串口是否存在啊!

Linux c 串口通信 nread=0,求分析

经过验证,串口应该没有数据读上来,所以导致知缓册读到的数据的数量是0,打印出hello是因为buff本搭宏身初始化就是Hello,所以显示是Hello

#include

#include

#include

#include

#include

#include

#include

int main()

{

static char filename=”t1.txt” ;

int fd;

int nread, i;

char buff = “Hello\n”;

if((fd = open(filename,O_RDON)) 0 )

// =i ;

printf(“哪帆nread=%d,%s\n”,nread, buff);

close(fd);

return 0;

}

串口没有接收到数据,接收缓冲区里面就没有数据,nread肯定就是0啊,你得用个调试软件发送一组数据,然后再接收.

另外参兆埋考一下我腔猜耐的串口初始化函数,这个伍春是肯定没问题的:

int InitComm(char *devname)

{

struct termios new_termio;

int fd;

/* Comm serial port not opened */

fd = open (devname, O_RDWR | O_NOCTTY);

if(fd

{

printf(“Open Serial Port Device %s error no %d\n”,

devname , fd );

return 0;

}

bzero ((unsigned char *) &new_termio, sizeof (new_termio));

new_termio.c_cflag = B| CS8 | CLOCAL | CREAD;

new_termio.c_iflag = IGNPAR; //ingorn parity check error

new_termio.c_oflag = 0;

new_termio.c_lflag = 0;

new_termio.c_cc = 0;

new_termio.c_cc = 0;

tcflush (fd, TCIFLUSH);

tcsetattr (fd, TCSANOW, &new_termio);//设置流控制,波涛率,校验位

return fd;

}

fd = InitComm(“/dev/ttyS0”);

这什么阿,看不懂。

linux termios.h的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于linux termios.h,深入探究Linux下的termios.h库函数,ARM9里的LINUX系统的串口设备节点ttys#用fd open打不开是怎么回事,有没有知道的错误提示,Linux c 串口通信 nread=0,求分析的信息别忘了在本站进行查找喔。


数据运维技术 » 深入探究Linux下的termios.h库函数 (linux termios.h)