我使用C函数difftime有时会返回65535 [英] My c-function using difftime returns 65535 sometimes

查看:186
本文介绍了我使用C函数difftime有时会返回65535的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用difftime由于通信心跳停止检测秒的时间的功能。这个功能可以运行为每50毫秒一样快。该功能似乎在除了工作,一旦当它返回65535我可以减少执行一次,因为difftime返回的第二只以秒为单位。但我不知道这是否会解决这个问题。这是不是问题,因为我没有正确地从双铸造difftime返回到uint16_t?

这个程序在Ubuntu 64位机器上运行。

请帮忙。谢谢。

  uint16_t commlost(uint16_t heartbeat_read_cur)
{
    time_t的CURRENT_TIME;
    静态time_t的previous_time;
    静态uint16_t commlost_secs_cur = 0;
    静态uint16_t commlost_secs_ preV = 0;
    静态uint16_t heartbeat_read_ preV = 0;
    静态布尔FIRST_TIME = TRUE;
    如果(FIRST_TIME)
    {
        previous_time =时间(NULL);
        FIRST_TIME = FALSE;
    }    时间(安培; CURRENT_TIME);    commlost_secs_ preV = commlost_secs_cur;    如果(heartbeat_read_ $ P $光伏== heartbeat_read_cur)
    {        commlost_secs_cur + = difftime(CURRENT_TIME,previous_time);
    }
    其他
    {
        heartbeat_read_ preV = heartbeat_read_cur;
        commlost_secs_cur = 0;
    }    previous_time = CURRENT_TIME;    返回(commlost_secs_cur);
}


解决方案

difftime()是系统中使用的,其中 time_t的支持的时间比整数秒更高的分辨率。虽然允许的,我从来没有遇到过这样的系统 - 我无法想象存在一个。在具体而言, time_t的在整体秒为单位POSIX系统,以及 difftime()相当于它的参数算术减法,所以实际上你最好只使用:

  commlost_secs_cur + = CURRENT_TIME  -  previous_time;

也就是说,你的实现是有些过于复杂,而不是累积时间,因为previous呼叫,其可以是比时钟分辨率较小,则可以简单地时间戳的最后心跳观察,此后返回时间

  INT commlost(uint16_t heartbeat_read_cur)
{
    time_t的CURRENT_TIME =时间(0);
    静态time_t的heartbeat_timestamp = 0;
    静态uint16_t heartbeat_read_ preV;    //如果第一次或心跳变...
    如果(heartbeat_timestamp == 0 || heartbeat_read_ preV!= heartbeat_read_cur)
    {
        // ...时间戳心跳
        heartbeat_timestamp =时间(0);        // ...最后保持心跳
        heartbeat_read_ preV = heartbeat_read_cur;
    }    //返回秒自去年心跳时间戳
    返回CURRENT_TIME - heartbeat_timestamp;
}

我不知道你为什么会使用 uint16_t 此;有很少或没有优势,除非它是用于与某些特定的协议或文件格式的兼容性。

I have a function that uses difftime to detect the time in seconds since the communication heartbeat has stopped. This function could run as fast as every 50 milliseconds. The function seems to work except once in a while it returns 65535. I can reduce the execution to once a second since the return of difftime is only in seconds. But I don't know if it will fix the problem. Is it the problem because I am not properly casting the difftime return back to uint16_t from double?

This program runs in an ubuntu 64bit machine.

Please help. Thanks.

uint16_t commlost(uint16_t heartbeat_read_cur)
{  
    time_t current_time;  
    static time_t previous_time;  
    static uint16_t commlost_secs_cur = 0;  
    static uint16_t commlost_secs_prev = 0;  
    static uint16_t heartbeat_read_prev = 0;  
    static bool first_time = TRUE;
    if (first_time)
    {
        previous_time = time(NULL);
        first_time = FALSE;
    }

    time(&current_time);

    commlost_secs_prev = commlost_secs_cur;

    if(heartbeat_read_prev == heartbeat_read_cur)
    {

        commlost_secs_cur += difftime(current_time, previous_time);
    }
    else
    {
        heartbeat_read_prev = heartbeat_read_cur;
        commlost_secs_cur = 0;
    }

    previous_time = current_time;

    return (commlost_secs_cur);
}

解决方案

difftime() is of use on systems where time_t supports time to a higher resolution than integral seconds. Although allowed, I've never encountered such a system - I don't imagine one exists. On POSIX systems specifically, time_t is measured in integral seconds, and difftime() is equivalent to arithmetic subtraction of its arguments, so in fact you are better off simply using:

commlost_secs_cur += current_time - previous_time ;

That said, your implementation is somewhat over-complex, rather than accumulating time since the previous call, which may be smaller than the clock resolution, you could simply time-stamp the last heartbeat observed and return time since then:

int commlost(uint16_t heartbeat_read_cur)
{  
    time_t current_time = time(0) ;  
    static time_t heartbeat_timestamp = 0 ; 
    static uint16_t heartbeat_read_prev  ;

    // If first time, or heartbeat changed...
    if( heartbeat_timestamp == 0 || heartbeat_read_prev != heartbeat_read_cur)
    {
        // ... timestamp heartbeat
        heartbeat_timestamp = time(0) ;

        // ... keep last heartbeat
        heartbeat_read_prev = heartbeat_read_cur ;
    }

    // Return seconds since last heartbeat timestamp
    return current_time - heartbeat_timestamp ;
}

I am not sure why you would use uint16_t for this; there is little or no advantage unless it is for compatibility with some specific protocol or file format.

这篇关于我使用C函数difftime有时会返回65535的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆