linux串口操作函数
创始人
2024-06-09 07:20:14
0

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 配置串口参数串口收发数据主要是要做好端口配置工作,需要包含,定义终端控制结构以及

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,设备数据

#p#

示例:

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

/* 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 */

timeout.tv_sec  = 10; timeout.tv_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参数是定义在的常量,一般是下表中的一个

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 #include

int fd;

int status;

ioctl(fd, TIOCMGET, &status);

Listing 6 - Dropping DTR with the TIOCMSET ioctl. #include #include

int fd;

int status;

ioctl(fd, TIOCMGET, &status);

status &= ~TIOCM_DTR;

ioctl(fd, TIOCMSET, status);

【编辑推荐】

  1. linux下面串口工具C-kermit
  2. linux大扫盲:linux之Tar命令常用参数
  3. Linux系统巧用NMAP来收集主机信息

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...