micro_climate/App/Src/frt_protocol.c

1306 lines
39 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "frt_protocol.h"
#include "cmsis_os.h"
#include "assertions.h"
#include "inflash.h"
#include "anemometer_dev.h"
#include "sht30.h"
#include "hp203b.h"
extern u_int8_t rs485_out_buff[50];
extern uart_device_info uart_devices[];
/* 静态函数申明 */
static void send_uart_pack(device_handle device,FRT_MsgFunctionCode_e cmd_type, const void *data, u_int16_t len);
static void FRT_MsgProc_ReadRegister(device_handle device, void *pMsg);
static void FRT_MsgProc_WriteRegister(device_handle device, void *pMsg);
static u_int16_t FRT_ReadReg(unsigned char regId);
/* 读 */
static u_int16_t FRT_ReadRegMinWindDiretion(void *pMsg);
static u_int16_t FRT_ReadRegThroughWindDiretion(void *pMsg);
static u_int16_t FRT_ReadRegAverageWindDirection(void *pMsg);
static u_int16_t FRT_ReadRegInstantaneousWindDirection(void *pMsg);
static u_int16_t FRT_ReadRegPeakWindDiretion(void *pMsg);
static u_int16_t FRT_ReadRegMaxWindDirection(void *pMsg);
static u_int16_t FRT_ReadRegMinWindSpeed(void *pMsg);
static u_int16_t FRT_ReadRegThroughWindSpeed(void *pMsg);
static u_int16_t FRT_ReadRegAverageWindSpeed(void *pMsg);
static u_int16_t FRT_ReadRegInstantaneousWindSpeed(void *pMsg);
static u_int16_t FRT_ReadRegPeakWindSpeed(void *pMsg);
static u_int16_t FRT_ReadRegMaxWindSpeed(void *pMsg);
static u_int16_t FRT_ReadRegTemperature(void *pMsg);
static u_int16_t FRT_ReadRegHumidity(void *pMsg);
static u_int16_t FRT_ReadRegPressure(void *pMsg);
static u_int16_t FRT_ReadRegRain(void *pMsg);
/* 读 */
static u_int16_t FRT_ReadRegPrecipitationIntensity(void *pMsg);
static u_int16_t FRT_ReadRegDeviceAddr(void *pMsg);
static u_int16_t FRT_ReadRegCommuBaudRate(void *pMsg);
static u_int16_t FRT_ReadRegSpeedAverageTime(void *pMsg);
static u_int16_t FRT_ReadRegTempHumUpdateTime(void *pMsg);
static u_int16_t FRT_ReadRegRainPowerCtl(void *pMsg);
/* 写 */
static u_int16_t FRT_WriteRegDeviceAddr(void *pMsg);
static u_int16_t FRT_WriteRegCommuBaudRate(void *pMsg);
static u_int16_t FRT_WriteRegSpeedAverageTime(void *pMsg);
static u_int16_t FRT_WriteRegTempHumUpdateTime(void *pMsg);
static u_int16_t FRT_WriteRegSoftReset(void *pMsg);
static u_int16_t FRT_WriteRegResetDefault(void *pMsg);
/* 读配置 */
static u_int16_t FRT_ReadRegTransducerCFG1R5(void *pMsg);
static u_int16_t FRT_ReadRegTransducerCFG4R5(void *pMsg);
static u_int16_t FRT_ReadRegTransducerCFG8R0(void *pMsg);
static u_int16_t FRT_ReadRegRSSIRange(void *pMsg);
static u_int16_t FRT_ReadRegDISTANCE(void *pMsg);
static u_int16_t FRT_ReadRegErrorData(void *pMsg);
static u_int16_t FRT_ReadRegWindC(void *pMsg);
static u_int16_t FRT_ReadRegPoint_1X(void *pMsg);
static u_int16_t FRT_ReadRegPoint_2X(void *pMsg);
static u_int16_t FRT_ReadRegPoint_3X(void *pMsg);
static u_int16_t FRT_ReadRegPoint_4X(void *pMsg);
static u_int16_t FRT_ReadRegPoint_5X(void *pMsg);
static u_int16_t FRT_ReadRegPoint_1Y(void *pMsg);
static u_int16_t FRT_ReadRegPoint_2Y(void *pMsg);
static u_int16_t FRT_ReadRegPoint_3Y(void *pMsg);
static u_int16_t FRT_ReadRegPoint_4Y(void *pMsg);
static u_int16_t FRT_ReadRegPoint_5Y(void *pMsg);
static u_int16_t FRT_ReadRegLinearEnable(void *pMsg);
/* 写配置 */
static u_int16_t FRT_WriteRegTransducerCFG1R5(void *pMsg);
static u_int16_t FRT_WriteRegTransducerCFG4R5(void *pMsg);
static u_int16_t FRT_WriteRegTransducerCFG8R0(void *pMsg);
static u_int16_t FRT_WriteRegRSSIRange(void *pMsg);
static u_int16_t FRT_WriteRegDISTANCE(void *pMsg);
static u_int16_t FRT_WriteRegPoint_1Y(void *pMsg);
static u_int16_t FRT_WriteRegPoint_2Y(void *pMsg);
static u_int16_t FRT_WriteRegPoint_3Y(void *pMsg);
static u_int16_t FRT_WriteRegPoint_4Y(void *pMsg);
static u_int16_t FRT_WriteRegPoint_5Y(void *pMsg);
static u_int16_t FRT_WriteRegLinearEnable(void *pMsg);
static void pdebug_mcs_info();
/* 功能码处理表 */
FRT_FuncionMsgProcTable_s g_MsgTbl[] =
{
{ FRT_FUNCTION_CODE_READ_REGISTER, FRT_MsgProc_ReadRegister },
{ FRT_FUNCTION_CODE_WRITE_REGISTER, FRT_MsgProc_WriteRegister },
};
/* 读寄存器处理表 */
FRT_RegProcTable_s g_RegTbl[] =
{
{ FRT_REGISTER_MIN_WIND_DIRECTION, FRT_ReadRegMinWindDiretion }, /* 最小风向 */
{ FRT_REGISTER_THROUGH_WIND_DIRECTION, FRT_ReadRegThroughWindDiretion }, /* 极小风向 */
{ FRT_REGISTER_AVERAGE_WIND_DIRECTION, FRT_ReadRegAverageWindDirection }, /* 平均风向 */
{ FRT_REGISTER_INSTANTANEOUS_WIND_DIRECTION, FRT_ReadRegInstantaneousWindDirection }, /* 瞬时风向 */
{ FRT_REGISTER_PEAK_WIND_DIRECTION, FRT_ReadRegPeakWindDiretion }, /* 极大风向 */
{ FRT_REGISTER_MAX_WIND_DIRECTION, FRT_ReadRegMaxWindDirection }, /* 最大风向 */
{ FRT_REGISTER_MIN_WIND_SPEED, FRT_ReadRegMinWindSpeed }, /* 最小风速 */
{ FRT_REGISTER_THROUGH_WIND_SPEED, FRT_ReadRegThroughWindSpeed }, /* 极小风速 */
{ FRT_REGISTER_AVERAGE_WIND_SPEED, FRT_ReadRegAverageWindSpeed }, /* 平均风速 */
{ FRT_REGISTER_INSTANTANEOUS_WIND_SPEED, FRT_ReadRegInstantaneousWindSpeed }, /* 瞬时风速 */
{ FRT_REGISTER_PEAK_WIND_SPEED, FRT_ReadRegPeakWindSpeed }, /* 极大风速 */
{ FRT_REGISTER_MAX_WIND_SPEED, FRT_ReadRegMaxWindSpeed }, /* 最大风速 */
{ FRT_REGISTER_TEMPERATURE, FRT_ReadRegTemperature }, /* 大气温度 */
{ FRT_REGISTER_HUMIDITY, FRT_ReadRegHumidity }, /* 大气湿度 */
{ FRT_REGISTER_PRESSURE, FRT_ReadRegPressure }, /* 大气压 */
{ FRT_REGISTER_RAIN, FRT_ReadRegRain }, /* 雨量 */
{ FRT_REGISTER_PRECIPITATION_INTENSITY, FRT_ReadRegPrecipitationIntensity }, /* 总辐射 */
{ FRT_REGISTER_DEVICE_ADDR, FRT_ReadRegDeviceAddr }, /* 设备地址 */
{ FRT_REGISTER_COMMU_BAUDRATE, FRT_ReadRegCommuBaudRate }, /* 波特率 */
{ FRT_REGISTER_SPEED_AVERAGE_TIME, FRT_ReadRegSpeedAverageTime }, /* 风速平均时间 */
{ FRT_REGISTER_TEMPHUM_UPDATE_TIME, FRT_ReadRegTempHumUpdateTime }, /* 温湿度更新时间 */
{ FRT_REGISTER_RAIN_POWER_CONTROL, FRT_ReadRegRainPowerCtl }, /* 雨量电源时间 */
{ FRT_REGISTER_TRANSDUCER_CFG_1R5, FRT_ReadRegTransducerCFG1R5 }, /* 读换能器参数寄存器值1R5 */
{ FRT_REGISTER_TRANSDUCER_CFG_4R5, FRT_ReadRegTransducerCFG4R5 }, /* 读换能器参数寄存器值4R5 */
{ FRT_REGISTER_TRANSDUCER_CFG_8R0, FRT_ReadRegTransducerCFG8R0 }, /* 读换能器参数寄存器值8R0 */
{ FRT_REGISTER_RSSI_RANGE, FRT_ReadRegRSSIRange }, /* 读RSSI有效范围 */
{ FRT_REGISTER_DISTANCE, FRT_ReadRegDISTANCE }, /* 读探头表面有效距离 */
{ FRT_REGISTER_ERROR_LOG, FRT_ReadRegErrorData }, /* 读错误日志 */
{ FRT_REGISTER_WIND_C, FRT_ReadRegWindC }, /* 读计算得到的风速 */
{ FRT_REGISTER_LINEAR_POINT_X_1, FRT_ReadRegPoint_1X }, /* 线性插值原始点1 */
{ FRT_REGISTER_LINEAR_POINT_X_2, FRT_ReadRegPoint_2X }, /* 线性插值原始点2 */
{ FRT_REGISTER_LINEAR_POINT_X_3, FRT_ReadRegPoint_3X }, /* 线性插值原始点3 */
{ FRT_REGISTER_LINEAR_POINT_X_4, FRT_ReadRegPoint_4X }, /* 线性插值原始点4 */
{ FRT_REGISTER_LINEAR_POINT_X_5, FRT_ReadRegPoint_5X }, /* 线性插值原始点5 */
{ FRT_REGISTER_LINEAR_POINT_Y_1, FRT_ReadRegPoint_1Y }, /* 线性插值校准点1 */
{ FRT_REGISTER_LINEAR_POINT_Y_2, FRT_ReadRegPoint_2Y }, /* 线性插值校准点2 */
{ FRT_REGISTER_LINEAR_POINT_Y_3, FRT_ReadRegPoint_3Y }, /* 线性插值校准点3 */
{ FRT_REGISTER_LINEAR_POINT_Y_4, FRT_ReadRegPoint_4Y }, /* 线性插值校准点4 */
{ FRT_REGISTER_LINEAR_POINT_Y_5, FRT_ReadRegPoint_5Y }, /* 线性插值校准点5 */
{ FRT_REGISTER_LINEAR_ENABLE, FRT_ReadRegLinearEnable }, /* 线性插值使能 */
};
/* 写寄存器处理表 */
FRT_RegProcTable_s g_Write_RegTbl[] =
{
{ FRT_REGISTER_DEVICE_ADDR, FRT_WriteRegDeviceAddr }, /* 写设备地址 */
{ FRT_REGISTER_COMMU_BAUDRATE, FRT_WriteRegCommuBaudRate }, /* 写波特率 */
{ FRT_REGISTER_SPEED_AVERAGE_TIME, FRT_WriteRegSpeedAverageTime }, /* 写风速平均时间 */
{ FRT_REGISTER_TEMPHUM_UPDATE_TIME, FRT_WriteRegTempHumUpdateTime }, /* 写温湿度更新时间 */
{ FRT_REGISTER_SOFT_REST, FRT_WriteRegSoftReset }, /* 软件复位 */
{ FRT_REGISTER_REST_DEFAULT_SETTING, FRT_WriteRegResetDefault }, /* 恢复出厂设置 */
{ FRT_REGISTER_TRANSDUCER_CFG_1R5, FRT_WriteRegTransducerCFG1R5 }, /* 写换能器参数寄存器值1R5 */
{ FRT_REGISTER_TRANSDUCER_CFG_4R5, FRT_WriteRegTransducerCFG4R5 }, /* 写换能器参数寄存器值4R5 */
{ FRT_REGISTER_TRANSDUCER_CFG_8R0, FRT_WriteRegTransducerCFG8R0 }, /* 写换能器参数寄存器值8R0 */
{ FRT_REGISTER_RSSI_RANGE, FRT_WriteRegRSSIRange }, /* 写RSSI有效范围 */
{ FRT_REGISTER_DISTANCE, FRT_WriteRegDISTANCE }, /* 写探头表面有效距离 */
{ FRT_REGISTER_LINEAR_POINT_Y_1, FRT_WriteRegPoint_1Y }, /* 线性插值校准点1 */
{ FRT_REGISTER_LINEAR_POINT_Y_2, FRT_WriteRegPoint_2Y }, /* 线性插值校准点2 */
{ FRT_REGISTER_LINEAR_POINT_Y_3, FRT_WriteRegPoint_3Y }, /* 线性插值校准点3 */
{ FRT_REGISTER_LINEAR_POINT_Y_4, FRT_WriteRegPoint_4Y }, /* 线性插值校准点4 */
{ FRT_REGISTER_LINEAR_POINT_Y_5, FRT_WriteRegPoint_5Y }, /* 线性插值校准点5 */
{ FRT_REGISTER_LINEAR_ENABLE, FRT_WriteRegLinearEnable }, /* 线性插值使能 */
};
/**
* @brief modbus crc16算法
* @param
* @retval
*/
unsigned short CRC16(unsigned char *arr_buff, unsigned char len)
{
unsigned short crc=0xFFFF;
unsigned char i, j;
for ( j=0; j<len; j++){
crc=crc ^*arr_buff++;
for ( i=0; i<8; i++){
if( ( crc&0x0001) >0){
crc=crc>>1;
crc=crc^ 0xa001;
}else{
crc=crc>>1;
}
}
}
return crc;
}
/**
* @brief 交换高低字节
* @param
* @retval
*/
static u_int16_t FRT_swap_endian_16(u_int16_t value)
{
return ((value << 8) | (value >> 8));
}
/**
* @brief 读最小风向寄存器值
* @param
* @retval
*/
static u_int16_t FRT_ReadRegMinWindDiretion(void *pMsg)
{
u_int16_t value = (u_int16_t)(g_stMcs_Para.min_wind_direction *10);
return FRT_swap_endian_16(value);
}
/**
* @brief 读极小风向寄存器值
* @param
* @retval
*/
static u_int16_t FRT_ReadRegThroughWindDiretion(void *pMsg)
{
u_int16_t value = (u_int16_t)(g_stMcs_Para.trough_wind_direction *10);
return FRT_swap_endian_16(value);
}
/**
* @brief 读平均风向寄存器值
* @param
* @retval
*/
static u_int16_t FRT_ReadRegAverageWindDirection(void *pMsg)
{
u_int16_t value = (u_int16_t)(g_stMcs_Para.average_wind_direction *10);
return FRT_swap_endian_16(value);
}
/**
* @brief 读瞬时风向寄存器值
* @param
* @retval
*/
static u_int16_t FRT_ReadRegInstantaneousWindDirection(void *pMsg)
{
u_int16_t value = (u_int16_t)(g_stMcs_Para.instantaneous_wind_direction *10);
return FRT_swap_endian_16(value);
}
/**
* @brief 读极大风向寄存器值
* @param
* @retval
*/
static u_int16_t FRT_ReadRegPeakWindDiretion(void *pMsg)
{
u_int16_t value = (u_int16_t)(g_stMcs_Para.peak_wind_direction *10);
return FRT_swap_endian_16(value);
}
/**
* @brief 读最大风向寄存器值
* @param
* @retval
*/
static u_int16_t FRT_ReadRegMaxWindDirection(void *pMsg)
{
u_int16_t value=(u_int16_t)(g_stMcs_Para.max_wind_direction *10);
return FRT_swap_endian_16(value);
}
/**
* @brief 读最小风速寄存器值
* @param
* @retval
*/
static u_int16_t FRT_ReadRegMinWindSpeed(void *pMsg)
{
u_int16_t value=(u_int16_t)(g_stMcs_Para.min_wind_speed *10);
return FRT_swap_endian_16(value);
}
/**
* @brief 读最小风速寄存器值
* @param
* @retval
*/
static u_int16_t FRT_ReadRegThroughWindSpeed(void *pMsg)
{
u_int16_t value=(u_int16_t)(g_stMcs_Para.trough_wind_speed *10);
return FRT_swap_endian_16(value);
}
/**
* @brief 读平均风速寄存器值
* @param
* @retval
*/
static u_int16_t FRT_ReadRegAverageWindSpeed(void *pMsg)
{
u_int16_t value=(u_int16_t)(g_stMcs_Para.average_wind_speed *10);
return FRT_swap_endian_16(value);
}
/**
* @brief 读瞬时风速寄存器值
* @param
* @retval
*/
static u_int16_t FRT_ReadRegInstantaneousWindSpeed(void *pMsg)
{
u_int16_t value=(u_int16_t)(g_stMcs_Para.instantaneous_wind_speed *10);
return FRT_swap_endian_16(value);
}
/**
* @brief 读极大风速寄存器值
* @param
* @retval
*/
static u_int16_t FRT_ReadRegPeakWindSpeed(void *pMsg)
{
u_int16_t value=(u_int16_t)(g_stMcs_Para.peak_wind_speed *10);
return FRT_swap_endian_16(value);
}
/**
* @brief 读最大风速寄存器值
* @param
* @retval
*/
static u_int16_t FRT_ReadRegMaxWindSpeed(void *pMsg)
{
u_int16_t value=(u_int16_t)(g_stMcs_Para.max_wind_speed *10);
return FRT_swap_endian_16(value);
}
/**
* @brief 读温度寄存器值
* @param
* @retval
*/
static u_int16_t FRT_ReadRegTemperature(void *pMsg)
{
u_int16_t value= (u_int16_t)(g_stMcs_Para.temperature*10);
return FRT_swap_endian_16(value);
}
/**
* @brief 读湿度寄存器值
* @param
* @retval
*/
static u_int16_t FRT_ReadRegHumidity(void *pMsg)
{
u_int16_t value=(u_int16_t)(g_stMcs_Para.humidity*10);
return FRT_swap_endian_16(value);
}
/**
* @brief 读气压寄存器值
* @param
* @retval
*/
static u_int16_t FRT_ReadRegPressure(void *pMsg)
{
u_int16_t value=(u_int16_t)(g_stMcs_Para.pressure*10);
return FRT_swap_endian_16(value);
}
/**
* @brief 读雨量寄存器值
* @param
* @retval
*/
static u_int16_t FRT_ReadRegRain(void *pMsg)
{
u_int16_t value=9;
return FRT_swap_endian_16(value);
}
/**
* @brief 读光辐射强度寄存器值
* @param
* @retval
*/
static u_int16_t FRT_ReadRegPrecipitationIntensity(void *pMsg)
{
u_int16_t value=10;
return FRT_swap_endian_16(value);
}
/**
* @brief 读设备地址寄存器值
* @param
* @retval
*/
static u_int16_t FRT_ReadRegDeviceAddr(void *pMsg)
{
read_config_info();
u_int16_t value=g_usrConfigInfo.addr;
return FRT_swap_endian_16(value);
}
/**
* @brief 读波特率寄存器值
* @param
* @retval
*/
static u_int16_t FRT_ReadRegCommuBaudRate(void *pMsg)
{
read_config_info();
u_int16_t value=g_usrConfigInfo.uart_baud;
// term_printf("%x", value);
return FRT_swap_endian_16(value);
}
/**
* @brief 读风速平均时间值
* @param
* @retval
*/
static u_int16_t FRT_ReadRegSpeedAverageTime(void *pMsg)
{
read_config_info();
u_int16_t value=g_usrConfigInfo.speed_average_time;
return FRT_swap_endian_16(value);
}
/**
* @brief 读温湿度更新时间值
* @param
* @retval
*/
static u_int16_t FRT_ReadRegTempHumUpdateTime(void *pMsg)
{
read_config_info();
u_int16_t value=g_usrConfigInfo.temp_hum_update_time;
return FRT_swap_endian_16(value);
}
/**
* @brief 读雨量电源控制值
* @param
* @retval
*/
static u_int16_t FRT_ReadRegRainPowerCtl(void *pMsg)
{
return 0;
}
/**
* @brief 写设备地址
* @param
* @retval
*/
static u_int16_t FRT_WriteRegDeviceAddr(void *pMsg)
{
uint16_t *pMsgAddr = (uint16_t *)pMsg;
uint16_t data = *pMsgAddr;
g_usrConfigInfo.addr = data;
save_usr_config_info(g_usrConfigInfo);
return 0;
}
/**
* @brief 写波特率
* @param
* @retval
*/
static u_int16_t FRT_WriteRegCommuBaudRate(void *pMsg)
{
uint16_t *pMsgAddr = (uint16_t *)pMsg;
uint16_t data = *pMsgAddr;
g_usrConfigInfo.uart_baud = data;
save_usr_config_info(g_usrConfigInfo);
MX_USART3_UART_Init(g_usrConfigInfo.uart_baud);
MX_USART1_UART_Init(g_usrConfigInfo.uart_baud);
return 0;
}
/**
* @brief 写风速平均时间
* @param
* @retval
*/
static u_int16_t FRT_WriteRegSpeedAverageTime(void *pMsg)
{
uint16_t *pMsgAddr = (uint16_t *)pMsg;
uint16_t data = *pMsgAddr;
g_usrConfigInfo.speed_average_time = data;
save_usr_config_info(g_usrConfigInfo);
return 0;
}
/**
* @brief 写温湿度更新时间
* @param
* @retval
*/
static u_int16_t FRT_WriteRegTempHumUpdateTime(void *pMsg)
{
uint16_t *pMsgAddr = (uint16_t *)pMsg;
uint16_t data = *pMsgAddr;
g_usrConfigInfo.temp_hum_update_time = data;
save_usr_config_info(g_usrConfigInfo);
return 0;
}
/**
* @brief 软件复位
* @param
* @retval
*/
static u_int16_t FRT_WriteRegSoftReset(void *pMsg)
{
uint16_t *pMsgAddr = (uint16_t *)pMsg;
uint16_t data = *pMsgAddr;
if(data == 0x0001)
{
__iar_builtin_set_FAULTMASK(1);
NVIC_SystemReset();
}
return 0;
}
/**
* @brief 恢复出厂设置
* @param
* @retval
*/
static u_int16_t FRT_WriteRegResetDefault(void *pMsg)
{
uint16_t *pMsgAddr = (uint16_t *)pMsg;
uint16_t data = *pMsgAddr;
if(data == 0x0001)
{
delete_config_info();
__iar_builtin_set_FAULTMASK(1);
NVIC_SystemReset();
}
return 0;
}
/**
* @brief 读换能器参数寄存器值1R5
* @param
* @retval
*/
static u_int16_t FRT_ReadRegTransducerCFG1R5(void *pMsg)
{
read_config_info();
u_int16_t value=g_stConfigInfo.transducer_cfg_1R5;
return FRT_swap_endian_16(value);
}
/**
* @brief 读换能器参数寄存器值4R5
* @param
* @retval
*/
static u_int16_t FRT_ReadRegTransducerCFG4R5(void *pMsg)
{
read_config_info();
u_int16_t value=g_stConfigInfo.transducer_cfg_4R5;
return FRT_swap_endian_16(value);
}
/**
* @brief 读换能器参数寄存器值8R0
* @param
* @retval
*/
static u_int16_t FRT_ReadRegTransducerCFG8R0(void *pMsg)
{
read_config_info();
u_int16_t value=g_stConfigInfo.transducer_cfg_8R0;
return FRT_swap_endian_16(value);
}
/**
* @brief 读RSSI有效范围
* @param
* @retval
*/
static u_int16_t FRT_ReadRegRSSIRange(void *pMsg)
{
read_config_info();
u_int16_t value=g_stConfigInfo.RSSI_range;
return FRT_swap_endian_16(value);
}
/**
* @brief 读换能器探头距离
* @param
* @retval
*/
static u_int16_t FRT_ReadRegDISTANCE(void *pMsg)
{
read_config_info();
u_int16_t value=g_stConfigInfo.transducer_distace;
return FRT_swap_endian_16(value);
}
/**
* @brief 读错误日志
* @param
* @retval
*/
static u_int16_t FRT_ReadRegErrorData(void *pMsg)
{
u_int16_t value=*((u_int16_t*)&g_error_log);
return FRT_swap_endian_16(value);
}
/**
* @brief 读当前声速
* @param
* @retval
*/
static u_int16_t FRT_ReadRegWindC(void *pMsg)
{
u_int16_t value=(u_int16_t)(weather_info.wind_c *10);
return FRT_swap_endian_16(value);
}
/**
* @brief 读线性插值原始点1
* @param
* @retval
*/
static u_int16_t FRT_ReadRegPoint_1X(void *pMsg)
{
u_int16_t value=g_stConfigInfo.linear_point_1_x;
return FRT_swap_endian_16(value);
}
/**
* @brief 读线性插值原始点2
* @param
* @retval
*/
static u_int16_t FRT_ReadRegPoint_2X(void *pMsg)
{
u_int16_t value=g_stConfigInfo.linear_point_2_x;
return FRT_swap_endian_16(value);
}
/**
* @brief 读线性插值原始点3
* @param
* @retval
*/
static u_int16_t FRT_ReadRegPoint_3X(void *pMsg)
{
u_int16_t value=g_stConfigInfo.linear_point_3_x;
return FRT_swap_endian_16(value);
}
/**
* @brief 读线性插值原始点4
* @param
* @retval
*/
static u_int16_t FRT_ReadRegPoint_4X(void *pMsg)
{
u_int16_t value=g_stConfigInfo.linear_point_4_x;
return FRT_swap_endian_16(value);
}
/**
* @brief 读线性插值原始点5
* @param
* @retval
*/
static u_int16_t FRT_ReadRegPoint_5X(void *pMsg)
{
u_int16_t value=g_stConfigInfo.linear_point_5_x;
return FRT_swap_endian_16(value);
}
/**
* @brief 读线性插值矫正点1
* @param
* @retval
*/
static u_int16_t FRT_ReadRegPoint_1Y(void *pMsg)
{
u_int16_t value=g_stConfigInfo.linear_point_1_y;
return FRT_swap_endian_16(value);
}
/**
* @brief 读线性插值矫正点2
* @param
* @retval
*/
static u_int16_t FRT_ReadRegPoint_2Y(void *pMsg)
{
u_int16_t value=g_stConfigInfo.linear_point_2_y;
return FRT_swap_endian_16(value);
}
/**
* @brief 读线性插值矫正点3
* @param
* @retval
*/
static u_int16_t FRT_ReadRegPoint_3Y(void *pMsg)
{
u_int16_t value=g_stConfigInfo.linear_point_3_y;
return FRT_swap_endian_16(value);
}
/**
* @brief 读线性插值矫正点4
* @param
* @retval
*/
static u_int16_t FRT_ReadRegPoint_4Y(void *pMsg)
{
u_int16_t value=g_stConfigInfo.linear_point_4_y;
return FRT_swap_endian_16(value);
}
/**
* @brief 读线性插值矫正点5
* @param
* @retval
*/
static u_int16_t FRT_ReadRegPoint_5Y(void *pMsg)
{
u_int16_t value=g_stConfigInfo.linear_point_5_y;
return FRT_swap_endian_16(value);
}
/**
* @brief 读线性插值使能状态
* @param
* @retval
*/
static u_int16_t FRT_ReadRegLinearEnable(void *pMsg)
{
u_int16_t value=g_stConfigInfo.linear_enable;
return FRT_swap_endian_16(value);
}
/**
* @brief 写换能器参数寄存器值1R5
* @param
* @retval
*/
static u_int16_t FRT_WriteRegTransducerCFG1R5(void *pMsg)
{
uint16_t *pMsgAddr = (uint16_t *)pMsg;
uint16_t data = *pMsgAddr;
g_stConfigInfo.transducer_cfg_1R5 = data;
save_factory_config_info(g_stConfigInfo);
return 0;
}
/**
* @brief 写换能器参数寄存器值4R5
* @param
* @retval
*/
static u_int16_t FRT_WriteRegTransducerCFG4R5(void *pMsg)
{
uint16_t *pMsgAddr = (uint16_t *)pMsg;
uint16_t data = *pMsgAddr;
g_stConfigInfo.transducer_cfg_4R5 = data;
save_factory_config_info(g_stConfigInfo);
return 0;
}
/**
* @brief 写换能器参数寄存器值8R0
* @param
* @retval
*/
static u_int16_t FRT_WriteRegTransducerCFG8R0(void *pMsg)
{
uint16_t *pMsgAddr = (uint16_t *)pMsg;
uint16_t data = *pMsgAddr;
g_stConfigInfo.transducer_cfg_8R0 = data;
save_factory_config_info(g_stConfigInfo);
return 0;
}
/**
* @brief 写RSSI有效范围
* @param
* @retval
*/
static u_int16_t FRT_WriteRegRSSIRange(void *pMsg)
{
uint16_t *pMsgAddr = (uint16_t *)pMsg;
uint16_t data = *pMsgAddr;
g_stConfigInfo.RSSI_range = data;
save_factory_config_info(g_stConfigInfo);
return 0;
}
/**
* @brief 写换能器探头距离
* @param
* @retval
*/
static u_int16_t FRT_WriteRegDISTANCE(void *pMsg)
{
uint16_t *pMsgAddr = (uint16_t *)pMsg;
uint16_t data = *pMsgAddr;
g_stConfigInfo.transducer_distace = data;
save_factory_config_info(g_stConfigInfo);
return 0;
}
/**
* @brief 写线性插值矫正点1
* @param
* @retval
*/
static u_int16_t FRT_WriteRegPoint_1Y(void *pMsg)
{
// 获取此时XY数据
uint16_t *pMsgPointY = (uint16_t *)pMsg;
uint16_t PointY = *pMsgPointY;
uint16_t PointX = g_stMcs_Para.average_wind_speed * 10;
// 写入Flash
g_stConfigInfo.linear_point_1_x = PointX;
g_stConfigInfo.linear_point_1_y = PointY;
save_factory_config_info(g_stConfigInfo);
return 0;
}
/**
* @brief 写线性插值矫正点2
* @param
* @retval
*/
static u_int16_t FRT_WriteRegPoint_2Y(void *pMsg)
{
// 获取此时XY数据
uint16_t *pMsgPointY = (uint16_t *)pMsg;
uint16_t PointY = *pMsgPointY;
uint16_t PointX = g_stMcs_Para.average_wind_speed * 10;
// 写入Flash
g_stConfigInfo.linear_point_2_x = PointX;
g_stConfigInfo.linear_point_2_y = PointY;
save_factory_config_info(g_stConfigInfo);
return 0;
}
/**
* @brief 写线性插值矫正点3
* @param
* @retval
*/
static u_int16_t FRT_WriteRegPoint_3Y(void *pMsg)
{
// 获取此时XY数据
uint16_t *pMsgPointY = (uint16_t *)pMsg;
uint16_t PointY = *pMsgPointY;
uint16_t PointX = g_stMcs_Para.average_wind_speed * 10;
// 写入Flash
g_stConfigInfo.linear_point_3_x = PointX;
g_stConfigInfo.linear_point_3_y = PointY;
save_factory_config_info(g_stConfigInfo);
return 0;
}
/**
* @brief 写线性插值矫正点4
* @param
* @retval
*/
static u_int16_t FRT_WriteRegPoint_4Y(void *pMsg)
{
// 获取此时XY数据
uint16_t *pMsgPointY = (uint16_t *)pMsg;
uint16_t PointY = *pMsgPointY;
uint16_t PointX = g_stMcs_Para.average_wind_speed * 10;
// 写入Flash
g_stConfigInfo.linear_point_4_x = PointX;
g_stConfigInfo.linear_point_4_y = PointY;
save_factory_config_info(g_stConfigInfo);
return 0;
}
/**
* @brief 写线性插值矫正点5
* @param
* @retval
*/
static u_int16_t FRT_WriteRegPoint_5Y(void *pMsg)
{
// 获取此时XY数据
uint16_t *pMsgPointY = (uint16_t *)pMsg;
uint16_t PointY = *pMsgPointY;
uint16_t PointX = g_stMcs_Para.average_wind_speed * 10;
// 写入Flash
g_stConfigInfo.linear_point_5_x = PointX;
g_stConfigInfo.linear_point_5_y = PointY;
save_factory_config_info(g_stConfigInfo);
return 0;
}
/**
* @brief 写线性插值使能
* @param
* @retval
*/
static u_int16_t FRT_WriteRegLinearEnable(void *pMsg)
{
uint16_t *pMsgData = (uint16_t *)pMsg;
uint16_t data = *pMsgData;
g_stConfigInfo.linear_enable = data;
save_factory_config_info(g_stConfigInfo);
return 0;
}
/**
* @brief 封装协议发送
* @param
* @retval 数据正确返回TRUE数据异常返回FALSE
*/
static void send_uart_pack(device_handle device,FRT_MsgFunctionCode_e cmd_type, const void *data, u_int16_t len)
{
memset(rs485_out_buff,0,sizeof(rs485_out_buff));
frt_climate_pack_resp *pack = (frt_climate_pack_resp*)rs485_out_buff;
pack->addr = g_usrConfigInfo.addr;
pack->func = FRT_FUNCTION_CODE_READ_REGISTER;
pack->data_len = len;
memcpy(pack->data, data, len);
*(u_int16_t*)&pack->data[len] = CRC16((u_int8_t *)&pack->addr,pack->data_len+3);
uart_dev_write(device,(u_int8_t*)pack,pack->data_len+5);
}
/**
* @brief 遍历读取寄存器
* @param
* @retval
*/
u_int16_t FRT_ReadReg(unsigned char regId)
{
for(u_int16_t i = 0; i < sizeof(g_RegTbl) / sizeof(FRT_RegProcTable_s); i++){
//term_printf("regId:%d, g_RegTbl.regId :%d\r\n",regId,g_RegTbl[i].regId);
if (regId == g_RegTbl[i].regId){
return g_RegTbl[i].pRegProc(NULL);
}
}
return 0;
}
/**
* @brief 处理读取寄存器数据
* @param
* @retval
*/
void FRT_MsgProc_ReadRegister(device_handle device, void *pMsg)
{
// static u_int8_t reg_value_buff[2*100]={0x00};
//
// memset(reg_value_buff,0,sizeof(reg_value_buff));
// frt_climate_pack *pack = (frt_climate_pack*)pMsg;
//
// u_int16_t start_reg_addr = (pack->addr_begin_high_byte<<8)| pack->addr_begin_low_byte;
// u_int16_t reg_num= (pack->regnum_begin_high_byte<<8)| pack->regnum_begin_low_byte;
//
// AssertError(start_reg_addr < 100,return, "读取寄存器起始地址错误%d",start_reg_addr);
// AssertError(reg_num < 100,return, "读取寄存器数量错误%d",reg_num);
//
// /* 采集sht30数据 */
// AssertError(get_temp_humi_data(&g_stMcs_Para.temperature, &g_stMcs_Para.humidity),g_stMcs_Para.temperature=0;g_stMcs_Para.humidity=0,"采集sht30温湿度数据失败" );
//
//// /* 调试信息 */
//// pdebug_mcs_info();
//
// for(u_int16_t pos=0; pos <reg_num; pos++){
// *(u_int16_t*)&reg_value_buff[pos*2] = FRT_ReadReg((start_reg_addr+pos));
// }
//
// if(device == g_term_uart_handle)
// {
// pdebug_mcs_info();
// }
// if(device == g_rs485_uart_handle)
// {
// send_uart_pack(device, FRT_FUNCTION_CODE_READ_REGISTER, (u_int8_t*)&reg_value_buff, reg_num*2);
// pdebug_mcs_info();
// }
uint8_t *data = (uint8_t *)pMsg;
u_int16_t start_reg_addr = (data[2] << 8)| data[3];
u_int16_t reg_num= (data[4] << 8)| data[5];
if ( \
start_reg_addr < 0x00 ||\
(start_reg_addr > FRT_REGISTER_PRESSURE && start_reg_addr < FRT_REGISTER_DEVICE_ADDR) ||\
(start_reg_addr > FRT_REGISTER_TEMPHUM_UPDATE_TIME && start_reg_addr < FRT_REGISTER_TRANSDUCER_CFG_1R5) ||\
start_reg_addr > FRT_REGISTER_LINEAR_ENABLE \
)
{
term_printf("start_reg_addr error:%d", start_reg_addr);
return;
}
if ( \
reg_num < 0x01 ||\
(((reg_num + start_reg_addr - 1) > FRT_REGISTER_PRESSURE) && ((reg_num + start_reg_addr - 1) < FRT_REGISTER_DEVICE_ADDR)) ||\
(((reg_num + start_reg_addr -1) > FRT_REGISTER_TEMPHUM_UPDATE_TIME) && ((reg_num + start_reg_addr -1) < FRT_REGISTER_TRANSDUCER_CFG_1R5)) ||\
((reg_num + start_reg_addr -1) > FRT_REGISTER_LINEAR_ENABLE) \
)
{
term_printf("reg_num error:%d", reg_num);
return;
}
/* 采集sht30数据 */
// AssertError(get_temp_humi_data(&g_stMcs_Para.temperature, &g_stMcs_Para.humidity),g_stMcs_Para.temperature=0;g_stMcs_Para.humidity=0,"采集sht30温湿度数据失败" );
// /* 调试信息 */
// pdebug_mcs_info();
static u_int8_t reg_value_buff[2*100]={0x00};
memset(reg_value_buff,0,sizeof(reg_value_buff));
for(u_int16_t pos=0; pos <reg_num; pos++){
*(u_int16_t*)&reg_value_buff[pos*2] = FRT_ReadReg((start_reg_addr+pos));
}
if(device == g_term_uart_handle)
{
send_uart_pack(device, FRT_FUNCTION_CODE_READ_REGISTER, (u_int8_t*)&reg_value_buff, reg_num*2);
// pdebug_mcs_info();
}
if(device == g_rs485_uart_handle)
{
send_uart_pack(device, FRT_FUNCTION_CODE_READ_REGISTER, (u_int8_t*)&reg_value_buff, reg_num*2);
// pdebug_mcs_info();
}
}
/**
* @brief 处理写入寄存器数据
* @param
* @retval
*/
void FRT_MsgProc_WriteRegister(device_handle device, void *pMsg)
{
uint8_t *data = (uint8_t *)pMsg;
u_int16_t start_reg_addr = (data[2] << 8)| data[3];
u_int16_t reg_num= (data[4] << 8)| data[5];
u_int16_t byte_num= data[6];
unsigned short return_crc_value;
// 校验
if (start_reg_addr < FRT_REGISTER_DEVICE_ADDR ||\
(start_reg_addr > FRT_REGISTER_REST_DEFAULT_SETTING && start_reg_addr < FRT_REGISTER_TRANSDUCER_CFG_1R5) ||\
(start_reg_addr > FRT_REGISTER_DISTANCE && start_reg_addr < FRT_REGISTER_LINEAR_POINT_Y_1) ||\
(start_reg_addr > FRT_REGISTER_LINEAR_ENABLE) \
)
{
term_printf("start_reg_addr error:%d", start_reg_addr);
return;
}
if (reg_num < 0x01 ||\
((reg_num + start_reg_addr - 1) < FRT_REGISTER_DEVICE_ADDR) ||\
(((reg_num + start_reg_addr -1) > FRT_REGISTER_REST_DEFAULT_SETTING) && ((reg_num + start_reg_addr -1) < FRT_REGISTER_TRANSDUCER_CFG_1R5)) ||\
(((reg_num + start_reg_addr -1) > FRT_REGISTER_DISTANCE) && ((reg_num + start_reg_addr -1) < FRT_REGISTER_LINEAR_POINT_Y_1)) ||\
((reg_num + start_reg_addr -1) > FRT_REGISTER_LINEAR_ENABLE) \
)
{
term_printf("reg_num error:%d", reg_num);
return;
}
// 取出数据
uint16_t content[50] = {0};
for (uint16_t var = 0; var < reg_num; var++)
{
content[var] = data[7 + 2 * var] << 8 | data[7 + 2 * var + 1];
}
//// 调试
// for (int i = 0; i < 20; i++)
// {
// term_printf("%x", content[i]);
// }
// 发回数据
uint8_t Trans_data[6];
Trans_data[0] = g_usrConfigInfo.addr;
Trans_data[1] = data[1];
Trans_data[2] = start_reg_addr;
Trans_data[3] = reg_num;
return_crc_value = CRC16(Trans_data, 4);
Trans_data[4] = return_crc_value;
Trans_data[5] = return_crc_value >> 8;
uart_dev_write(device, Trans_data, 6);
for(u_int16_t pos=0; pos <reg_num; pos++)
{
for (uint16_t i = 0; i < sizeof(g_Write_RegTbl) / sizeof(FRT_RegProcTable_s); i++)
{
if (g_Write_RegTbl[i].regId == (start_reg_addr + pos))
{
g_Write_RegTbl[i].pRegProc(&content[pos]);
}
}
}
// if(device == g_term_uart_handle)
// {
// pdebug_mcs_info();
// }
// if(device == g_rs485_uart_handle)
// {
// pdebug_mcs_info();
// }
}
/**
* @brief 读取串口数据
* @param
* @retval
*/
static int uart_read_frt_climate_pack(device_handle uart_handle,u_int8_t *buff, u_int32_t buff_size)
{
u_int32_t offset = 0;
char c = 0;
// frt_climate_pack *pack = (frt_climate_pack *)buff;
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] != g_usrConfigInfo.addr && buff[0] != 0xFF)
{
memcpy(buff, buff + 1, offset - 1);
offset--;
buff_size--;
}
}
// 判断地址对不对,不对缩一位
else if(buff[1] != FRT_FUNCTION_CODE_READ_REGISTER && buff[1] != FRT_FUNCTION_CODE_WRITE_REGISTER)
{
memcpy(buff, buff + 1, offset - 1);
offset--;
buff_size--;
}
// 读寄存器
else if (buff[1] == FRT_FUNCTION_CODE_READ_REGISTER & offset == 8)
{
return offset;
}
// 写寄存器
else if (buff[1] == FRT_FUNCTION_CODE_WRITE_REGISTER & offset == 7 + buff[6] * 2 +2)
{
return offset;
}
}
return 0;
}
/**
* @brief 处理一条消息
* @param
* @retval
*/
void FRT_MsgHandler(device_handle device, u_int8_t *pMsg, u_int32_t MsgLen)
{
// frt_climate_pack *pack = (frt_climate_pack*)pMsg;
//
// //AssertErrorNoPrint((CRC16(pMsg, MsgLen-2) == FRT_CLIMATE_PACK_CRC16(pHeader)),return);
//// AssertError((CRC16(pMsg, MsgLen-2) == FRT_CLIMATE_PACK_CRC16(pack)),return,"crc16校验失败");
//// AssertError((pack->func == FRT_FUNCTION_CODE_READ_REGISTER) ||\
//// (pack->func == FRT_FUNCTION_CODE_WRITE_REGISTER),return,"crc16校验失败");
//// 断言有问题CRC出错卡死
// if(CRC16(pMsg, MsgLen-2) != FRT_CLIMATE_PACK_CRC16(pack))
// {
//// term_printf("CRC不过");
// return;
// }
// if((pack->func != FRT_FUNCTION_CODE_READ_REGISTER) && (pack->func != FRT_FUNCTION_CODE_WRITE_REGISTER))
// {
//// term_printf("功能码不过");
// return;
// }
//
// for (u_int16_t i = 0; i < sizeof(g_MsgTbl) / sizeof(FRT_FuncionMsgProcTable_s); i++){
// if (pack->func == g_MsgTbl[i].msgId){
// g_MsgTbl[i].pMsgProc(device, pMsg);
// }
// }
if(CRC16(pMsg, MsgLen-2) != FRT_CLIMATE_BUFF_CRC16(pMsg))
{
// term_printf("CRC不过");
return;
}
if((pMsg[1] != FRT_FUNCTION_CODE_READ_REGISTER) && (pMsg[1] != FRT_FUNCTION_CODE_WRITE_REGISTER))
{
// term_printf("功能码不过");
return;
}
for (u_int16_t i = 0; i < sizeof(g_MsgTbl) / sizeof(FRT_FuncionMsgProcTable_s); i++){
if (pMsg[1] == g_MsgTbl[i].msgId){
g_MsgTbl[i].pMsgProc(device, pMsg);
}
}
}
/**
* @brief 读取并解析串口数据
* @param
* @retval
*/
static u_int8_t rs485_buff[50]={0x00};
void read_and_process_uart_data(device_handle device)
{
if(uart_dev_char_present(device)){
osDelay(20);
memset(rs485_buff,0,sizeof(rs485_buff));
int ret = uart_read_frt_climate_pack(device, rs485_buff, sizeof(rs485_buff));
if(ret > 0){
FRT_MsgHandler(device, rs485_buff, ret);
}
}
}
/* 打印微气象数据 */
static void pdebug_mcs_info()
{
term_printf("min_wind_direction: %.2f\r\n", g_stMcs_Para.min_wind_direction);
term_printf("average_wind_direction: %.2f\r\n", g_stMcs_Para.average_wind_direction);
term_printf("max_wind_direction: %.2f\r\n", g_stMcs_Para.max_wind_direction);
term_printf("min_wind_speed: %.2f\r\n", g_stMcs_Para.min_wind_speed);
term_printf("average_wind_speed: %.2f\r\n", g_stMcs_Para.average_wind_speed);
term_printf("max_wind_speed: %.2f\r\n", g_stMcs_Para.max_wind_speed);
term_printf("temperature: %.2f\r\n", g_stMcs_Para.temperature);
term_printf("humidity: %.2f\r\n", g_stMcs_Para.humidity);
term_printf("pressure: %.2f\r\n", g_stMcs_Para.pressure);
term_printf("rain: %.2f\r\n", g_stMcs_Para.precipitation);
// term_printf("precipitation_intensity: %.2f\r\n", g_stMcs_Para.precipitation_intensity);
}
// 30 03 00 00 00 0B 00 2C