|
一. 网络连接
首先需要在新的工作中填充一个事件处理函数,和一个初始化函数,这两个函数的添加位置在OSAL_SerialApp.c文件中,这一点在我们之前的《ZigBee 2007学习分享之从这里开始入门》中已经详细的说明了,在这里不再多说,这一贴我们只描述串口透传部分的内容。
1. 初始化
在初始化函数中,我们只需要初始化串口相关的参数即可。串口初始化,有点像上位机中的串口初始化一样,通过对一个结构体变量进行赋值,再调用串口打开函数即可。
要使用串口,必须要编译串口相关的代码,在IAR中通过对工程是设置进行,这添加HAL_UART=TRUE即可
- void SerialApp_Init( uint8 task_id )
- {
- halUARTCfg_t uartConfig;
- SerialApp_TaskID = task_id;
- SerialApp_RxSeq = 0xC3;
- SampleApp_NwkState = DEV_INIT;
-
- afRegister( (endPointDesc_t *)&SerialApp_epDesc );
- RegisterForKeys( task_id );
- uartConfig.configured = TRUE; // 2x30 don't care - see uart driver.
- uartConfig.baudRate = SERIAL_APP_BAUD;
- uartConfig.flowControl = FALSE;
- uartConfig.flowControlThreshold = SERIAL_APP_THRESH; // 2x30 don't care - see uart driver.
- uartConfig.rx.maxBufSize = SERIAL_APP_RX_SZ; // 2x30 don't care - see uart driver.
- uartConfig.tx.maxBufSize = SERIAL_APP_TX_SZ; // 2x30 don't care - see uart driver.
- uartConfig.idleTimeout = SERIAL_APP_IDLE; // 2x30 don't care - see uart driver.
- uartConfig.intEnable = TRUE; // 2x30 don't care - see uart driver.
- uartConfig.callBackFunc = SerialApp_CallBack;
- HalUARTOpen (SERIAL_APP_PORT, &uartConfig);
- #if defined ( LCD_SUPPORTED )
- HalLcdWriteString( "SerialApp", HAL_LCD_LINE_2 );
- #endif
-
- ZDO_RegisterForZDOMsg( SerialApp_TaskID, End_Device_Bind_rsp );
- ZDO_RegisterForZDOMsg( SerialApp_TaskID, Match_Desc_rsp );
- }
复制代码
2. 网络连接
网络连接由两个事件组成,一是网络请求,二是网络响应。在网络变化事件中,端点请求网络连接,并发送网络请求事件SERIALAPP_CONNECTREQ_CLUSTER。如果网络连接正常,同时发送一个响应事件SERIALAPP_CONNECTRSP_CLUSTER,从而完成网络连接。
- void SerialApp_DeviceConnect()
- {
- #if ZDO_COORDINATOR
-
- #else
-
- uint16 nwkAddr;
- uint16 parentNwkAddr;
- char buff[30] = {0};
-
- HalLedBlink( HAL_LED_2, 3, 50, (1000 / 4) );
-
- nwkAddr = NLME_GetShortAddr();
- parentNwkAddr = NLME_GetCoordShortAddr();
- sprintf(buff, "parent:%d self:%d\r\n", parentNwkAddr, nwkAddr);
- HalUARTWrite ( 0, (uint8*)buff, strlen(buff));
-
- SerialApp_TxAddr.addrMode = (afAddrMode_t)Addr16Bit;
- SerialApp_TxAddr.endPoint = SERIALAPP_ENDPOINT;
- SerialApp_TxAddr.addr.shortAddr = parentNwkAddr;
-
- buff[0] = HI_UINT16( nwkAddr );
- buff[1] = LO_UINT16( nwkAddr );
-
- if ( AF_DataRequest( &SerialApp_TxAddr, &SerialApp_epDesc,
- SERIALAPP_CONNECTREQ_CLUSTER,
- 2,
- (uint8*)buff,
- &SerialApp_MsgID,
- 0,
- AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
- {
- }
- else
- {
- // Error occurred in request to send.
- }
-
- #endif //ZDO_COORDINATOR
- }
复制代码
3. 网络事件的处理
网络事件保护请求连接、连接响应,数据接收等。这些事件的处理函数为SerialApp_ProcessMSGCmd。
- 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
- {
- // 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;
- }
- 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;
- }
- }
复制代码
|
|