[STM32F4] 从UART传感器读取数据

[复制链接]
420|18
封存into 发表于 2025-9-16 09:03 | 显示全部楼层 |阅读模式
Amphenol SM-UART-04L是一款激光粉尘传感器从UART传感器读取数据。我不知道如何从UART传感器读取数据。

我已经试过网上的许多说明,但至今未能成功读取数据。

目前通过CubeIDE进行测试,将STM32F411RE通过USB连接到电脑,能够使用TERA TERM传输和接收数据,但无法从传感器读数。

是否可以在连接到电脑时从传感器读取数据,类似于I2C?

如果不能,应该如何从传感器读取数据?以下是代码。

使用LL库而不是HAL!

uint8_t USART2_GetChar()
{
        uint8_t data;

        data = RxBuffer[RxTailIndex];                                // read the character indexed by RxTailIndex (ie the oldest valid character in the buffer)
                RxTailIndex++;                                                        //increment the tail index (now points to the next oldest char
                if(RxTailIndex>= 1000)                                        // wrap the tail index around to zero if it reaches the max value of the array
                        RxTailIndex = 0;



        return data;
}


// Function to determine if a character is available.
// Returns non zero if there are characters in the buffer
// Returns zero if no characters are in the buffer

uint8_t USART2_CharAvail()
{

return(RxHeadIndex-RxTailIndex);   // compare head and tail index, if they are different it means there are characters present. If difference is 0
                                                                        // there are no characters in the buffer
}

void USART2_IRQHandler()
{
        if(LL_USART_IsActiveFlag_RXNE(USART2) && LL_USART_IsEnabledIT_RXNE(USART2))        // Check to see if it is the character received interrupt that has fired
        {
                Data = LL_USART_ReceiveData8(USART2);  // Reading from the USART Data Rx register clears the RXNE flag

                RxBuffer[RxHeadIndex] = Data;                        // Store the received character in the RxBuffer at the location inexed by RxHeadIndex
                                        RxHeadIndex++;                                                // Increment RxHeadIndex
                                        if(RxHeadIndex >= 1000)                                // If RxHeadIndex is at the max value of the array wrap around to 0. This may overwrite oldest chars
                                                RxHeadIndex = 0;                            // if they have not been cleared. Moral of the story , read from the buffer before it overwrites.

        }
}

  while (1)
  {
          AirQuality();
  }

void AirQuality()
{
  uint8_t data;
  if(USART2_CharAvail())                                                // Test to see if a character has been recieved
  {
          data = USART2_GetChar();                                        // if yes do something interesting with it


  }
}

公羊子丹 发表于 2025-9-17 08:15 | 显示全部楼层
楼主你好,先确认一下 传感器的波特率和数据格式(8N1/校验位),很多人是因为配置不对导致一直收不到数据。
周半梅 发表于 2025-9-17 08:16 | 显示全部楼层
Amphenol SM-UART-04L 默认波特率一般是 9600bps,STM32 的 USART2 初始化参数要和它完全一致,不然 ISR 根本进不来。
帛灿灿 发表于 2025-9-17 08:17 | 显示全部楼层
你现在的环形缓冲区思路没问题,就是要先确认 中断有没有触发,可以在 USART2_IRQHandler() 里打个 GPIO 翻转看。
童雨竹 发表于 2025-9-17 08:20 | 显示全部楼层
另外要注意,不能和 PC 同时连一个 UART,你现在 USB-TTL 和传感器是不是共用 USART2?这样肯定收不到数据。
万图 发表于 2025-9-17 08:20 | 显示全部楼层
这类传感器会周期性发送固定帧,比如头两个字节是 0x42 0x4d,你要在 AirQuality() 里判断帧头,把完整数据包拼出来才有用。
Wordsworth 发表于 2025-9-17 08:21 | 显示全部楼层
建议你用逻辑分析仪抓一下 UART 线,确认传感器是不是有数据发出来,先从物理层排查。
Bblythe 发表于 2025-9-17 08:22 | 显示全部楼层
你用 LL 库收数据没问题,但初始化时要记得 LL_USART_EnableIT_RXNE(USART2);,不然 ISR 不会触发。
Pulitzer 发表于 2025-9-17 08:23 | 显示全部楼层
如果只是想验证数据,可以先不用中断,直接轮询 LL_USART_IsActiveFlag_RXNE(),看看能不能读到字节。
Uriah 发表于 2025-9-17 08:24 | 显示全部楼层
还有一点:STM32F4 的 VDDIO 和传感器电平要对得上,如果传感器是 5V UART,记得加电平转换,不然收不到数据。
Clyde011 发表于 2025-9-17 08:25 | 显示全部楼层
总结:先确认波特率、物理连接和电平 → 再确认中断是否触发 → 最后解析固定帧格式,基本就能读到粉尘浓度数据了。
tpgf 发表于 2025-9-17 11:33 | 显示全部楼层
从UART传感器读取数据的核心在于理解其通信协议并正确配置串口参数
stormwind123 发表于 2025-9-17 16:04 | 显示全部楼层
硬件连接是否正确?
七毛钱 发表于 2025-9-17 17:05 | 显示全部楼层
UART配置是否正确?
内政奇才 发表于 2025-9-17 18:05 | 显示全部楼层
传感器供电是否正常
海滨消消 发表于 2025-9-17 19:06 | 显示全部楼层
通信协议不对?
豌豆爹 发表于 2025-9-17 20:07 | 显示全部楼层
用逻辑分析仪或示波器检查 UART 信号。
海滨消消 发表于 2025-9-17 21:08 | 显示全部楼层
在TERA TERM中手动发送命令试试
麻花油条 发表于 2025-9-17 22:08 | 显示全部楼层
建议查看参考传感器手册。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

66

主题

124

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部