/*--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
*******************************************************************************/