主题 : select-poll串口接收数据为什么被截断? 复制链接 | 浏览器收藏 | 打印
级别: 侠客
UID: 5655
精华: 0
发帖: 69
金钱: 345 两
威望: 69 点
贡献值: 0 点
综合积分: 138 分
注册时间: 2009-05-02
最后登录: 2018-10-29
楼主  发表于: 2018-10-29 11:08

 select-poll串口接收数据为什么被截断?

复制代码
  1. #include     <stdio.h>
  2. #include     <stdlib.h>
  3. #include     <unistd.h>
  4. #include     <sys/types.h>
  5. #include     <sys/stat.h>
  6. #include     <fcntl.h>
  7. #include     <termios.h>  
  8. #include     <errno.h>
  9. #include     <string.h>
  10. #include     <signal.h>
  11. #include    <pthread.h>
  12. #define FALSE 0
  13. #define TRUE  1
  14. int fd[2];
  15. pthread_t threads[10];
  16. char ucbuff[50];
  17. char buf[] = "hello WORLD!";
  18. static struct termios newtios,oldtios; /*termianal settings */
  19. static int saved_portfd=-1;            /*serial port fd */
  20. static void reset_tty_atexit(void)
  21. {
  22.     if(saved_portfd != -1)
  23.     {
  24.         tcsetattr(saved_portfd,TCSANOW,&oldtios);
  25.     }
  26. }
  27. /*cheanup signal handler */
  28. static void reset_tty_handler(int signal)
  29. {
  30.     if(saved_portfd != -1)
  31.     {
  32.         tcsetattr(saved_portfd,TCSANOW,&oldtios);
  33.     }
  34.     _exit(EXIT_FAILURE);
  35. }
  36. static int open_port(const char *portname)
  37. {
  38.     struct sigaction sa;
  39.     int portfd;
  40.     printf("opening serial port:%s\n",portname);
  41.     /*open serial port */
  42.     if((portfd=open(portname,O_RDWR | O_NOCTTY)) < 0 )
  43.     {
  44.            printf("open serial port %s fail \n ",portname);
  45.            return portfd;
  46.     }
  47.     /*get serial port parnms,save away */
  48.     tcgetattr(portfd,&newtios);
  49.     memcpy(&oldtios,&newtios,sizeof newtios);
  50.     /* configure new values */
  51.     cfmakeraw(&newtios); /*see man page */
  52.     newtios.c_iflag |=IGNPAR; /*ignore parity on input */
  53.     newtios.c_oflag &= ~(OPOST | ONLCR | OLCUC | OCRNL | ONOCR | ONLRET | OFILL);
  54.     newtios.c_cflag = CS8 | CLOCAL | CREAD;
  55.     newtios.c_cc[VMIN]=1; /* block until 1 char received */
  56.     newtios.c_cc[VTIME]=0; /*no inter-character timer */
  57.     /* 115200 bps */
  58.     cfsetospeed(&newtios,B115200);
  59.     cfsetispeed(&newtios,B115200);
  60.     /* register cleanup stuff */
  61.     atexit(reset_tty_atexit);
  62.     memset(&sa,0,sizeof sa);
  63.     sa.sa_handler = reset_tty_handler;
  64.     sigaction(SIGHUP,&sa,NULL);
  65.     sigaction(SIGINT,&sa,NULL);
  66.     sigaction(SIGPIPE,&sa,NULL);
  67.     sigaction(SIGTERM,&sa,NULL);
  68.     /*apply modified termios */
  69.     saved_portfd=portfd;
  70.     tcflush(portfd,TCIFLUSH);
  71.     tcsetattr(portfd,TCSADRAIN,&newtios);
  72.     return portfd;
  73. }
  74. /**
  75. *@breif     main()
  76. */
  77. int main(int argc, char **argv)
  78. {
  79.      fd_set  recv_fds;  /* 定义接收fds  一个存放文件描述符(file descriptor),即文件句柄的聚合,实际上是一long类型的数组 */
  80.         int     maxfd   = 0;    /* 定义最大句柄 */
  81.          int     fd_result;
  82.          struct  timeval tv;       /* 超时时间 */
  83.     char *dev[10]={"/dev/ttyS1", "/dev/ttyS2"};
  84.     unsigned int i,len;
  85.     printf("\n demo uart1/uart2 external loop back function \n");
  86.     for(i = 0; i < 2; i++)
  87.     {
  88.         if((fd[i] = open_port(dev[i]))<0)
  89.                return -1;
  90.     }
  91.         tv.tv_sec   = 10;       //设定超时时间
  92.         tv.tv_usec  = 0;          //10000us = 10ms
  93.     if(fd[0] > maxfd)                            /* maxfd 为最大值  */
  94.         {
  95.                 maxfd = fd[0];
  96.         }
  97.         if(fd[1] > maxfd)
  98.         {
  99.             maxfd = fd[1];
  100.         }
  101.     for(;;)
  102.         {  
  103.             /* 注意每次都要重新设置 */
  104.             FD_ZERO(&recv_fds);
  105.             FD_SET(fd[0],&recv_fds);                                /* 分别把句柄加入读监视集合里去   */
  106.             FD_SET(fd[1],&recv_fds);                                /* 分别把句柄加入读监视集合里去   */
  107.             fd_result = select(maxfd + 1, &recv_fds, NULL, NULL, &tv);  /* 注意是最大值加1                   */
  108.             if(fd_result < 0)
  109.             {
  110.                 printf("select err");                               /* select函数出错                 */
  111.             usleep(10000);
  112.             continue;
  113.             }
  114.          else if(fd_result == 0)
  115.          {
  116.             // printf("select time out \n"); /* 在设定的tv时间内,socket的状态没有发生变化 */
  117.             usleep(10000);
  118.             continue;
  119.          }
  120.          else                                                        /* 开始读数据 */
  121.          {
  122.             if(FD_ISSET(fd[0], &recv_fds))                      /* 先判断一下是哪个句柄可读 */
  123.                 {
  124.                 len  = read(fd[0],ucbuff,0xff);               /*  读取串口数据  */
  125.         printf("%d \n", len);
  126.                                 /*
  127.                                   ** 数据解析
  128.                                   */        
  129.             }  
  130.             if(FD_ISSET(fd[1], &recv_fds))                      /* 先判断一下是哪个句柄可读 */
  131.                 {
  132.                 len  = read(fd[1],ucbuff,0xff);                /*  读取串口数据  */
  133.                                 /*
  134.                                   ** 数据解析
  135.                                   */        
  136.                }
  137.          }
  138.         printf("%s \n", ucbuff);
  139.        }
  140.     
  141.     return(0);
  142.       
  143. }


问题:
调试助手向板子发送数据“hello world”,但是板子打印出来之每次接收到4个字符,,比如第一次只接收到