点对点通信_radio.c

上传人:第*** 文档编号:32826781 上传时间:2018-02-12 格式:DOCX 页数:6 大小:62.66KB
返回 下载 相关 举报
点对点通信_radio.c_第1页
第1页 / 共6页
点对点通信_radio.c_第2页
第2页 / 共6页
点对点通信_radio.c_第3页
第3页 / 共6页
点对点通信_radio.c_第4页
第4页 / 共6页
点对点通信_radio.c_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《点对点通信_radio.c》由会员分享,可在线阅读,更多相关《点对点通信_radio.c(6页珍藏版)》请在金锄头文库上搜索。

1、/*Filename: radio.cTarget: cc2430Revised: 16/12-2005Revision: 1.0*/#include cul.hextern volatile BYTE sppRxStatus;extern volatile BYTE sppTxStatus;_no_init SPP_RX_STRUCT rxData PM0_XDATA; /rxData_no_init SPP_TX_STRUCT txData PM0_XDATA; /txData/*typedef structBYTE payloadLength;BYTE destAddress;BYTE

2、flags;BYTE *payload;SPP_TX_STRUCT;typedef structBYTE payloadLength;BYTE destAddress;BYTE srcAddress;BYTE flags;BYTE payloadSPP_MAX_PAYLOAD_LENGTH + SPP_FOOTER_LENGTH;SPP_RX_STRUCT;*/-/ See cul.h for a description of this function./*This function initializes the radio by calling sppInit.The frequency

3、 of operation is set andthis nodes address isstored. The radio is set up for handling non-IEEE 802.15.4 packets.这个函数初始化 radio(运行 SPP 模式时):设置频率及节点地址。*/-void radioInit(UINT32 frequency, BYTE localAddress)sppInit(frequency,localAddress);return;/-/ See cul.h for a description of this function./*This fun

4、ction sends data of a given length either to a specified recipient (may be broadcast)using the radio. If the number of bytes to be transferred is larger than the size of the TX FIFO,the data is split over an adequate number of packets. The function uses the SPP library. If theradio is busy, or the p

5、acket is sent but not ACKed, the transmission is retried. If the retiresfails, the function returns FALSE. If the packet is ACKed correctly by the receiver, thefunction returns TRUE.transmitData:指向将被传送的数据。dataLenght:被传送数据的长度。remoteAddress:接收数据包的节点地址。doAck:设置是否需要应答。*/-/*contionuousMode()调用的 radioSend

6、():res = radioSend(sendBuffer, sizeof(sendBuffer), remoteAddr, DO_NOT_ACK );其中 sendBuffer = Hello;即为所要发送的数据, transmitData 指向 sendBuffer首地址,然后通过指针 transmitData 为结构体 txData 进行配置(要注意 txData 为所发送数据的描述性结构体,要把结构体 txData 内各项都配置好);配置好后 radioSend()调用 sppSend(而 sppSend()形式为sppSend(SPP_TX_STRUCT* pPacketPointe

7、r),因此结构体指针 pPacketPointer 指向的是结构体 txData 的首地址,可以这么想象:结构体 pPacketPointer等同于结构体 txData;数据的传递 过程是这样的: sendBuffer -*transmitData-txData-&txData-*pPacketPointer-pPacketPointer*/BOOL radioSend(BYTE* transmitData, WORD dataLength, BYTE remoteAddress, BYTE doAck)WORD sent = 0; /发送字节数BOOL status = TRUE; /状态标

8、志位WORD remaining; /剩余字节数BYTE retries; /重试次数BYTE res; /txData.destAddress = remoteAddress; /接收数据的节点地址置为目的地址txData.flags = doAck; /需要应答while(sent SPP_MAX_PAYLOAD_LENGTH) /如果剩余数据长度 SPP 限定的最大数据长度txData.payloadLength = SPP_MAX_PAYLOAD_LENGTH;/则此次发送中 SPP 数据包里的数据的长度 =SPP 限定的最大数据长度sent += SPP_MAX_PAYLOAD_LE

9、NGTH; /已经发送了的数据长度= 已经发送了的数据长度+此次发送的数据的长度else /如果剩余数据长度dataLength,则必定跳出 while();/也即当剩余数据小于 SPP_MAX_PAYLOAD_LENGTH 时,数据发送完后就跳出 while 了。while(retries) /未到最大重发次数res = sppSend(/*调用 sppSend()发送数据,txData 为所发送数据的结构体,见开头*/ if(res = CHANNEL_BUSY) /如果当前通道状态为忙碌halWait(10); /等待retries-; if(retries = 0)status = F

10、ALSE; /所有重试完后仍忙碌,直接返回 FALSE 状态else /不忙碌 retries = 0; /则停止重试,直接发送while(sppTxStatus = TX_IN_PROGRESS); /正在发送过程中,等待/发送完成,跳出if(sppTxStatus = DEST_UNREACHABLE) /发送完,但返回未发送到目的地status = FALSE; /返回 FALSE 状态return status; /返回状态值/-/ See cul.h for a description of this function./*This function turns on the rad

11、io receiver and waits until either a correctly addressed packetpacket is received or the unit has been waiting for a specified time period. The function employsthe SPP library for radio access.receiveData:双指针指向接收数据length:指向接收数据的长度timeout:接收器接收等待时间;若在等待时间内未接收到数据间,则返回 FALSE;若设其值为 0,则将会一直等到接收到数据。sender

12、:函数将数据包源地址赋给此 sender*/-BOOL radioReceive(BYTE* receiveData, BYTE* length, WORD timeout, BYTE* sender)BOOL status = TRUE;BOOL continueWaiting = TRUE; /是否连续等待状态字BOOL useTimeout = FALSE; /是否使用超时状态字if(timeout) /如果设置了等待时间,则 useTimeout=TRUEuseTimeout = TRUE; sppReceive( /接收数据while(sppRxStatus != RX_COMPLE

13、TE) & (continueWaiting) /接收未完成并且连续等待if(useTimeout) /如果使用超时halWait(0x01);timeout-;if(timeout = 0) /超时结束continueWaiting = FALSE;status = FALSE;STOP_RADIO();if(status = TRUE)*receiveData = rxData.payload; /指向接收的数据*length = rxData.payloadLength; /指向接收数据的长度*sender = rxData.srcAddress; /指向接收数据的源地址return status; /返回状态 说明:本文作者所记录,程序注释基本为个人理解,错误再所难免,还请高手指点,本人随时更新,转载请注明出处。谢谢!

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 建筑/环境 > 工程造价

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号