linux串口(epoll 注意ET/LT使用)

void *thread_basedata(void* arg)
{

    int i, ret;

    // Allocate memory for read buffer, set size according to your needs
    unsigned char read_buf [DATA_LEN];

    // Normally you wouldn't do this memset() call, but since we will just receive
    // ASCII data for this example, we'll set everything to 0 so we can
    // call printf() easily.
    memset(&read_buf, '\0', sizeof(read_buf));

    // Read bytes. The behaviour of read() (e.g. does it block?,
    // how long does it block for?) depends on the configuration
    // settings above, specifically VMIN and VTIME
    int num_bytes = 0;

    int epfd, nfds;
    struct epoll_event event;
      struct epoll_event* events;

      events=calloc(10, sizeof(event));  
    epfd = epoll_create(10); // 创建epoll实例

    event.data.fd = uart3; // 添加标准输入到epoll
    event.events = EPOLLIN | EPOLLET;
     epoll_ctl(epfd, EPOLL_CTL_ADD, uart3, &event);

    for(;;)
    {
        ret = epoll_wait(epfd, events, 10, -1);// -1 :wait until it happen
        for(i=0; i<ret; i++)
        {
            if (events[i].data.fd == uart3) {
                num_bytes = read(events[i].data.fd, read_buf, DATA_LEN);

                if (num_bytes < 0) 
                {
                    UARTSendData((char*)"Error reading: %s\r\n", strerror(errno));
                    //while(1);
                }
                RTK_LOCATION_HandleRoverData(read_buf, num_bytes);
                WriteFIFO(&save_fifo,read_buf, num_bytes);
                num_bytes = 0;
            }
        }
    }
    close(uart3);
}

 

posted @ 2024-09-30 10:57  妖岭  阅读(16)  评论(0编辑  收藏  举报