/**
* @brief 串口初始化
* @param ucPORT 串口号
* ulBaudRate 波特率
* ucDataBits 数据位
* eParity 校验位
* @retval None
*/
BOOL
xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity )
{
static uint32_t ePar_ity;
(void)ucPORT; //不修改串口
(void)ucDataBits; //不修改数据位长度
if (eParity ==MB_PAR_NONE) //校验格式
{
ePar_ity= UART_PARITY_NONE;
}else if (eParity ==MB_PAR_ODD)
{
ePar_ity= UART_PARITY_ODD;
}else if (eParity ==MB_PAR_EVEN)
{
ePar_ity=UART_PARITY_EVEN;
}
MX_USART2_UART_Init((uint32_t) ulBaudRate,ePar_ity);
return TRUE;
}
/**
* @brief 通过串口发送数据
* @param None
* @retval None
*/
BOOL
xMBPortSerialPutByte( CHAR ucByte )
{
/* Put a byte in the UARTs transmit buffer. This function is called在UARTs传输缓冲区中放置一个字节。这个函数被调用
* by the protocol stack if pxMBFrameCBTransmitterEmpty( ) has been按照协议栈,如果pxMBFrameCBTransmitterEmpty( )已经发送数据
* called. */
#if EN_USART1_RX==1
UART1_TX_MODE();
#endif
if(HAL_UART_Transmit(&huart2,(uint8_t*)&ucByte,1,0x05)!=HAL_OK) //发送数据
return FALSE;
else
return TRUE;
}
/**
* @brief 从串口获得数据
* @param None
* @retval None
*/
BOOL
xMBPortSerialGetByte( CHAR * pucByte )
{
/* Return the byte in the UARTs receive buffer. This function is called将字节返回到UARTs receive接收缓冲区
* by the protocol stack after pxMBFrameCBByteReceived( ) has been called.
*/
/**pucByte = USART_ReceiveData(USART1); */
#if EN_USART1_RX==1
UART1_RX_MODE();
#endif
if(HAL_UART_Receive(&huart2,(uint8_t*)pucByte,1,0x05)!=HAL_OK) //接收数据
return FALSE;
else
return TRUE;
}
/* Create an interrupt handler for the transmit buffer empty interrupt
* (or an equivalent) for your target processor. This function should then
* call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that
* a new character can be sent. The protocol stack will then call
* xMBPortSerialPutByte( ) to send the character.
*/
void prvvUARTTxReadyISR( void )
{
//pxMBFrameCBTransmitterEmpty = xMBRTUTransmitFSM //mb.c eMBInit函数中
pxMBFrameCBTransmitterEmpty(); //发送状态机
}
/* Create an interrupt handler for the receive interrupt for your target
* processor. This function should then call pxMBFrameCBByteReceived( ). The
* protocol stack will then call xMBPortSerialGetByte( ) to retrieve the
* character.
*/
void prvvUARTRxISR( void )
{
//pxMBFrameCBByteReceived = xMBRTUReceiveFSM //mb.c eMBInit函数中
pxMBFrameCBByteReceived(); //接收状态机
}