/****************************************************************************** 版权所有 (C), 2018-2099, Radkil ****************************************************************************** 文 件 名 : client_setting.c 版 本 号 : 初稿 作 者 : radkil 生成日期 : 2026年8月20日 最近修改 : 功能描述 : PVIV中间层 修改历史 : 1.日 期 : 2026年8月20日 作 者 : radkil 修改内容 : 创建文件 ******************************************************************************/ #include "BHBF.h" #include "pb.h" #include "pb_decode.h" #include "pb_encode.h" #include "bsp_PV.pb.h" #include "bsp_IV.pb.h" /*----------------------------------------------* * 外部变量说明 * *----------------------------------------------*/ /*----------------------------------------------* * 外部函数原型说明 * *----------------------------------------------*/ /*----------------------------------------------* * 内部函数原型说明 * *----------------------------------------------*/ /*----------------------------------------------* * 全局变量 * *----------------------------------------------*/ /*----------------------------------------------* * 模块级变量 * *----------------------------------------------*/ static uint32_t g_uiIVModuleID = 0; static PV_struct_define decoded_PV_variable = { 0 }; static PV_struct_define decoded_PV = { 0 }; static IV_struct_define IV = { 0 }; static char g_IV_buffer[1024] = {0}; /*----------------------------------------------* * 常量定义 * *----------------------------------------------*/ /*----------------------------------------------* * 宏定义 * *----------------------------------------------*/ int check_PV(char *_pBuffer, uint32_t _iSize) { if (_pBuffer[0] != 0x55) return -1; if (_pBuffer[1] != 0x55) return -1; uint16_t crc_check = ((_pBuffer[_iSize - 1] << 8) | _pBuffer[_iSize - 2]); uint16_t crc_check1 = Rd_modbusCRC16((uint8_t *)_pBuffer, _iSize - 2); if (crc_check == crc_check1) { return _iSize; } else { return 0; } } void decode_PV(const char *_pBuffer, uint32_t _iSize) { if (_pBuffer[2] == 0x01 && _pBuffer[3] == 0x01) //01 01 设置PV { pb_istream_t i_pv_stream; i_pv_stream = pb_istream_from_buffer((const pb_byte_t *)&_pBuffer[4], _iSize - 6); pb_decode(&i_pv_stream, PV_struct_define_fields, &decoded_PV_variable); if(decoded_PV_variable.TimeStamp>decoded_PV.TimeStamp) { decoded_PV=decoded_PV_variable; MsgCenter_SendTo(MODULE_NAME_CUSTOM, CUSTOM_GET_PV, (void *)&decoded_PV, sizeof(decoded_PV)); } } else if (*(_pBuffer + 2) == 0x02 && *(_pBuffer + 3) == 0x01) //设置PV { } else if (*(_pBuffer + 2) == 0x03 && *(_pBuffer + 3) == 0x01) //返回IV { } else { } } static void SendIV_ModuleHandler(const Msg_t *pstMsg) { if (NULL == pstMsg) { return; } switch (pstMsg->m_uiMsgID) { case CUSTOM_SET_IV: { if (pstMsg->m_uiDataLen >= sizeof(IV)) { RD_MEMCPY(&IV, pstMsg->m_aucData, sizeof(IV)); pb_ostream_t IV_o_stream = pb_ostream_from_buffer((pb_byte_t *)&g_IV_buffer[2], sizeof(g_IV_buffer) - 2); pb_encode(&IV_o_stream, IV_struct_define_fields, &IV); g_IV_buffer[0] = 0x55; g_IV_buffer[1] = 0x55; uint16_t crc = Rd_modbusCRC16((const uint8_t *)&(g_IV_buffer[0]), IV_o_stream.bytes_written + 2); g_IV_buffer[IV_o_stream.bytes_written + 2] = (crc>>8) &0xff; g_IV_buffer[IV_o_stream.bytes_written + 3] = crc & 0xff;; rd_ComSend(g_ptlpuart, g_IV_buffer, IV_o_stream.bytes_written + 4); } break; } default: break; } } static void Send_IV(void *argument) { while(1) { MsgCenter_ProcessWait(g_uiIVModuleID, 2); } } void SendIV_Init(void) { g_uiIVModuleID = MsgCenter_Register(MODULE_NAME_SENDIV, SendIV_ModuleHandler); const osThreadAttr_t Send_PV_attributes = { .name = MODULE_NAME_SENDIV, .stack_size = 1024, .priority = (osPriority_t) osPriorityRealtime1, }; (void)osThreadNew(Send_IV, NULL, &Send_PV_attributes); }