第一步,和配置串口一样。
不再赘述。
第二步,修改接收数据部分
void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt ) { uint16 flashTime; switch ( pkt->clusterId ) { case SAMPLEAPP_PERIODIC_CLUSTERID: //接收数据部分 HalUARTWrite(0,"Rx:",sizeof("Rx:")); HalUARTWrite(0,pkt->cmd.Data,pkt->cmd.DataLength); //刚刚把pkt打成了pkkt,而且还加了双引号,结构收到的数据就是pkt->cmd.Data HalUARTWrite(0,"\n",1); // 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; } }
afIncomingMSGPacket_t是一个结构体,进入可以看到
typedef struct { osal_event_hdr_t hdr; /* OSAL Message header */ 消息头 uint16 groupId; /* Message's group ID - 0 if not set */ 消息组ID uint16 clusterId; /* Message's cluster ID */ 消息族ID afAddrType_t srcAddr; /* Source Address, if endpoint is STUBAPS_INTER_PAN_EP, it's an InterPAN message */ 源地址类型 uint16 macDestAddr; /* MAC header destination short address */MAC物理地址 uint8 endPoint; /* destination endpoint */ MAC目标端点 uint8 wasBroadcast; /* TRUE if network destination was a broadcast address */ 广播地址 uint8 LinkQuality; /* The link quality of the received data frame */ 接收数据帧的链路质量 uint8 correlation; /* The raw correlation value of the received data frame */ 接收数据帧的相关的未加工的值 int8 rssi; /* The received RF power in units dBm */ dbm接受的射频功率 uint8 SecurityUse; /* deprecated */ 弃用 uint32 timestamp; /* receipt timestamp from MAC */ MAC收到时间标记 uint8 nwkSeqNum; /* network header frame sequence number */网络报头帧序列号 afMSGCommandFormat_t cmd; /* Application Data */应用程序数据 } afIncomingMSGPacket_t;
afMSGCommandFormat_t又是一个结构体,进入
typedef struct { uint8 TransSeqNumber; uint16 DataLength; // Number of bytes in TransData uint8 *Data; //这个就是数据啦! } afMSGCommandFormat_t;
第三步,修改发送数据部分
void SampleApp_SendPeriodicMessage( void ) { // uint8 data[10]="fudianheg"; // if ( AF_DataRequest( &SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_PERIODIC_CLUSTERID,//簇id 9,//发送数据长度 data,//数据缓冲区 //1, //(uint8*)&SampleAppPeriodicCounter, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ) == afStatus_SUCCESS ) { } else { // Error occurred in request to send. } }
直接上代码
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 ); 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. osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT, SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT ); //任务id,事件的编号(同一个人任务下可以有多个事件),以毫秒为单位发送的时间 } 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 ) { // Send the periodic message SampleApp_SendPeriodicMessage();//这就是我们发送的数据 // 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; }