Chinaunix
标题:
linux串口poll多路读取的数据被截断问题
[打印本页]
作者:
stm32f103vct
时间:
2018-10-26 15:26
标题:
linux串口poll多路读取的数据被截断问题
本帖最后由 stm32f103vct 于 2018-10-26 15:28 编辑
/****************************************************************************
* *
* Copyright (c) 2014 Nuvoton Technology Corp. All rights reserved. *
* *
****************************************************************************/
/****************************************************************************
*
* FILENAME
* uart_test.c
*
* VERSION
* 1.0
*
* DESCRIPTION
* This is the test program used to test the UARTs on NUC970 EV board
*
* DATA STRUCTURES
* None
*
* FUNCTIONS
* None
*
* HISTORY
*
*
* REMARK
* None
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <pthread.h>
#define FALSE 0
#define TRUE 1
int fd[2];
pthread_t threads[10];
char ucbuff[50];
char buf[] = "hello ZLG!";
static struct termios newtios,oldtios; /*termianal settings */
static int saved_portfd=-1; /*serial port fd */
static void reset_tty_atexit(void)
{
if(saved_portfd != -1)
{
tcsetattr(saved_portfd,TCSANOW,&oldtios);
}
}
/*cheanup signal handler */
static void reset_tty_handler(int signal)
{
if(saved_portfd != -1)
{
tcsetattr(saved_portfd,TCSANOW,&oldtios);
}
_exit(EXIT_FAILURE);
}
static int open_port(const char *portname)
{
struct sigaction sa;
int portfd;
printf("opening serial port:%s\n",portname);
/*open serial port */
if((portfd=open(portname,O_RDWR | O_NOCTTY)) < 0 )
{
printf("open serial port %s fail \n ",portname);
return portfd;
}
/*get serial port parnms,save away */
tcgetattr(portfd,&newtios);
memcpy(&oldtios,&newtios,sizeof newtios);
/* configure new values */
cfmakeraw(&newtios); /*see man page */
newtios.c_iflag |=IGNPAR; /*ignore parity on input */
newtios.c_oflag &= ~(OPOST | ONLCR | OLCUC | OCRNL | ONOCR | ONLRET | OFILL);
newtios.c_cflag = CS8 | CLOCAL | CREAD;
newtios.c_cc[VMIN]=1; /* block until 1 char received */
newtios.c_cc[VTIME]=0; /*no inter-character timer */
/* 115200 bps */
cfsetospeed(&newtios,B115200);
cfsetispeed(&newtios,B115200);
/* register cleanup stuff */
atexit(reset_tty_atexit);
memset(&sa,0,sizeof sa);
sa.sa_handler = reset_tty_handler;
sigaction(SIGHUP,&sa,NULL);
sigaction(SIGINT,&sa,NULL);
sigaction(SIGPIPE,&sa,NULL);
sigaction(SIGTERM,&sa,NULL);
/*apply modified termios */
saved_portfd=portfd;
tcflush(portfd,TCIFLUSH);
tcsetattr(portfd,TCSADRAIN,&newtios);
return portfd;
}
/**
*@breif main()
*/
int main(int argc, char **argv)
{
fd_set recv_fds; /* 定义接收fds 一个存放文件描述符(file descriptor),即文件句柄的聚合,实际上是一long类型的数组 */
int maxfd = 0; /* 定义最大句柄 */
int fd_result;
struct timeval tv; /* 超时时间 */
char *dev[10]={"/dev/ttyS1", "/dev/ttyS2"};
unsigned int i,len;
printf("\n demo uart1/uart2 external loop back function \n");
for(i = 0; i < 2; i++)
{
if((fd[i] = open_port(dev[i]))<0)
return -1;
}
tv.tv_sec = 10; //设定超时时间
tv.tv_usec = 0; //10000us = 10ms
if(fd[0] > maxfd) /* maxfd 为最大值 */
{
maxfd = fd[0];
}
if(fd[1] > maxfd)
{
maxfd = fd[1];
}
for(;;)
{
/* 注意每次都要重新设置 */
FD_ZERO(&recv_fds);
FD_SET(fd[0],&recv_fds); /* 分别把句柄加入读监视集合里去 */
FD_SET(fd[1],&recv_fds); /* 分别把句柄加入读监视集合里去 */
fd_result = select(maxfd + 1, &recv_fds, NULL, NULL, &tv); /* 注意是最大值加1 */
if(fd_result < 0)
{
printf("select err"); /* select函数出错 */
usleep(10000);
continue;
}
else if(fd_result == 0)
{
// printf("select time out \n"); /* 在设定的tv时间内,socket的状态没有发生变化 */
usleep(10000);
continue;
}
else /* 开始读数据 */
{
if(FD_ISSET(fd[0], &recv_fds)) /* 先判断一下是哪个句柄可读 */
{
len = read(fd[0],ucbuff,0xff); /* 读取串口数据 */
printf("%d \n", len);
/*
** 数据解析
*/
}
if(FD_ISSET(fd[1], &recv_fds)) /* 先判断一下是哪个句柄可读 */
{
len = read(fd[1],ucbuff,0xff); /* 读取串口数据 */
/*
** 数据解析
*/
}
}
printf("%s \n", ucbuff);
}
return(0);
}
复制代码
结果如图数据被截断:
23840478.png
(4.28 KB, 下载次数: 88)
下载附件
2018-10-26 15:28 上传
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2