STM32
直播中

何夏庄

7年用户 922经验值
私信 关注
[问答]

求大佬分享STM32串口接收发送子程序

求大佬分享STM32串口接收发送子程序

回帖(1)

朱艳丽

2021-12-9 10:22:38
/*上传STM32串口接收发送子程序,调试验证OK,供参考*/

#include


static UART_HandleTypeDef* pstm32uart_entry = NULL;


int STM32_SendBuffer(const char* pcmd);
const char* STM32_GetBuffer(void);


#define UART__TIMEOUT 500


void WaitSTM32UartReady()
{
    HAL_UART_StateTypeDef status;
    do
    {
        status = HAL_UART_GetState(pstm32uart_entry);
    }while(status != HAL_UART_STATE_READY);
}


int uart_putc(int c)
{
    while (!__HAL_UART_GET_FLAG(pstm32uart_entry, UART_FLAG_TXE));
   
    pstm32uart_entry->Instance->TDR =  (c & 0xff); // for f030c8
    return 0;
}


int uart_getc(void)
{
    while (!__HAL_UART_GET_FLAG(pstm32uart_entry, UART_FLAG_RXNE));
return  pstm32uart_entry->Instance->RDR & 0xff;
}


int  STM32_SendBuffer(const char* pcmd)
{
    while(*pcmd != 'n')
        uart_putc(*pcmd++);
   
    return SUCCESS;
}


static char STM32_Buf[32];


const char* STM32_GetBuffer()
{
    int i = 0;
    if(pstm32uart_entry == NULL)return NULL;
      
    while(1)
    {
        char szchar = uart_getc();
        if(szchar == 'n')
            break;
        else STM32_Buf[i++] = szchar;
    };
   
   
    if(i == 1)STM32_Buf[0] = 0x00;
    else STM32_Buf[i+1] = 0x00;

    //   DBGSTR("STM32_GetBuffer : %s",&STM32_Buf[0]);
    return &STM32_Buf[0];
}
举报

更多回帖

发帖
×
20
完善资料,
赚取积分