Chinaunix首页 | 论坛 | 博客
  • 博客访问: 148731
  • 博文数量: 54
  • 博客积分: 1732
  • 博客等级: 上尉
  • 技术积分: 520
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-23 23:29
文章分类

全部博文(54)

文章存档

2011年(3)

2010年(26)

2009年(25)

分类: LINUX

2009-10-25 00:27:24

串口操作函数

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,设备数据
示例:
  假定我们要从一个串口和一个 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);

转自http://wushuan.cublog.cn
阅读(1561) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~