linux串口操作函数
主要讲解了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);
相关推荐:
- [高中教育]电子线路高频非线性部分2.1
- [高中教育]中班美术活动——我的小手
- [高中教育]常用三极管参数大全
- [高中教育]计算机常见故障及解决办法
- [高中教育]风机基础环水平度控制方法探讨
- [高中教育]机械安全工程(专升本)阶段性作业3
- [高中教育]2009年安徽省高考语文考试说明刍议
- [高中教育]unit5 let's eat公开课教案设
- [高中教育]计算机网络原理课后习题答案
- [高中教育]2016-2022年中国新能源市场研究与投资
- [高中教育]2015-2020年中国会议行业市场评估及投
- [高中教育]经销商大会峰会主持人串词开场白
- [高中教育]2014新版北师大数学三年级上册小熊购物
- [高中教育]七年级第一学期体育与健康全套教案
- [高中教育]第三章:国际金融市场
- [高中教育]六年级下册数学单元测试-2.比例 北师大
- [高中教育]2016年上海海事大学法学院624刑法之《
- [高中教育]中国碳化钙产业竞争现状及未来五年投资
- [高中教育]网络时代,我们怎么玩
- [高中教育]圆锥曲线——高中数学基础知识与典型例
- 高集医院世界艾滋病宣传日活动方案
- 苏教版六年级英语上册期末试卷含答案
- 全民枪战生化英雄模式幽灵怎么玩 生化
- 灿烂的宋元文化一导学案
- 第2章货币资金与应收款项
- 北师大版八年级下册数学第三章《分式》
- 浅析高分子材料成型加工技术
- 华南理工大学2013年度共青团先进集体及
- 教师资格科目二小学教案模板(共合集)
- 工程扩建可研报告
- 中华人民共和国海事局2014年度招录公务
- 提高农村小学生作文能力的教学尝试
- 徒手心肺复苏术操作步骤
- 毛概试题库7-15章
- 2014-2015学年度(上)初中班主任工作计
- 企业驾驶员安全生产责任书
- 第07章 不等式测试题-2016年高考文科数
- 医疗器械经营企业工作程序
- 考研英语必背36篇_彩版_精华
- 初中9月13-15假期作业 (1)




