linux串口编程带超时的read函数

/*--01--*//*--02--*//*--03--*//*--04--*//*--05--*//*--06--*//*--07--*//*--08--*/
/*******************************************************************************
 *@文件名:
 *@描  述:
 *@版  本:
 *@平  台:
 *@作  者:
 *@记  录:
*******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include <serial_port.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

/* Private macro -------------------------------------------------------------*/

/* Public fuctions -----------------------------------------------------------*/

/* Private functions ---------------------------------------------------------*/
static int interface_open(const char *port, speed_t baudrate);
static int interface_read(int fd, void *vptr, size_t n, struct timeval *timeout);

/* Private typedef -----------------------------------------------------------*/

/* External variables --------------------------------------------------------*/

/* Public variables ----------------------------------------------------------*/
struct SerialPort_Interface SerialPort = {
    .open = interface_open,
    .read = interface_read,
};

/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/
/*******************************************************************************
*@功    能:
*@备    注:
*******************************************************************************/
static int interface_open(const char *port, speed_t baudrate)
{
    int fd;
    struct termios options;

    fd = open(port, O_RDWR|O_NOCTTY|O_NDELAY);

    if (fd == -1) {
        printf("can't open %s", port);
        return -1;
    }

    tcgetattr(fd, &options);

    cfsetispeed(&options, baudrate);
    cfsetospeed(&options, baudrate);
    options.c_cflag |= CLOCAL | CREAD;
    options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
    options.c_oflag &= ~OPOST;
    options.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP);
    options.c_iflag &= ~(IXON | IXOFF | IXANY);

    if (tcsetattr(fd, TCSANOW, &options) != 0) {
        printf("%s tcsetattr failed\n", port);
        close(fd);
        return -1;
    }

    return fd;
}

/*******************************************************************************
*@功    能:
*@备    注:
*******************************************************************************/
static int interface_read(int fd, void *vptr, size_t n, struct timeval *timeout)
{
    int     ret;
    fd_set  rset;
	char	*ptr;
	size_t	nleft;
	ssize_t	nread;

    FD_ZERO(&rset);
    FD_SET(fd, &rset);

	ptr = (char *)vptr;
	nleft = n;
    //Linux3.2.0之后版本select的超时参数会被更新,其余平台请注意该用法是否适用
    while (1) {
        ret = select(fd+1, &rset, NULL, NULL, timeout);

        if (ret <= 0) {
            if (errno == EINTR)
                continue;
            else
                return(-1);
        }

        if (FD_ISSET(fd, &rset)) {
            if ((nread = read(fd, ptr, nleft)) < 0)
                continue;
            nleft -= nread;
            ptr   += nread;
            if (nleft > 0)
                continue;
            else
                return(0);
        }

        continue;
    }
}

/*******************************************************************************
* End Of File         
*******************************************************************************/
/*--01--*//*--02--*//*--03--*//*--04--*//*--05--*//*--06--*//*--07--*//*--08--*/
/*******************************************************************************
 *@文件名:
 *@描  述:
 *@版  本:
 *@平  台:
 *@作  者:
 *@记  录:
*******************************************************************************/

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __SERIAL_PORT_H
#define __SERIAL_PORT_H

/* Includes ------------------------------------------------------------------*/
#include <termio.h>
#include <stdlib.h>

/* Macro ---------------------------------------------------------------------*/

/* Exported types ------------------------------------------------------------*/
struct SerialPort_Interface {
    int (*open)(const char *port, speed_t baudrate);
    int (*read)(int fd, void *vptr, size_t n, struct timeval *timeout);
};

/* Public variables ----------------------------------------------------------*/
extern struct SerialPort_Interface SerialPort;

/* Exported functions --------------------------------------------------------*/
/* IO operation functions *****************************************************/


#endif

/*******************************************************************************
* End Of File         
*******************************************************************************/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值