Linux下C语言串口通信

最近在做一个GPS项目,第一部分是将开发板和GPS用串口通信,接受GPS上传来的数据。

Linux下所有的设备都是以文件形式存储的,串口也是。

整个串口通信的流程图为:


所用到的头文件为:

 
#include <stdio.h>
#include <fcntl.h>
#include <assert.h>
#include <termios.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <errno.h>

用到了两个全局变量:

static int fd;
static int ret;

所需要的函数为:

 
int uart_open(int fd,const char *pathname);
int uart_config(int fd,int baude,int c_flow, int bits, char parity, int stop);
int safe_read(int fd, char *vptr, size_t len);
int uart_read(int fd, char *r_buf, size_t lenth);//串口读取数据
int uart_close(int fd);

打开串口:

 
int uart_open()
{
 
    assert(pathname);//检测串口路径是否存在
    fd = open(pathname,O_RDWR|O_NTCCY|O_NDELAY);//以只读形式、不将此终端作为此进程的终端控制器、非阻塞的形式打开串口
    if(fd == -1)
    {
 
        perror("uart open failed!");
        return -1;
    }
    if(fcntl(fd,F_SETFL,0)<0)//设置串口非阻塞,因为这里是以非阻塞形式打开的,所以第三个参数为0,后面会详细介绍fcntl函数
    {
 
        perror("fcntl failed!");
        return -1;
    }
    return fd;
}

配置串口:

配置串口非常重要,这里我做了一个配置串口函数的流程图


配置串口,就是设给termios结构体内的数据赋值,下面科普一下termios结构体


 
最小的termios结构体如下:struct termios{
         tcflag_t c_iflag;//输入模式       tcflag_t c_oflag;//输出模式       tcflag_t c_cflag;//控制模式       tcflag_t c_lflag;//本地模式(也叫局部模式)       cc_t           c_cc[NCCS];//控制字符特性}输入模式和输出模式都比较好理解,这里介绍一下控制模式和本地模式控制模式:主要用于控制终端设备的硬件设置。如:波特率,奇偶校验位,数据位,停止位,数据流控制等。本地模式:主要用来控制终端设备不同的特色。如:回显的各种方式,是否允许特殊字符,禁止刷新等。c_cc数组:特殊控制字元可提供使用者设定一些特殊的功能。         比如这里我们用到的c_cc[VTIME]设置等待时间,c_cc[VMIN]设置最小接受字符
用到的有关termios的函数:int tcgetattr(int fd, struct termios &termios_p);//用于获取termios结构体属性。成功返回0,失败返回非0int tcsetattr(int fd, int actions, const struct termios *termios_p);//用于激活termios结构体配置
int fcntl(int fd, int cmd, long arg)
  • 18
    点赞
  • 105
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值