我想写的是ZigBee的广播 组播 点对点播,但是他们都是建立在这个传输的之上,所以先学习这个吧 ,顺便我也在学习一下。
首先打开一个协议栈。ZStack-CC2530-2.3.0-1.4.0 \ Projects \ zstack \ Samples \ SampleApp \ CC2530DB 怕你们弄混了就把目录贴出来。
1 先在SampleApp.c文件中添加串口头文件 #include "MT_UART.h"
2 然后在void SampleApp_Init( uint8 task_id )函数下添加以下内容。
void SampleApp_Init( uint8 task_id ) { SampleApp_TaskID = task_id; SampleApp_NwkState = DEV_INIT; SampleApp_TransID = 0; <span style="color:#cc6600;"> MT_UartInit(); MT_UartRegisterTaskID(task_id);//注册串口 HALUARTWrite(0,"Reset",sizeof("Reset"));//提示复位 协调器复位一下 就打印一次也是证明串口配置好得标志</span> /*
然后go to 进去MT_UartInit();函数
你会看到:
</pre><pre name="code" class="cpp">void MT_UartInit () { halUARTCfg_t uartConfig; /* Initialize APP ID */ App_TaskID = 0; /* UART Configuration */ uartConfig.configured = TRUE; uartConfig.baudRate =<span style="color:#33cc00;"> MT_UART_DEFAULT_BAUDRATE</span>; uartConfig.flowControl = MT_UART_DEFAULT_OVERFLOW; uartConfig.flowControlThreshold = MT_UART_DEFAULT_THRESHOLD; uartConfig.rx.maxBufSize = MT_UART_DEFAULT_MAX_RX_BUFF; uartConfig.tx.maxBufSize = MT_UART_DEFAULT_MAX_TX_BUFF; uartConfig.idleTimeout = MT_UART_DEFAULT_IDLE_TIMEOUT; uartConfig.intEnable = TRUE; #if defined (ZTOOL_P1) || defined (ZTOOL_P2) uartConfig.callBackFunc = MT_UartProcessZToolData; #elif defined (ZAPP_P1) || defined (ZAPP_P2) uartConfig.callBackFunc = MT_UartProcessZAppData; #else uartConfig.callBackFunc = NULL;
</pre><pre name="code" class="cpp">*/
然后 go to 到上边绿色的的值
</pre><pre name="code" class="cpp">会看到
#if !defined( MT_UART_DEFAULT_OVERFLOW ) #define MT_UART_DEFAULT_OVERFLOW TRUE <span style="color:#ff0000;">//如果你用的是 两线的串口DDL 那么就需要修改成 FALSE</span><div class="client_siderbar_list_item"> </div> #endif #if !defined MT_UART_DEFAULT_BAUDRATE #define MT_UART_DEFAULT_BAUDRATE <span style="color:#ff6666;"> HAL_UART_BR_38400 </span><span style="color:#cc0000;"> //这一句就是波特率值 默认 38400 我们改成 115200</span> #endif #define MT_UART_DEFAULT_THRESHOLD MT_UART_THRESHOLD #define MT_UART_DEFAULT_MAX_RX_BUFF MT_UART_RX_BUFF_MAX #if !defined( MT_UART_DEFAULT_MAX_TX_BUFF )
}
到这步我们的串口配置完了。如果烧到板子中 复位 就会打印Reset
结果:
//////////////////////////////////////////////***************************************************************************************///////////////////////////////////////////////
接下来才是正事。
回到SampleApp.c文件
先看看uint16 SampleApp_ProcessEvent( uint8 task_id, uint16 events )函数
uint16 SampleApp_ProcessEvent( uint8 task_id, uint16 events ) { afIncomingMSGPacket_t *MSGpkt; (void)task_id; // Intentionally unreferenced parameter if ( events & SYS_EVENT_MSG ) { MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID ); while ( MSGpkt ) { switch ( MSGpkt->hdr.event ) { // Received when a key is pressed case KEY_CHANGE: SampleApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys ); break; // Received when a messages is received (OTA) for this endpoint case AF_INCOMING_MSG_CMD: SampleApp_MessageMSGCB( MSGpkt );<span style="color:#ff0000;">//这是接收到的数据包处理函数 go to 进去</span>
/* <span style="color:#009900;"> void SampleApp_MessageMSGCB( </span><span style="color:#993399;">afIncomingMSGPacket_t</span><span style="color:#ff6666;"> </span><span style="color:#009900;">*pkt ) </span><span style="color:#993399;">//我们的数据都在这里面 可以 go to</span>
<span style="color:#993399;"> 进去 发现他是一个结构体 然后再go to afMSGCommandFormat_t 就会发现我们的数据都在那里 。 </span><span style="color:#009900;"> { uint16 flashTime; switch ( pkt->clusterId ) { case SAMPLEAPP_PERIODIC_CLUSTERID:</span><span style="color:#ff0000;">//发送定义的数据包ID #define SAMPLEAPP_PERIODIC_CLUSTERID 1</span>
<span style="color: rgb(51, 204, 0); white-space: pre;"> </span><span style="color:#ff0000;">//添加 HalUARTWrite(0,"I get data",sizeof("<span style="font-family: Arial, Helvetica, sans-serif;">I get data</span>"));//提示收到数据</span><span style="color:#33cc00;"> </span><span style="color:#009900;"> break; case SAMPLEAPP_FLASH_CLUSTERID: flashTime = BUILD_UINT16(pkt->cmd.Data[1], pkt->cmd.Data[2] ); HalLedBlink( HAL_LED_4, 4, 50, (flashTime / 4) ); break; } } */</span> break; // Received whenever the device changes state in the network case ZDO_STATE_CHANGE: SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status); if ( (SampleApp_NwkState == DEV_ZB_COORD) || (SampleApp_NwkState == DEV_ROUTER) || (SampleApp_NwkState == DEV_END_DEVICE) ) { // Start sending the periodic message in a regular interval. <span style="color:#ff0000;"> //下面的的三个参数决定发送数据的周期命脉</span> osal_start_timerEx(<span style="color:#ff6666;"> SampleApp_TaskID, </span><span style="color:#ff0000;">//它就是任务ID 函数开头定义了 SampleAPP初始化任务ID</span><span style="color:#ff6666;"> SAMPLEAPP_SEND_PERIODIC_MSG_EVT, </span><span style="color:#ff0000;"> //他在SampleApp.h文件中 #define SAMPLEAPP_SEND_PERIODIC_MSG_EVT 0x0001</span><span style="color:#ff6666;"> SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT </span>); <span style="color:#ff0000;">// #define SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT 5000 <span style="font-family: Arial, Helvetica, sans-serif;">时间 毫秒单位 5s</span></span> } else { // Device is no longer in the network } break; default: break; } // Release the memory osal_msg_deallocate( (uint8 *)MSGpkt ); // Next - if one is available MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID ); } // return unprocessed events return (events ^ SYS_EVENT_MSG); } // Send a message out - This event is generated by a timer // (setup in SampleApp_Init()). if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT )<span style="color:#ff0000;">//事件 ID标志号 #define SAMPLEAPP_SEND_PERIODIC_MSG_EVT 0x0001</span> { // Send the periodic message SampleApp_SendPeriodicMessage();<span style="color:#ff0000;">//这是主要代码 这是周期性发射数据函数 go to 进去</span>
<span style="color:#009900;">/*</span>
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="color:#009900;">void SampleApp_SendPeriodicMessage( void ) {</span></span>
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="color: rgb(0, 153, 0); white-space: pre;"> </span><span style="color:#ff0000;">添加 uint8 data[</span><span style="color:#3333ff;">10</span><span style="color:#ff0000;">] = {'0','1','2','3','4','5','6','7','8','9'};</span><span style="color:#009900;"> if ( AF_DataRequest( &SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_PERIODIC_CLUSTERID, 1, </span><span style="color:#ff0000;">//改成 </span><span style="color:#3333ff;">10</span><span style="color:#009900;"> (uint8*)&SampleAppPeriodicCounter, </span><span style="color:#ff0000;">// 改成data</span><span style="color:#009900;"> &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ) == afStatus_SUCCESS ) { } else { // Error occurred in request to send. } } </span></span>
<span style="color:#009900;"><span style="font-family: Arial, Helvetica, sans-serif;">*</span><span style="font-family: Arial, Helvetica, sans-serif;">/</span></span>
// Setup to send message again in normal period (+ a little jitter) osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT, (SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT + (osal_rand() & 0x00FF)) ); // return unprocessed events return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT); } // Discard unknown events return 0; }
找到下边 这个函数 就是 读出 信息的函数了 我们做一些 修改 。
void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt ) { uint16 flashTime; switch ( pkt->clusterId ) { case SAMPLEAPP_PERIODIC_CLUSTERID:
<span style="white-space:pre"> </span>HalUARTWrite(0,"I get data",sizeof(<span style="font-family: Arial, Helvetica, sans-serif;">"I get data"</span>));<span style="color:#ff0000;">//提示收到数据</span>
<span style="white-space:pre"> </span>HalUARTWrite(0,&pkt->cmd.Data[0],10);<span style="color:#ff0000;">//指针打印数据</span>
<span style="white-space:pre"> </span>HalUARTWrite(0,"\n",1);<span style="color:#ff0000;">//换行</span> break; case SAMPLEAPP_FLASH_CLUSTERID: flashTime = BUILD_UINT16(pkt->cmd.Data[1], pkt->cmd.Data[2] ); HalLedBlink( HAL_LED_4, 4, 50, (flashTime / 4) ); break; } }
然后进行烧写 需要注意是
实验结果
后面写的 广播 组播 点对点 都是依托此实训 写的 。
敬请期待。