串口初始化
2016-04-06 18:07
351 查看
在开发过程中常用的串口,初始化程序,以及封装的字符串发送函数
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <string.h>
static struct termios option_old;
/************************************************************
*函数名称: int uart_init(char *devname)
*函数功能: 串口初始化程序
*函数参数: devname 串口设备文件序号
*函数返回: 串口设备文件描述符
***********************************************************/
int uart_init(char *devname)
{
int uart_fd;
struct termios option_new;
uart_fd = open(devname, O_RDWR|O_NDELAY); //O_NDELAY 不阻塞
if(uart_fd < 0)
{
DebugL("open_dev error!");
_exit(-1);
}
tcgetattr(uart_fd, &option_old); //保存串口属性
tcgetattr(uart_fd, &option_new);
cfsetispeed(&option_new, B115200); //波特率为115200
cfsetospeed(&option_new, B115200); //波特率为115200
/*option_new.c_cflag &= ~CSIZE; //设置数据位时先关闭历史设置
option_new.c_cflag |= CS8; //数据位为8位
option_new.c_cflag &= ~CSTOPB; //1位停止位
option_new.c_cflag &= ~PARENB; //无奇偶校验位
option_new.c_lflag &= ~(ICANON); //非标准模式
// option_new.c_lflag &= ~ECHO; //关回显,在使用GPRS模组时需关回显
option_new.c_lflag |= ECHO; */ //开回显
/***********能接收0x03的配置***************/
option_new.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
option_new.c_oflag &= ~OPOST;
option_new.c_cflag |= CLOCAL | CREAD;
option_new.c_lflag &= ~(ICANON | ECHOE | ISIG);
tcsetattr(uart_fd, TCSANOW, &option_new);
return uart_fd;
}
/************************************************************
*函数名称: void uart_uninit(int uart_fd)
*函数功能: 串口反初始化程序
*函数参数: uart_fd 串口设备文件描述符
*函数返回: 无
***********************************************************/
void uart_uninit(int uart_fd)
{
/*还原串口属性*/
tcsetattr(uart_fd, TCSANOW, &option_old);
/*关闭串口*/
close(uart_fd);
}
/************************************************************
*函数名称: void uart_send_str(int uart_fd, char *str)
*函数功能: 串口发送字符串
*函数参数: uart_fd 串口设备文件描述符
* str:待发送的字符
*函数返回: 无
***********************************************************/
void uart_send_str(int uart_fd, char *str)
{
int ret;
ret = write(uart_fd, str, strlen(str));
if(ret < 0)
{
DebugL("write error!");
}
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <string.h>
static struct termios option_old;
/************************************************************
*函数名称: int uart_init(char *devname)
*函数功能: 串口初始化程序
*函数参数: devname 串口设备文件序号
*函数返回: 串口设备文件描述符
***********************************************************/
int uart_init(char *devname)
{
int uart_fd;
struct termios option_new;
uart_fd = open(devname, O_RDWR|O_NDELAY); //O_NDELAY 不阻塞
if(uart_fd < 0)
{
DebugL("open_dev error!");
_exit(-1);
}
tcgetattr(uart_fd, &option_old); //保存串口属性
tcgetattr(uart_fd, &option_new);
cfsetispeed(&option_new, B115200); //波特率为115200
cfsetospeed(&option_new, B115200); //波特率为115200
/*option_new.c_cflag &= ~CSIZE; //设置数据位时先关闭历史设置
option_new.c_cflag |= CS8; //数据位为8位
option_new.c_cflag &= ~CSTOPB; //1位停止位
option_new.c_cflag &= ~PARENB; //无奇偶校验位
option_new.c_lflag &= ~(ICANON); //非标准模式
// option_new.c_lflag &= ~ECHO; //关回显,在使用GPRS模组时需关回显
option_new.c_lflag |= ECHO; */ //开回显
/***********能接收0x03的配置***************/
option_new.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
option_new.c_oflag &= ~OPOST;
option_new.c_cflag |= CLOCAL | CREAD;
option_new.c_lflag &= ~(ICANON | ECHOE | ISIG);
tcsetattr(uart_fd, TCSANOW, &option_new);
return uart_fd;
}
/************************************************************
*函数名称: void uart_uninit(int uart_fd)
*函数功能: 串口反初始化程序
*函数参数: uart_fd 串口设备文件描述符
*函数返回: 无
***********************************************************/
void uart_uninit(int uart_fd)
{
/*还原串口属性*/
tcsetattr(uart_fd, TCSANOW, &option_old);
/*关闭串口*/
close(uart_fd);
}
/************************************************************
*函数名称: void uart_send_str(int uart_fd, char *str)
*函数功能: 串口发送字符串
*函数参数: uart_fd 串口设备文件描述符
* str:待发送的字符
*函数返回: 无
***********************************************************/
void uart_send_str(int uart_fd, char *str)
{
int ret;
ret = write(uart_fd, str, strlen(str));
if(ret < 0)
{
DebugL("write error!");
}
}
相关文章推荐
- /proc/sysrq-trigger文件的功能
- Android酷炫实用的开源框架(UI框架)
- webservice开发
- GDB 反向调试(Reverse Debugging)
- 问题1:javascript的函数对象问题
- mybatis generator maven-plugin 集成
- numpy 学习,里面有converters参数的使用方法
- IP格式合法问题研究
- mysql两大存储引擎的区别与选择
- Qt Quick实现的涂鸦程序
- LUA IO库
- 使用word模板生成pdf文件
- python为什么有私有方法和变量
- Android 发送广播更加方便快捷高效的方式
- 数据库索引
- In App Purchase(应用内购)
- shell if手册
- .bar : 嵌入子元素
- Ubuntu 15.10安装ns2.35+nam
- 通过一个实例理解 offsetLeft,offsetTop; offsetWidth , offsetHeight