代码看的比较费解. 你这个应该是先发送一个指令集,然后传感器会回应一个指令集,对吧 你用中断去接受,返回的数据应该有一定协议的吧? 看你代码貌似长度是自己定义了一个环形缓冲? 你按照协议去接收是最简单的吧 |
#define MAX_SENSOR_BUF_LEN 64
#define SENSOR_RXBUF_LEN 7
#define SENSOR_TXBUF_LEN 8
static uint8_t sensorUartBuffer[MAX_SENSOR_BUF_LEN];
static uint8_t sensorUartBufferIndex = 0;
static uint8_t sensorReportEnabled = 0;
static uint8_t prxbuff = 0;
//static uint8_t ptxbuff = 0;
static uint8_t rxbuff[SENSOR_RXBUF_LEN];
static uint8_t txbuff[SENSOR_TXBUF_LEN] = {0x30,0x03,0x00,0x06,0x00,0x01,0x60,0x2A}; //PM2.5
uint16_t i=0;
uint8_t *pSend;
pSend = txbuff;
HAL_Delay(5000);
while(i++ < SENSOR_TXBUF_LEN)
{
if(HAL_UART_Transmit(&MyDebugUart,(uint8_t *)(pSend++),1,5000) != HAL_OK)
{
debug_print("HAL_UART_Transmit fail!");
}
}
if( HAL_UART_Receive_IT(&MyDebugUart,(uint8_t *)sensorUartBuffer,1) != HAL_OK )
{
debug_print("HAL_UART_Receive fail!");
}
for(size_t i=0;i<=sizeof(rxbuff)-1;i++)
{
debug_print("@%d %x@",rxbuff[i],rxbuff[i]);
}
中断处理函数:
void sensorIRQHandler(void)
{
uint8_t data;
if((MyDebugUart.Instance->SR) & UART_FLAG_RXNE)
{
CLEAR_BIT(MyDebugUart.Instance->SR,UART_FLAG_RXNE|UART_FLAG_ORE);
data = MyDebugUart.Instance->DR;
/*
2关注 丨 2人回答
来自( 测试设备 )
3关注 丨 3人回答
来自( 医疗健康 )
1关注 丨 1人回答
来自( Mouser )
7关注 丨 6人回答
来自( 测试设备 )
5关注 丨 5人回答
来自( ST )