linux串口操作函数

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

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来收集主机信息
责任编辑:赵宁宁 来源: chinaitlab
相关推荐

2011-06-22 17:49:35

Linux Qt 串口

2009-08-25 15:59:28

C#串口操作

2020-10-08 10:10:51

Linux系统编程信号集

2009-08-25 17:02:20

C#串口操作

2010-04-22 16:01:48

Aix操作系统串口

2009-09-23 10:26:12

linux串口工具C-kermit

2023-03-08 15:55:53

Linux驱动鸿蒙

2010-06-11 11:35:55

Linux串口测试工具

2017-03-13 16:46:11

Linuxminicomusb串口

2009-12-25 16:53:06

Linux操作系统

2010-06-07 14:05:38

Linux串口测试工具

2012-09-20 09:43:33

Linux服务器串口Linux

2011-06-22 17:36:50

QT Linux 串口

2010-06-13 17:12:10

Linux串口测试工具

2014-08-14 09:25:31

Linux串口

2009-04-11 15:12:24

VxWorks串口例子

2010-05-28 10:53:07

Linux串口测试工具

2009-12-14 17:46:40

Linux桌面操作系统

2011-01-11 14:56:51

Linux基本操作

2010-06-18 10:42:51

Linux Acces
点赞
收藏

51CTO技术栈公众号