You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
4.4 KiB

3 weeks ago
/******************************************************************************
(C), 2018-2099, Radkil
******************************************************************************
: client_setting.c
: 稿
: radkil
: 2026820
:
: PVIV中间层
:
1. : 2026820
: radkil
:
******************************************************************************/
3 weeks ago
#include "BHBF.h"
3 weeks ago
#include "pb.h"
#include "pb_decode.h"
#include "pb_encode.h"
#include "bsp_PV.pb.h"
#include "bsp_IV.pb.h"
3 weeks ago
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
static uint32_t g_uiIVModuleID = 0;
3 weeks ago
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};
3 weeks ago
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
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;
3 weeks ago
}
}
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));
3 weeks ago
}
}
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)
3 weeks ago
{
if (NULL == pstMsg)
3 weeks ago
{
return;
}
3 weeks ago
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);
Rd_Delay(1000);
}
break;
}
default:
break;
3 weeks ago
}
}
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) osPriorityHigh4,
};
(void)osThreadNew(Send_IV, NULL, &Send_PV_attributes);
}