C语言 创建数据包时出现编译错误



我正在为tinyos学习本教程,我想尝试一下。我尝试创建数据包,但它给了我以下错误。我不知道怎么了。这可能是一些简单的事情,但我无法弄清楚它是什么。

#include "TestMsg.h"
    ...
        event void AMControl.startDone(error_t error) {
            if (error == SUCCESS) {
                call Leds.led0On();
                //create packet
                TestMsg_t* msg = call Packet.getPayload(&packet, sizeof(TestMsg_t));
                msg->NodeID = TOS_NODE_ID;
    //          
    //          //TODO in the meantime this can change
    //          button_state_t val = call Get.get();
    //          msg->Data = ( val == BUTTON_PRESSED ? 1 : 0 );
    //          
    //          //send packet
    //          if (call AMSend.send(AM_BROADCAST_ADDR, &packet, sizeof(TestMsg_t)) == SUCCESS) {
    //              radioBusy = TRUE;
    //          }
            } else {
                call AMControl.start();
            }
        }
    ...

这是TestMsg.h

#ifndef TEST_MSG_H
#define TEST_MSG_H
typedef nx_struct _TestMsg {
    nx_uint16_t NodeID;
    nx_uint8_t Data;
} TestMsg_t;
enum {
    AM_RADIO = 6
};
#endif /* TEST_MSG_H */

这是视频中声明的部分

我得到的错误是这样的:

In file included from /home/advanticsys/ws/TestRadio/src/TestRadioAppC.nc:5:
In component `TestRadioC':
/home/advanticsys/ws/TestRadio/src/TestRadioC.nc: In function `AMControl.startDone':
/home/advanticsys/ws/TestRadio/src/TestRadioC.nc:43: syntax error before `*'
/home/advanticsys/ws/TestRadio/src/TestRadioC.nc:44: `msg' undeclared (first use in this function)
/home/advanticsys/ws/TestRadio/src/TestRadioC.nc:44: (Each undeclared identifier is reported only once
/home/advanticsys/ws/TestRadio/src/TestRadioC.nc:44: for each function it appears in.)

更新

结构和标头有问题。

#include "Szar.h"
#include "BarType.h"
module SzarP {
    uses interface Boot;
    uses interface Leds;
}
implementation {
    event void Boot.booted() {
        // TODO Auto-generated method stub
        call Leds.led0On();
        Szar_t foo;
        Szar_t *szar = &foo;
        BarType_t barVar;
        barVar.data = 0;
        BarType_t *pBarVar = &barVar;
        pBarVar->data = 1;
    }
}

以下是 2 个头文件。

#ifndef SZAR_H
#define SZAR_H
typedef nx_struct _Szar {
    nx_uint8_t szar1;
    nx_uint16_t szar2;
} Szar_t;
#endif /* SZAR_H */

#ifndef BAR_TYPE_H
#define BAR_TYPE_H
typedef struct _BarType {
    uint8_t id;
    uint32_t data;
} BarType_t;
#endif /* BAR_TYPE_H */

和错误:

In file included from /home/advanticsys/ws/Szar/src/SzarAppC.nc:6:
In component `SzarP':
/home/advanticsys/ws/Szar/src/SzarP.nc: In function `Boot.booted':
/home/advanticsys/ws/Szar/src/SzarP.nc:15: syntax error before `foo'
/home/advanticsys/ws/Szar/src/SzarP.nc:19: `barVar' undeclared (first use in this function)
/home/advanticsys/ws/Szar/src/SzarP.nc:19: (Each undeclared identifier is reported only once
/home/advanticsys/ws/Szar/src/SzarP.nc:19: for each function it appears in.)
/home/advanticsys/ws/Szar/src/SzarP.nc:20: syntax error before `*'
/home/advanticsys/ws/Szar/src/SzarP.nc:21: `pBarVar' undeclared (first use in this function)

出于某种奇怪的原因,我必须在函数外部声明每个变量,然后它就可以工作了。例:

bool radioBusy = FALSE;
message_t packet;
TestMsg_t *messageToSend;
button_state_t buttonState;
event void AMControl.startDone(error_t error) {
    if (error == SUCCESS) {
        call Leds.led0On();
        messageToSend = call Packet.getPayload(&packet, sizeof(TestMsg_t));
        messageToSend->NodeID = TOS_NODE_ID;
        //TODO in the meantime this can change
        buttonState = call Get.get();
        messageToSend->Data = ( buttonState == BUTTON_PRESSED ? 1 : 0 );
        //send packet
        if (call AMSend.send(AM_BROADCAST_ADDR, &packet, sizeof(TestMsg_t)) == SUCCESS) {
            radioBusy = TRUE;
        }
    } else {
        call AMControl.start();
    }
}

如果我在函数/事件/命令的开头声明我的变量,它们之前没有任何代码,它也有效。

相关内容

  • 没有找到相关文章

最新更新