首页 > 其他分享 >不用定时器用接收超时做超时处理

不用定时器用接收超时做超时处理

时间:2022-08-18 10:13:09浏览次数:88  
标签:rt RT p0 定时器 接收 recvdatafile ringbuffer 超时 EVENT

  • [原始代码 ]
while (1)
{
    /* 接收感兴趣的事件WRITE_EVENT,以永久等待方式去接收 */
    if (rt_event_recv(recvdata_event, WRITE_EVENT, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, &set) != RT_EOK)
        continue;
    do
    {
        /* 接收感兴趣的事件WRITE_EVENT,以1000ms超时方式接收 */
        if (rt_event_recv(recvdata_event, WRITE_EVENT, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, rt_tick_from_millisecond(1000), &set) == RT_EOK)
        {
            /* 判断写入的数据大小到没到所设置的ringbuffer的阈值 */
            if (rt_ringbuffer_data_len(recvdatabuf) > THRESHOLD)
            {
                /* 到阈值就直接写数据 */
                recvdatafile_p0 = fopen("recvdata_p0.csv", "ab+");
                if (recvdatafile_p0 != RT_NULL)
                {
                    while(rt_ringbuffer_data_len(recvdatabuf))
                    {
                        size = rt_ringbuffer_get(recvdatabuf, (rt_uint8_t *)writebuffer, THRESHOLD);
                        fwrite(writebuffer, 1, size, recvdatafile_p0);
                    }
                    fclose(recvdatafile_p0);
                }
            }
            /* 阈值没到就继续接收感兴趣的事件WRITE_EVENT,以1000ms超时方式接收 */
            continue;
        }
        /* 1000ms到了,还没有收到感兴趣的事件,这时候不管到没到阈值,直接写 */
        recvdatafile_p0 = fopen("recvdata_p0.csv", "ab+");
        if (recvdatafile_p0 != RT_NULL)
        {
            while(rt_ringbuffer_data_len(recvdatabuf))
            {
                size = rt_ringbuffer_get(recvdatabuf, (rt_uint8_t *)writebuffer, THRESHOLD);
                fwrite(writebuffer, 1, size, recvdatafile_p0);
            }
            fclose(recvdatafile_p0);
        }
    } while(0);
}
  • [ 少了两行代码]
while (1)
{
    do
    {
        /* 接收感兴趣的事件WRITE_EVENT,以1000ms超时方式接收 */
        if (rt_event_recv(recvdata_event, WRITE_EVENT, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, rt_tick_from_millisecond(1000), &set) == RT_EOK)
        {
            /* 判断写入的数据大小到没到所设置的ringbuffer的阈值 */
            if (rt_ringbuffer_data_len(recvdatabuf) > THRESHOLD)
            {
                /* 到阈值就直接写数据 */
                recvdatafile_p0 = fopen("recvdata_p0.csv", "ab+");
                if (recvdatafile_p0 != RT_NULL)
                {
                    while(rt_ringbuffer_data_len(recvdatabuf))
                    {
                        size = rt_ringbuffer_get(recvdatabuf, (rt_uint8_t *)writebuffer, THRESHOLD);
                        fwrite(writebuffer, 1, size, recvdatafile_p0);
                    }
                    fclose(recvdatafile_p0);
                }
            }
            /* 阈值没到就继续接收感兴趣的事件WRITE_EVENT,以1000ms超时方式接收 */
            continue;
        }
        /* 1000ms到了,还没有收到感兴趣的事件,这时候不管到没到阈值,直接写 */
        recvdatafile_p0 = fopen("recvdata_p0.csv", "ab+");
        if (recvdatafile_p0 != RT_NULL)
        {
            while(rt_ringbuffer_data_len(recvdatabuf))
            {
                size = rt_ringbuffer_get(recvdatabuf, (rt_uint8_t *)writebuffer, THRESHOLD);
                fwrite(writebuffer, 1, size, recvdatafile_p0);
            }
            fclose(recvdatafile_p0);
        }
    } while(0);
}

少的两行,其实是同步线程用的;不然线程隔固定时间运行;

标签:rt,RT,p0,定时器,接收,recvdatafile,ringbuffer,超时,EVENT
From: https://www.cnblogs.com/chino-ll/p/16597719.html

相关文章