基于Contiki-NG与Cooja开发Sky mote程序时遇语法错误求助
etimer_set in Contiki-NG Cooja Sky Mote Let's cut straight to the chase: your compile errors are caused by a simple C syntax mistake—you're calling the etimer_set() function directly in the global scope, which isn't allowed in C. Global scope only supports declarations (like variable definitions, macros, or process declarations), not executable code like function calls.
Looking at your code, this line is the culprit:
etimer_set(&et, PERIOD); // Set the timer
You've placed this right after declaring your et timer variable, outside of any function. The compiler sees the & operator and numeric constant PERIOD here and doesn't know what to do with them, hence the "expected declaration specifiers" errors.
The Fix
All you need to do is remove that global etimer_set() call. Your process's main loop already includes the same etimer_set() line, so you don't lose any functionality by deleting the global one.
Here's the corrected full code:
#include "contiki.h" #include <stdio.h> #define PERIOD CLOCK_SECOND * 2 static struct etimer et; // Define the timer /* Definition of the processes (actually a protothread) */ PROCESS(blink_LED, "blink_LED"); /* Load this process at boot */ AUTOSTART_PROCESSES(&blink_LED); /* The process */ PROCESS_THREAD(blink_LED, ev, data) { /* Application starts */ PROCESS_BEGIN(); /* Main loop of the application */ while(1){ etimer_set(&et, PERIOD); PROCESS_WAIT_EVENT(); printf("Event executed \n"); if(etimer_expired(&et)){ printf("Timer RESET \n"); etimer_reset(&et); } } /* End of the process */ PROCESS_END(); }
Why This Works
In Contiki-NG, all timer operations (setting, resetting, checking expiration) must happen inside a process thread (the PROCESS_THREAD function). Global scope is only for declaring resources that need to be accessible across the process, not for running code. By keeping the timer setup inside your process's loop, you're following C's scope rules and Contiki-NG's process model correctly.
内容的提问来源于stack exchange,提问作者RTS

