采用ActiveMessageC组件来实现发送和采用AMSenderC组件发送是不同的。一般地,实现发送信息有两种思路:
1)仅使用ActiveMessageC组件,利用该组件提供的AMSend接口和SplitControl接口
2)使用AMSenderC组件的AMSend接口,同时使用ActiveMessageC组件的SplitControl接口
SplitControl主要作用是开启无线电。
值得注意的是,两种方式中,AMSend的接口声明是不一样的。前者的AMSend是带有参数,如AMSend[am_id_t id],而后者AMSend是不带参数的。
【测试】
²
上电运行后,在终端输入:
java net.tinyos.tools.Listen –commserial@/dev/ttyUSB0:telos
现象:按照一定先后顺序在终端显示接收到的数据:
00 FF FF 00 01 01 00 06 56
00 FF FF 00 01 01 00 06 12
00 FF FF 00 01 01 00 06 24
【测试程序】
/* by dickychen */
moduleRadioSenderC
{
uses interface Boot;
uses interface SplitControl as RadioControl;
uses interface Packet;
uses interface AMSend as RadioSend[am_id_t id];
uses interface Leds;
uses interface Timer asTimer0;
}
implementation
{
typedef nx_struct RealMsg
{
nx_uint8_t testmsg;
}RealMsg;
message_t MsgOnRadio;
event void Boot.booted()
{
call RadioControl.start( ) ;
}
event void RadioControl.startDone(error_t error )
{
if(error == SUCCESS)
{
call Timer0.startPeriodic(500);
}
}
event void RadioControl.stopDone(error_t error){ }
event void Timer0.fired()
{
RealMsg *btrpkt = (RealMsg *)(call Packet.getPayload(&MsgOnRadio,sizeof(RealMsg)));
btrpkt -> testmsg = 0x56;
callRadioSend.send[6](AM_BROADCAST_ADDR,&MsgOnRadio,sizeof(RealMsg));
}
event void RadioSend.sendDone[am_id_t id](message_t *msg, error_terror)
{
if(error == SUCCESS)
call Leds.led0Toggle();
}
}
configuration RadioSenderAppC
{
}
implementation
{
//components
components RadioSenderC,MainC,LedsC;
components ActiveMessageC as RadioC;
components new TimerMilliC( ) as TimerC0;
//wiring
RadioSenderC .Boot -> MainC;
RadioSenderC.RadioControl -> RadioC;
RadioSenderC.Packet -> RadioC;
RadioSenderC .RadioSend -> RadioC.AMSend;
RadioSenderC.Leds -> LedsC;
RadioSenderC.Timer0 -> TimerC0;
}