Tinyos学习笔记(三)
读取Telosb内部传感器数据,并在计算机上显示。
senseC.nc代码如下:
1 #include "Timer.h" 2 #include "sense.h" 3 #include "printf.h" 4 5 module senseC 6 { 7 uses{ 8 interface Boot; 9 interface Timer<TMilli>;//?????? 10 interface Read<uint16_t>; 11 interface SplitControl; 12 interface Packet; 13 interface AMSend; 14 } 15 } 16 implementation 17 { 18 bool busy=FALSE; 19 message_t pkt; 20 21 event void Boot.booted() 22 { 23 call SplitControl.start(); 24 } 25 event void SplitControl.startDone(error_t err) 26 { 27 if(SUCCESS==err) 28 call Timer.startPeriodic(500); 29 else 30 call SplitControl.start(); 31 } 32 event void SplitControl.stopDone(error_t err){} 33 34 event void Timer.fired() 35 { 36 call Read.read(); 37 } 38 39 event void Read.readDone(error_t err,uint16_t data) 40 { 41 if(SUCCESS==err) 42 { 43 if(!busy) 44 { 45 SenseMsg* tspkt=(SenseMsg*)(call Packet.getPayload(&pkt,sizeof(SenseMsg))); 46 if(NULL==tspkt) 47 return; 48 if (call Packet.maxPayloadLength() < sizeof(SenseMsg)) 49 return; 50 tspkt->data=((float)data/4096*1.5-0.986)/0.0035;//(float)data*3/4096 51 if(call AMSend.send(AM_BROADCAST_ADDR,&pkt,sizeof(SenseMsg))==SUCCESS) 52 busy=TRUE; 53 //java net.tinyos.tools.PrintfClient -comm serial@/dev/ttyUSB0:telosb 54 printf("The current temperature is %u.\n",tspkt->data); 55 printfflush(); 56 } 57 } 58 } 59 event void AMSend.sendDone(message_t* msg,error_t err) 60 { 61 if(msg==&pkt) 62 busy=FALSE; 63 } 64 }
注意事项:
- 使用printf()须在Makefile中添加代码:CFLAGS += -I$(TOSDIR)/lib/printf
- 可用的内部传感器组件可在$(TOSDIR)/chips/msp430/sensors/中找到
- 由Read.readDone()得到的数据还需进一步的计算
下面我们由Java tools输出程序的结果:
1、java net.tinyos.tools.Listen -comm serial@/dev/ttyUSB0:telosb
2、java net.tinyos.tools.MsgReader -comm serial@/dev/ttyUSB0:telosb SenseMsg
3、java net.tinyos.tools.PrintfClient -comm serial@/dev/ttyUSB0:telosb
4、java net.tinyos.sf.SerialForwarder -comm serial@/dev/ttyUSB0:telosb
=======================================================================
高洪臣 (Gavin Gao)
cggos@outlook.com
=======================================================================
高洪臣 (Gavin Gao)
cggos@outlook.com
=======================================================================