【问题标题】:time(NULL) returns 0 on PIC-IoT WG development boardtime(NULL) 在 PIC-IoT WG 开发板上返回 0
【发布时间】:2019-10-02 19:36:41
【问题描述】:

我正在使用 PIC-IoT WG wifi 开发板开展一个项目。我正在尝试修改演示代码以便能够在我自己的服务器上使用它。该演示在几个地方使用了这样的代码:

#include <time.h>
static void connectMQTT()
{
   uint32_t currentTime = time(NULL);

   if (currentTime > 0)
   {
      updateJWT(currentTime + UNIX_OFFSET);   
      MQTT_CLIENT_connect();
   }      
   debug_print("CLOUD: MQTT Connect");

   sendSubscribe = true;
}

每次我打开它时,它都会在 15-20 分钟内尝试连接到服务器,但是这些代码部分没有运行,因为 time(NULL) 似乎返回 0。

据我了解,对于 PIC 微控制器 time(NULL) 应该返回指令周期数。

为什么返回 0?为什么它停止返回 0?

【问题讨论】:

  • in case of PIC microcontrollers time(NULL) should return the number of instruction cycles 你在哪里找到这样的信息?很可能time(NULL) 没有在你的平台上实现,它只是time_t time(..) { return 0; } 用你的调试器检查,看看time() 函数里面有什么东西。
  • @KamilCuk 它来自我正在使用的编译器的文档 (XC16) Returns the calendar time encoded as a value of time_t. If the target environment cannot determine the time, the function returns -1 cast as a time_t. By default, the 16-bit compiler returns the time as instruction cycles. 所以最坏的情况我应该得到-1,但是当它不起作用或实际时间(如果有效)。
  • 这个例子比较奇怪。 time(0); /* start time */。也许尝试运行文档中的示例并查看它是否正确打印?他们也使用time(&amp;var)而不是示例中的返回值,试试看?
  • @KamilCuk 我尝试了示例中的代码,它也返回 0...

标签: c microcontroller pic xc16


【解决方案1】:

一般来说,time.h 中的 time(NULL) 返回一个 time_t,表示自 epoch (1970 Jan 1 00:00:00) 以来的秒数。将 time() 的输出放入 uint32 可能会导致“服务器”出现问题。将其更改为 time_t 并查看是否可以解决问题。

【讨论】:

    猜你喜欢
    • 2019-06-10
    • 2012-01-07
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    • 2016-03-22
    • 1970-01-01
    • 1970-01-01
    • 2015-01-27
    相关资源
    最近更新 更多