教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 文库大全 > 高中教育 >

linux串口操作函数

来源:网络收集 时间:2026-08-28
导读: 主要讲解了linux串口操作函数。 1.打开串口: fd = open(/dev/ttyf1, O_RDWR | O_NOCTTY | O_NDELAY); fcntl(fd, F_SETFL, 0); O_NOCTTY 选项防止程序受键盘控制中止操作键等影响. O_NDELAY 告诉 UNIX 不必另一端端口是否启用.(检测 DCD信号线状态) 2.往串口

主要讲解了linux串口操作函数。

1.打开串口: fd = open("/dev/ttyf1", O_RDWR | O_NOCTTY | O_NDELAY); fcntl(fd, F_SETFL, 0); O_NOCTTY 选项防止程序受键盘控制中止操作键等影响. O_NDELAY 告诉 UNIX 不必另一端端口是否启用.(检测 DCD信号线状态)

2.往串口发送数据n = write(fd, "ATZ\r", 4);

3.从串口读取数据当以原始数据模式(raw data mode)打开串口时,read 系统调用将不管串口输入缓存里有多少字符可读都返回.若没有数据,则阻塞直至有字符到来,或定时器超时.串口设置这个选项后,read 调用都是立即返回.没有数据可读时,read 返回 0 fcntl(fd, F_SETFL, FNDELAY);

解除这个功能是

fcntl(fd, F_SETFL, 0); 4.关闭串口

close(fd);

二.标准的 POSIX 配置串口参数串口收发数据主要是要做好端口配置工作,需要包含<termios.h>,定义终端控制结构以及

POSIX控制函数

termios结构

Table 3 - Termios Structure Members

Member Description

c_cflag Control options

c_lflag Line options

c_iflag Input options

c_oflag Output options

c_cc Control characters

c_ispeed Input baud (new interface)

c_ospeed Output baud (new interface)

struct termios termios_old,termios_new;

1)获取串口属性

tcgetattr(fdcom, &termios_old);

2)配置输入速率

cfsetispeed(&termios_new, baudrate); cfsetospeed(&termios_new, baudrate); 3) 控制模式,保证程序不会成为端口的占有者termios_new.c_cflag |= CLOCAL;控制模式,使能端口读取输入的数据termios_new.c_cflag |= CREAD; 4) 控制模式,屏蔽字符大小位,设置串口传输数据所用的位数termios_new.c_cflag &= ~CSIZE; termios_new.c_cflag |= CS5; //CS6,CS7,CS8

5)奇偶校验parity check

//无奇偶校验

termios_new.c_cflag &= ~PARENB ;

//偶校验

termios_new.c_cflag |= PARENB; termios_new.c_cflag &= PARODD;

//奇校验

termios_new.c_cflag |= PARENB; termios_new.c_cflag |= PARODD;

6)设置停止位

termios_new.c_cflag |= CSTOPB; //2stop bits termios_new.c_cflag &= ~CSTOPB; //1 stop bits.

7)其他属性配置

termios_new.c_oflag &= ~OPOST; //输出模式,原始数据输出termios_new.c_cc[VMIN] = 1; //控制字符,所要读取字符的最小数量termios_new.c_cc[VTIME] = 1;//控制字符,读取第一个字符的等待时间,以 0.1 妙为单



8)设置新属性

tcsetattr(fdcom, TCSANOW, &termios_new);

// TCSANOW:所由改变立即生效

//TCSADRAIN:等待所有东西都被发送出去后设


//TCSAFLUSH:将输入输出buffer全部溢出后设置

采用 select 系统调用读取串口数据跟其他 socket,设备数据

示例:

假定我们要从一个串口和一个 socket 读取数据.需要判断每个文件描述符的输入数据情况,但 10 妙内无数据的话,需

主要讲解了linux串口操作函数。

要通知用户没有数据可读.

/* Initialize the input set */

FD_ZERO(input);

FD_SET(fd, input); FD_SET(socket, input);

max_fd = (socket > fd ? socket : fd) + 1;

/* Initialize the timeout structure */

http://doc.guandang.net_sec = 10; http://doc.guandang.net_usec = 0;

/* Do the select */

n = select(max_fd, NULL, NULL, ;

/* See if there was an error */

if (n 0)

perror("select failed");

else if (n == 0)

puts("TIMEOUT");

else

{

/* We have input */

if (FD_ISSET(fd, input))

process_fd();

if (FD_ISSET(socket, input))

process_socket();

三.unix/linux下,采用 ioctl函数来实现串口配置功能int ioctl(int fd, int request, ...); fd 是串口描述符,request参数是定义在<termios.h>的常量,一般是下表中的一个

Table 10 - IOCTL Requests for Serial Ports

Request Description POSIX Function

TCGETS

Gets the current serial

port settings.

tcgetattr

TCSETS

Sets the serial port

settings immediately. tcsetattr(fd, TCSANOW, &options)

TCSETSF

Sets the serial port

settings after flushing the

input and output buffers. tcsetattr(fd, TCSAFLUSH, &options)

TCSETSW

Sets the serial port

settings after allowing

the input and output

buffers to drain/empty. tcsetattr(fd, TCSADRAIN, &options)

TCSBRK

Sends a break for the

given time. tcsendbreak, tcdrain

TCXONC

Controls software flow

control.

tcflow

TCFLSH

Flushes the input and/or

output queue.

tcflush

TIOCMGET

Returns the state of the

"MODEM" bits.

None

TIOCMSET

Sets the state of the

"MODEM" bits.

None

FIONREAD

Returns the number of

bytes in the input buffer.

None

为获取状态位,调用 ioctl函数,用一个整数来存放位指针.

Listing 5 - Getting the MODEM status bits. #include <unistd.h> #include <termios.h>

int fd;

int status;

ioctl(fd, TIOCMGET, &status);

Listing 6 - Dropping DTR with the TIOCMSET ioctl. #include <unistd.h> #include &
lt;termios.h>

int fd;

int status;

ioctl(fd, TIOCMGET, &status);

status &= ~TIOCM_DTR;

ioctl(fd, TIOCMSET, status);


…… 此处隐藏:1737字,全部文档内容请下载后查看。喜欢就下载吧 ……
linux串口操作函数.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wenku/1731049.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)