1、定义一个串口初始化函数,函数里面对串口进行配置
void initUart(halUARTCBack_t pf)
{
halUARTCfg_t uartConfig;
uartConfig.configured = TRUE;
uartConfig.baudRate = HAL_UART_BR_38400;
uartConfig.flowControl = FALSE;
uartConfig.flowControlThreshold = 48;
uartConfig.rx.maxBufSize = RX_BUF_LEN;
uartConfig.tx.maxBufSize = 128;
uartConfig.idleTimeout = 6;
uartConfig.intEnable = TRUE;
uartConfig.callBackFunc = pf;
HalUARTOpen (HAL_UART_PORT_0, &uartConfig);
}
2、在 ZB_ENTRY_EVENT 这个事件的处理函数中,调用initUart();参数uartRxCB是串口的回调函数。
if( event & ZB_ENTRY_EVENT )
{
……
……
if ( appState == APP_INIT )
{
logicalType = ZG_DEVICETYPE_COORDINATOR;
zb_WriteConfiguration(ZCD_NV_LOGICAL_TYPE, sizeof(uint8), &logicalType);
initUart(uartRxCB);
}
3、定义回调函数,用以接收串口数据
void uartRxCB( uint8 port, uint8 event )
{
uint8 pBuf[RX_BUF_LEN];
uint16 cmd;
uint16 len;
uint16 Endivce_addr;
if ( event != HAL_UART_TX_EMPTY )
{
len = HalUARTRead(HAL_UART_PORT_0, pBuf, RX_BUF_LEN) ;//HalUARTRead把接收到的数据存储到pBuf数组里。
if ( len>0 )
{
cmd = BUILD_UINT16(pBuf[SYS_PING_CMD_OFFSET+ 1], pBuf[SYS_PING_CMD_OFFSET]);
if( (pBuf[FRAME_SOF_OFFSET] == CPT_SOP) && (cmd == SYS_PING_REQUEST) ) //CPT_SOP=0xFE
{
// sysPingReqRcvd();
}
else
{
HalLcdWriteScreen( "Receive Datas","From PC" );
HalLcdWriteValue(pBuf[0],10,3);
HalLcdWriteValue (255, 16, 4);
Endivce_addr = pBuf[1]*256 + pBuf[0];
zb_SendDataRequest( Endivce_addr, LED_PWM_CMD_ID, len, pBuf, 0, 0, 0 );//AF_DEFAULT_RADIUS Endivce_addr
}
}
}
}
4、使用HalUARTWrite()函数进行串口数据的发送;
如HalUARTWrite(HAL_UART_PORT_0, pFrame, FRAME_LENTH);