这一贴又是和ZIGBEE技术有关,基于的硬件还是CC2530,最近一直在折腾无线
通信这方面的东西,所以这方面的话题就有些多了,今天的功能为QQ,QQ大家都比较熟悉了,估计初中的时候都用过,各种聊天撩妹,现在QQ用的比较少了。而本帖中的QQ则是加引号的,因为其只能实现短距离的通信,对了,如果有做无线通信或是无线传感网络的高手请多多指教,因为我有许多问题要请教,希望能够更好的学习。
下边是ZigBee模块的实物图
本帖的重点似乎都有些要偏离让我适用的产品,大家习惯就好了。
下边是ZigBee的CC2530的核心代码,是从协议栈代码例程上修改的:
- UINT16 SerialApp_ProcessEvent( uint8 task_id, UINT16 events )
- {
- (void)task_id; // Intentionally unreferenced parameter
-
- if ( events & SYS_EVENT_MSG )
- {
- afIncomingMSGPacket_t *MSGpkt;
- while ( (MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SerialApp_TaskID )) )
- {
- switch ( MSGpkt->hdr.event )
- {
- case AF_INCOMING_MSG_CMD:
- SerialApp_ProcessMSGCmd( MSGpkt ); //接收到信息的消息处理函数
- break;
-
- 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.
- HalLedSet(HAL_LED_1, HAL_LED_MODE_ON);
-
- if(SampleApp_NwkState != DEV_ZB_COORD)
- SerialApp_DeviceConnect();
- }
- else
- {
- // Device is no longer in the network
- }
- break;
- default:
- break;
- }
- osal_msg_deallocate( (uint8 *)MSGpkt );
- }
- return ( events ^ SYS_EVENT_MSG );
- }
- if ( events & SERIALAPP_SEND_EVT )
- {
- SerialApp_Send();
- return ( events ^ SERIALAPP_SEND_EVT );
- }
- if ( events & SERIALAPP_RESP_EVT )
- {
- SerialApp_Resp();
- return ( events ^ SERIALAPP_RESP_EVT );
- }
- return ( 0 ); // Discard unknown events.
- }
下面是协调器接收函数代码:
- void SerialApp_ProcessMSGCmd( afIncomingMSGPacket_t *pkt )
- {
- uint8 stat;
- uint8 seqnb;
- uint8 delay;
- switch ( pkt->clusterId )
- {
- // A message with a serial data block to be transmitted on the serial port.
- case SERIALAPP_CLUSTERID1: //收到发送过来的数据通过串口输出到电脑显示
- // Store the address for sending and retrying.
- osal_memcpy(&SerialApp_RxAddr, &(pkt->srcAddr), sizeof( afAddrType_t ));
- seqnb = pkt->cmd.Data[0];
- // Keep message if not a repeat packet
- if ( (seqnb > SerialApp_RxSeq) || // Normal
- ((seqnb < 0x80 ) && ( SerialApp_RxSeq > 0x80)) ) // Wrap-around
- {
- HalUARTWrite(SERIAL_APP_PORT, "kanBB: ",7);
- // Transmit the data on the serial port. // 通过串口发送数据到PC机
- if ( HalUARTWrite( SERIAL_APP_PORT, pkt->cmd.Data+1, (pkt->cmd.DataLength-1) ) )
- {
- // Save for next incoming message
- SerialApp_RxSeq = seqnb;
- stat = OTA_SUCCESS;
- HalUARTWrite(SERIAL_APP_PORT, "n", sizeof("n")); // 为了提高稳定性而将回车屏蔽掉
- }
- else
- {
- stat = OTA_SER_BUSY;
- }
- }
- else
- {
- stat = OTA_DUP_MSG;
- }
- // Select approproiate OTA flow-control delay.
- delay = (stat == OTA_SER_BUSY) ? SERIALAPP_NAK_DELAY : SERIALAPP_ACK_DELAY;
- // Build & send OTA response message.
- SerialApp_RspBuf[0] = stat;
- SerialApp_RspBuf[1] = seqnb;
- SerialApp_RspBuf[2] = LO_UINT16( delay );
- SerialApp_RspBuf[3] = HI_UINT16( delay );
- osal_set_event( SerialApp_TaskID, SERIALAPP_RESP_EVT ); //收到数据后,发送一个响应事件
- osal_stop_timerEx(SerialApp_TaskID, SERIALAPP_RESP_EVT);
- break;
- // A response to a received serial data block. // 接到响应消息
- case SERIALAPP_CLUSTERID2:
- if ((pkt->cmd.Data[1] == SerialApp_TxSeq) &&
- ((pkt->cmd.Data[0] == OTA_SUCCESS) || (pkt->cmd.Data[0] == OTA_DUP_MSG)))
- {
- SerialApp_TxLen = 0;
- osal_stop_timerEx(SerialApp_TaskID, SERIALAPP_SEND_EVT);
- }
- else
- {
- // Re-start timeout according to delay sent from other device.
- delay = BUILD_UINT16( pkt->cmd.Data[2], pkt->cmd.Data[3] );
- osal_start_timerEx( SerialApp_TaskID, SERIALAPP_SEND_EVT, delay );
- }
- break;
- case SERIALAPP_CONNECTREQ_CLUSTER:
- SerialApp_ConnectReqProcess((uint8*)pkt->cmd.Data);
-
- case SERIALAPP_CONNECTRSP_CLUSTER:
- SerialApp_DeviceConnectRsp((uint8*)pkt->cmd.Data);
-
- default:
- break;
- }
- }
下面是效果图: