TinyOS无线传感器网络串口通信和无线通信是怎样的

发布时间:2021-12-01 17:21:40 作者:柒染
来源:亿速云 阅读:225

今天就跟大家聊聊有关  TinyOS无线传感器网络串口通信和无线通信是怎样的,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

我作为TinyOS入门新手,在学习串口通信和无线通信时遇到不少问题。下面主要是给出一个自己写的示例程序。无线传感器节点收到其他节点的数据通过串口发送给PC,PC通过串口发送指令到节点,节点再通过无线广播PC发送的指令。

项目包含3个文件:Makefile、SerialC.nc、SerialAppC.nc。

Makefile

COMPONENT=SerialAppC
include $(MAKERULES)

SerialAppC.nc

configuration SerialAppC
{
}

implementation
{
 	components MainC, SerialC, LedsC, ActiveMessageC;
	components  PlatformSerialC;
	components SerialC as App;
	components new TimerMilliC() as Timer0;
	components new AMSenderC(6);
  	components new AMReceiverC(6);
	
 	App -> MainC.Boot;
 	App.Leds -> LedsC;
	App.Timer0->Timer0;

	App.StdControl->PlatformSerialC.StdControl;
	App.UartStream->PlatformSerialC.UartStream;
	
	App.RadioSend -> AMSenderC;
	 App.RadioPacket -> AMSenderC;
  	App.RadioAMPacket -> AMSenderC;
  	App.RadioControl -> ActiveMessageC;
  	App.RadioReceive -> AMReceiverC;
}

SerialC.nc

module SerialC 
{
 	uses interface Leds;
 	uses interface Boot;
 	uses interface Timer<TMilli> as  Timer0;
	
	uses interface Packet as RadioPacket;
 	uses interface AMPacket as RadioAMPacket;
 	uses interface AMSend as RadioSend;
  	uses interface Receive as RadioReceive;
  	uses interface SplitControl as RadioControl;
	uses interface StdControl;
	uses interface UartStream;
}
implementation
{
	uint32_t  count=0;
	uint8_t buff[2];
	bool radiobusy=FALSE;
	bool serialbusy=FALSE;
	message_t pkt;
	typedef nx_struct RadioMsg
	{
		nx_uint16_t nodeid;
		nx_uint16_t count;
	}RadioMsg;
	void setLeds(uint16_t val)
	{
    		if (val & 0x01)
      			call Leds.led0On();
    		else 
      			call Leds.led0Off();
    		if (val & 0x02)
     			 call Leds.led1On();
    		else
      			call Leds.led1Off();
    		if (val & 0x04)
      			call Leds.led2On();
    		else
      			call Leds.led2Off();
  	}
  	event void Boot.booted()
  	{
  		call RadioControl.start();
		call StdControl.start();
  	}
    	event void Timer0.fired(){}
    	
	async event void UartStream.sendDone(uint8_t *buf,uint16_t len,error_t error)
	{
		serialbusy=FALSE;
	}
	async event void UartStream.receivedByte(uint8_t byte)
	{
		call Leds.led2Toggle();
		setLeds(byte);
		if(!radiobusy)
		{
			RadioMsg* btrpkt=(RadioMsg*)(call RadioPacket.getPayload(&pkt,sizeof(RadioMsg)));
			btrpkt->nodeid=TOS_NODE_ID;
			btrpkt->count=byte;
			if(call RadioSend.send(AM_BROADCAST_ADDR,&pkt,sizeof(RadioMsg))==SUCCESS)
				radiobusy=TRUE;
		}
	}
	async event void UartStream.receiveDone(uint8_t *buf,uint16_t len,error_t error)
	{
		
	}
	event void RadioControl.startDone(error_t err)
	{

	}
	event void RadioControl.stopDone(error_t err)
	{
	
	}
	event void RadioSend.sendDone(message_t* msg,error_t error)
	{
		if(&pkt==msg)
			radiobusy=FALSE;
	}
	event message_t*  RadioReceive.receive(message_t* msg,void *payload,uint8_t len)
	{
		call Leds.led1Toggle();
		if(len==sizeof(RadioMsg))
		{
			RadioMsg* btrpkt =(RadioMsg*)payload;
			buff[0]=btrpkt->nodeid;
			buff[1]=btrpkt->count;
			
		}
		if(!serialbusy)
		{
			serialbusy=TRUE;
			call UartStream.send(buff, sizeof(buff) );		
		}
		return msg;
	}
}

看完上述内容,你们对  TinyOS无线传感器网络串口通信和无线通信是怎样的有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

推荐阅读:
  1. WLAN 无线网络 11 - 无线网络加密
  2. 无线通信基础资料整理2 之3G 4G

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

上一篇:如何使用KNN近邻算法

下一篇:Storm中如何封装为Fields对象

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》