linux串口读写例子

公众号

欢迎扫码关注本人微信公众号:公众号上分享更多嵌入式知识和资料,分享个人学习嵌入式的心得体会。欢迎大家一起来玩呀。
在这里插入图片描述

#include <stdio.h> 
#include <string.h>
#include <errno.h>
#include <unistd.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <fcntl.h> 
#include <termios.h> 
#include <unistd.h>
#define W_BUF "quanjianjun<191848727@qq.com> \n"


/**
 * @brief 打开串口端口               
 * @param char *path      传入串口节点的路径
 * @return                返回句柄
 */
int init_uart_port(char *path)
{
    int tty_fd = -1;
    int rv = -1;
    struct termios options; 
    
    printf("open tty :%s \n",path);
    tty_fd = open(path,O_RDWR|O_NOCTTY|O_NDELAY); //打开串口设备
 
    if(tty_fd < 0) 
    { 
        printf("open tty failed:%s\n", strerror(errno));
        goto cleanup;
    } 
    printf("open devices sucessful!\n"); 
    memset(&options, 0, sizeof(options));
    rv = tcgetattr(tty_fd, &options); //获取原有的串口属性的配置
 
    if(rv != 0) 
    { 
        printf("tcgetattr() failed:%s\n",strerror(errno));
        goto cleanup;
    }
 
    options.c_cflag|=(CLOCAL|CREAD ); // CREAD 开启串行数据接收,CLOCAL并打开本地连接模式
    options.c_cflag &=~CSIZE;// 先使用CSIZE做位屏蔽  
    options.c_cflag |= CS8; //设置8位数据位
    options.c_cflag &= ~PARENB; //无校验位

    options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);   //读串口需要设置这个标志位,不设置将无法读到串口消息
 
 
    /* 设置115200波特率  */
    printf("set baud rate speed\n");
    cfsetispeed(&options, B115200);
    cfsetospeed(&options, B115200);
 
    options.c_cflag &= ~CSTOPB;/* 设置一位停止位; */
 
    options.c_cc[VTIME] = 30; /* 非0的时候阻塞,阻塞等待时间*/
    options.c_cc[VMIN]  = 1; /* 阻塞读取时的最小字符数*/
    tcflush(tty_fd ,TCIFLUSH);/* tcflush清空终端未完成的输入/输出请求及数据;TCIFLUSH表示清空正收到的数据,且不读取出来 */

    printf("set tcsetattr \n");
    if((tcsetattr(tty_fd, TCSANOW,&options))!=0)
    {
        printf("tcsetattr failed:%s\n", strerror(errno));
        goto cleanup;
    }

    if (fcntl(tty_fd, F_SETFL, 0) < 0)     /* 恢复串口为阻塞状态 */
    {
        printf("fcntl F_SETFL\n");
    }

    return tty_fd;
    
cleanup:
    close(tty_fd);
    return -1;    
}

/**
 * @brief  关闭串口端口              
 * @param  int tty_fd 句柄
 * @return void               
 */
void close_uart_port(int tty_fd)
{
    close(tty_fd);
} 
 
int main(int argc, char **argv)
{
    int tty_fd = -1;
    int ret    = -1;
    char buf[50];
      	
	if(!(argc >= 2))
	{
		printf("usage: input /dev/ttyxxx\n");
		return 0;
	}

	tty_fd = init_uart_port(argv[1]);
	if (tty_fd == -1) {
	    printf("err \n");
	    return 0;
	}
	  
    while(1)
    {
		printf("write:%s \n",W_BUF);
        ret = write(tty_fd, W_BUF,strlen(W_BUF));
        if(ret < 0)
        {
            printf("Write() error:%s\n",strerror(errno));
        }

        ret = read(tty_fd, buf,1);
        if(ret > 0)
        {
            buf[ret] = '\0';
            printf("read() length:%d %s\n",ret,buf);
        }        
    }
    
    return 0;
}

可能出现的错误

提示“Resource temporarily unavailable”的原因及解决办法
问题:Linux环境下编程时,在读串口时,出现“Resource temporarily unavailable”的错误提示。

原因:串口设置成了非阻塞模式,但是没有用select去判断是否有数据到来就去读。

解决方法:

要么将串口设置成阻塞模式,要么使用select。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值