【发布时间】: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(&var)而不是示例中的返回值,试试看? -
@KamilCuk 我尝试了示例中的代码,它也返回 0...
标签: c microcontroller pic xc16