diff --git a/App/Inc/anemometer_dev.h b/App/Inc/anemometer_dev.h index b66fd56..199644e 100644 --- a/App/Inc/anemometer_dev.h +++ b/App/Inc/anemometer_dev.h @@ -129,8 +129,8 @@ typedef struct _mcs_para{ float32_t temperature; /* 环境温度 */ float32_t humidity; /* 环境湿度 */ float32_t pressure; /* 压强 */ - float32_t precipitation; /* 雨量 */ - float32_t precipitation_intensity; /* 光辐射 */ + uint16_t precipitation; /* 雨量 已经是10倍了 */ + uint32_t lightIntensity; /* 光强度 */ }mcs_para; extern mcs_para g_stMcs_Para; diff --git a/App/Src/frt_protocol.c b/App/Src/frt_protocol.c index f406543..32e7a16 100644 --- a/App/Src/frt_protocol.c +++ b/App/Src/frt_protocol.c @@ -196,7 +196,7 @@ FRT_RegProcTable_s g_Write_RegTbl[] = * @param * @retval */ -unsigned short CRC16(unsigned char *arr_buff, unsigned char len) +static unsigned short CRC16(unsigned char *arr_buff, unsigned char len) { unsigned short crc=0xFFFF; unsigned char i, j; diff --git a/Core/Src/freertos.c b/Core/Src/freertos.c index c8f55f6..2d5d034 100644 --- a/Core/Src/freertos.c +++ b/Core/Src/freertos.c @@ -156,6 +156,7 @@ void StartDefaultTask(void const * argument) { read_and_process_uart_data(g_rs485_uart_handle); read_and_process_uart_data(g_term_uart_handle); + readProcessRainData(g_rain_uart_handle); osDelay(200); } /* USER CODE END StartDefaultTask */ @@ -198,8 +199,8 @@ void LEDTask(void const * argument) { osDelay(1000); if(LED_Check_flag) - { - HAL_GPIO_TogglePin(GPIOC,GPIO_LED_CTRL_Pin); + {getLightRainData(); + HAL_GPIO_TogglePin(GPIOC,GPIO_LED_CTRL_Pin); } } /* USER CODE END StartDefaultTask */ diff --git a/Drivers/RainLight/rain.c b/Drivers/RainLight/rain.c index 8515b7d..894600c 100644 --- a/Drivers/RainLight/rain.c +++ b/Drivers/RainLight/rain.c @@ -1,6 +1,8 @@ #include "rain.h" #include "gpio.h" -#include "uart_dev.h" +#include "main.h" +#include "cmsis_os.h" +#include "anemometer_dev.h" // void rainLightPowerOn(void) @@ -35,7 +37,216 @@ void getLightData(void) uart_dev_write(g_rain_uart_handle, getRainDataCmd, sizeof(getRainDataCmd)); } -void readProcessRainData(device_handle device) +//ͬʱȡָһգĸֽڣʮֽ +void getLightRainData(void) +{ + uint8_t getRainDataCmd[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x04, 0x44, 0x09}; + uart_dev_write(g_rain_uart_handle, getRainDataCmd, sizeof(getRainDataCmd)); +} + +/** + * @brief ȡ + * @param + * @retval + */ +static int read_rain_pack(device_handle uart_handle,u_int8_t *buff, u_int32_t buff_size) +{ + u_int32_t offset = 0; + char c = 0; + + unsigned char new_buff[50]; + buff_size--; //Ԥһ'\0'λ + + for (int offset = 0; offset < buff_size;) + { +// ַȡ + c = uart_dev_in_char(uart_handle); + buff[offset++] = c; + +// жַǷǵַǵַٿʼȡһ + if(offset == sizeof(unsigned char)) + { + if(buff[0] != 0x01) + { + memcpy(buff, buff + 1, offset - 1); + offset--; + buff_size--; + } + } +// жϹԲԣһλ + else if(buff[1] != 0x06 && buff[1] != 0x03) + { + memcpy(buff, buff + 1, offset - 1); + offset--; + buff_size--; + } +// + else if (buff[1] == 0x03 && buff[2] == 0x02 && offset == 7) + { + return offset; + } + // + else if (buff[1] == 0x03 && buff[2] == 0x04 && offset == 9) + { + return offset; + } + // + else if (buff[1] == 0x03 && buff[2] == 0x08 && offset == 13) + { + return offset; + } +// + else if (buff[1] == 0x06 & offset == 8) + { + return offset; + } + } + return 0; +} + +/** + * @brief + * @param + * @retval + */ +static void processLightData(u_int8_t *pMsg) +{ + uint32_t lightVal = pMsg[0]<<24|pMsg[1]<<16|pMsg[2]<<8|pMsg[3]; + g_stMcs_Para.lightIntensity = lightVal; +// term_printf("lightVal:%d", lightVal); +} + +/** + * @brief + * @param + * @retval + */ +static void processRainData(u_int8_t *pMsg) +{ + uint32_t rainVal = pMsg[0]<<8|pMsg[1]; + g_stMcs_Para.precipitation = rainVal; +// term_printf("lightVal:%d", rainVal); +} + +/** + * @brief + * @param + * @retval + */ +static void processLightRainData(u_int8_t *lightMsg, u_int8_t *rainMsg) +{ + uint32_t rainVal = rainMsg[0]<<8|rainMsg[1]; + uint32_t lightVal = lightMsg[0]<<24|lightMsg[1]<<16|lightMsg[2]<<8|lightMsg[3]; + g_stMcs_Para.precipitation = rainVal; + g_stMcs_Para.lightIntensity = lightVal; +// term_printf("lightVal:%d", rainVal); +} + +/** + * @brief + * @param + * @retval + */ +static void process0RainData(u_int8_t *pMsg) { +} + +/** + * @brief modbus crc16㷨 + + * @param + * @retval + */ +static unsigned short CRC16(unsigned char *arr_buff, unsigned char len) +{ + unsigned short crc=0xFFFF; + unsigned char i, j; + for ( j=0; j0){ + crc=crc>>1; + crc=crc^ 0xa001; + }else{ + crc=crc>>1; + } + } + } + return crc; +} + +/** + * @brief һϢ + * @param + * @retval + */ +#define RAIN_BUFF_CRC16(x) ((x[MsgLen - 2]) | (x[MsgLen - 1] << 8)) +static void rainMsgHandler(device_handle device, u_int8_t *pMsg, u_int32_t MsgLen) +{ + if(CRC16(pMsg, MsgLen-2) != RAIN_BUFF_CRC16(pMsg)) + { +// term_printf("CRC"); + return; + } + if((pMsg[1] != 0x03) && (pMsg[1] != 0x06)) + { +// term_printf("벻"); + return; + } + uint8_t msgData[4] = {0}; + switch(MsgLen) + { + // + case 7: + msgData[0] = pMsg[3]; + msgData[1] = pMsg[4]; + processRainData(msgData); + break; + // + case 8: + msgData[0] = pMsg[4]; + msgData[1] = pMsg[5]; + process0RainData(msgData); + break; + //ݹ + case 9: + msgData[0] = pMsg[3]; + msgData[1] = pMsg[4]; + msgData[2] = pMsg[5]; + msgData[3] = pMsg[6]; + processLightData(msgData); + break; + //ݹ + case 13: + msgData[0] = pMsg[7];// + msgData[1] = pMsg[8]; + msgData[2] = pMsg[9]; + msgData[3] = pMsg[10]; + uint8_t rainMsg[] = {pMsg[3], pMsg[4]};// + processLightRainData(msgData, rainMsg); + break; + default: + break; + } +} + +/** + * @brief ȡ + * @param + * @retval + */ +static u_int8_t rs485_buff[20]={0x00}; +void readProcessRainData(device_handle device) +{ + if(uart_dev_char_present(device)) + { + osDelay(20); + memset(rs485_buff,0,sizeof(rs485_buff)); + int ret = read_rain_pack(device, rs485_buff, sizeof(rs485_buff)); + if(ret > 0) + { + rainMsgHandler(device, rs485_buff, ret); + } + } } \ No newline at end of file diff --git a/Drivers/RainLight/rain.h b/Drivers/RainLight/rain.h index 2e3566f..d4856b2 100644 --- a/Drivers/RainLight/rain.h +++ b/Drivers/RainLight/rain.h @@ -5,11 +5,15 @@ extern "C" { #endif +#include "uart_dev.h" + void rainLightPowerOn(void); //յԴ void rainLightPowerOff(void); //յԴ void getRainData(void); //ȡָ void clearRainData(void); //Դ void getLightData(void); //ȡ +void getLightRainData(void); //ȡ +void readProcessRainData(device_handle device);//ȡ #ifdef __cplusplus }