简单的串口接收超时函数
类似于select 函数。
设置的超时时间,若在规定的时间内接收到数据,则返回0(接收成功);否则返回-1
适用场景:
给一块设备发送信息,能够立马收到响应信息。
/*************************************************************************
* @函数名称 SerialKeyPressed
* @函数说明 测试超级终端是否有按键按下
* @输入参数 key:按键
* @输出参数 无
* @返回参数 1:正确
0:错误
**************************************************************************/
uint32_t SerialKeyPressed(uint8_t *key)
{
if ( USART_GetFlagStatus(USART1, USART_FLAG_RXNE) != RESET)
{
*key = (uint8_t)USART1->DR;
return 1;
}
else
{
return 0;
}
}
/************************************************************************
* @函数名称 Receive_Byte
* @函数说明 从发送端接收一个字节
* @输入参数 c: 接收字符
timeout: 超时时间
* @输出参数 无
* @返回参数 接收的结果
0:成功接收
-1:时间超时
*************************************************************************/
static int32_t Receive_Byte (uint8_t *c, uint32_t timeout)
{
while (timeout-- > 0)
{
if (SerialKeyPressed(c) == 1)
{
return 0;
}
}
return -1;
}