Files
CHJ/user/Protocol/ReadWriteDataByCom.c
2026-03-20 21:19:04 +08:00

593 lines
19 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 "../main/SystemInclude.h"
u16 parameterState;
/******************************************************************************/
void ReadTimeBase(void)
{
//===========================================================================
#ifndef ENABLE_EXT_RTC
#pragma message("[undefined] ENABLE_EXT_RTC")
#elif(ENABLE_EXT_RTC)
if(TestExtRTCReady()) ExtRTCTimeRead();
else ReadCurrentTime();
#else
// ReadCurrentTime();
#endif
MBBuf.RxPointer[MBBuf.Index++] = timer[YEAR];
MBBuf.RxPointer[MBBuf.Index++] = timer[MONTH];
MBBuf.RxPointer[MBBuf.Index++] = timer[DATE];
MBBuf.RxPointer[MBBuf.Index++] = timer[HOUR];
MBBuf.RxPointer[MBBuf.Index++] = timer[MINUTE];
MBBuf.RxPointer[MBBuf.Index++] = timer[SECOND];
MBBuf.DataByte = 6;
ModbusVariablePointerDec();
}
/******************************************************************************/
void WriteTimeBase(void)
{
MBBuf.DataByte = 6;
if(MBBuf.ByteNumber < 6)
{
MBBuf.ByteNumber = 0;
MBBuf.BusError = ILLEGAL_DATA_VALUE;
return;
}
timer[YEAR] = MBBuf.RxPointer[MBBuf.Index++];
timer[MONTH] = MBBuf.RxPointer[MBBuf.Index++];
timer[DATE] = MBBuf.RxPointer[MBBuf.Index++];
timer[HOUR] = MBBuf.RxPointer[MBBuf.Index++];
timer[MINUTE] = MBBuf.RxPointer[MBBuf.Index++];
timer[SECOND] = MBBuf.RxPointer[MBBuf.Index++];
//===========================================================================
#ifndef ENABLE_EXT_RTC
#pragma message("[undefined] ENABLE_EXT_RTC")
#elif(ENABLE_EXT_RTC)
if(TestExtRTCReady()) ExtRTCTimeSet();
#endif
//FY
// RTCCTL0 = 0xA500 + RTCTEVIE;
// RTCCTL13 = RTCHOLD;
// RTCYEAR = (u16)timer[YEAR]+2000;
// RTCMON = timer[MONTH];
// RTCDAY = timer[DATE];
// RTCHOUR = timer[HOUR];
// RTCMIN = timer[MINUTE];
// RTCSEC = timer[SECOND];
// RTCCTL0 = 0xA500 + RTCTEVIE;
// RTCCTL13 = RTCMODE; // Minute changed
// RTCCTL0 = 0;
}
/******************************************************************************/
u16 ReadSecond(void)
{
// u16 newSecond = RTCHOUR;
//
// if(newSecond > 11) newSecond -= 12;
// newSecond *= 60;
// newSecond += (u16)RTCMIN;
// newSecond *= 60;
// newSecond += (u16)RTCSEC;
// return newSecond;
return 1;
}
/******************************************************************************/
void SaveFactorySetting(void) // Save the factory setting
{
u16 I;
u8 temp[64];
for(I=0; I< SAVE_PARA_MAX; I++)
{
ReadMultiByteFromEEPROM(I*64, temp, 64, PARA_EEPROM);
WriteMultiByteToEEPROM(I*64, temp, 64, DATA_EEPROM);
}
WriteaAnAlarmData(0);
}
/******************************************************************************/
void RestoreFactorySetting(void)
{
u16 I;
unsigned char temp[64];
for(I=0; I< SAVE_PARA_MAX; I++)
{
ReadMultiByteFromEEPROM(I*64, temp, 64, DATA_EEPROM);
WriteMultiByteToEEPROM(I*64, temp, 64, PARA_EEPROM);
}
SystemParameterInit();
}
/******************************************************************************/
void WriteMEMSRatio(void)
{
// tmpLA = (u32)(NRHHigh - NRHLow);
// tmpLA *= (u32)RRRHRatio;
// tmpLA /= (u32)(RHHighTa - RHLowTa);
// tempL.Word[0] = (u16)tmpLA;
//
// MEMSRatio = WriteShortParameterToEEPROM(MEMS_RATIO, MEMS_RATIO_WIDTH);
//FY
}
/******************************************************************************/
//<2F>DZ<EFBFBD><C7B1><EFBFBD>Ϣ
void ReadCalibFAC(void)
{
tempL.Word[0] = 0x8000;//FAC<41><43><EFBFBD><EFBFBD>0x8000 <20><>ʾУ׼<D0A3><D7BC><EFBFBD>ݴ洢<DDB4><E6B4A2>ʽΪ<CABD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E6B4A2>FL1 FM1 FH1 <20><><EFBFBD><EFBFBD>FL2 FM2 FH2 <20><>......
//------------------------------------------------------------------------------
#ifndef ENABLE_ONE_CURVE
#pragma message("[undefined] ENABLE_ONE_CURVE")
#elif(ENABLE_ONE_CURVE)
calibType = INTCMD_CTYPE_CURVE;
#endif
//------------------------------------------------------------------------------
if(calibType == INTCMD_CTYPE_CURVE) tempL.Word[0] |= CALIB_COMP_FAC;//DATA_COMP_FAC
//------------------------------------------------------------------------------
#ifndef ENABLE_2ND_CURVE
#pragma message("[undefined] ENABLE_2ND_CURVE")
#elif(ENABLE_2ND_CURVE)
else if(calibType == INT_2ND_CURVE_CMD) tempL.Word[0] |= DATA_COMP_FAC2;
#endif
//------------------------------------------------------------------------------
#ifndef ENABLE_3RD_CURVE
#pragma message("[undefined] ENABLE_3RD_CURVE")
#elif(ENABLE_3RD_CURVE)
else if(calibType == INT_3RD_CURVE_CMD) tempL.Word[0] |= DATA_COMP_FAC3;
#endif
//------------------------------------------------------------------------------
else tempL.Word[0] = 0;
curveMode |= 0x0005;
}
/******************************************************************************/
void ReadCalibDepth(void)
{
//------------------------------------------------------------------------------
#ifndef ENABLE_ONE_CURVE
#pragma message("[undefined] ENABLE_ONE_CURVE")
#elif(ENABLE_ONE_CURVE)
calibType = INTCMD_CTYPE_CURVE;
#endif
//------------------------------------------------------------------------------
if(calibType == INTCMD_CTYPE_CURVE)
{
tempL.Word[0] = CALIB_DEPTH;
}
//------------------------------------------------------------------------------
#ifndef ENABLE_2ND_CURVE
#pragma message("[undefined] ENABLE_2ND_CURVE")
#elif(ENABLE_2ND_CURVE)
else if(calibType == INT_2ND_CURVE_CMD) tempL.Word[0] = CALIB_DEPTH2;
#endif
//------------------------------------------------------------------------------
#ifndef ENABLE_3RD_CURVE
#pragma message("[undefined] ENABLE_3RD_CURVE")
#elif(ENABLE_3RD_CURVE)
else if(calibType == INT_3RD_CURVE_CMD) tempL.Word[0] = CALIB_DEPTH3;
#endif
//------------------------------------------------------------------------------
else tempL.Word[0] = 0;
curveMode |= 0x00a0;
}
/******************************************************************************/
void ReadCalibWidth(void)
{
tempL.Word[0] = CALIB_WIDTH;
curveMode |= 0xa500;
}
/******************************************************************************/
void ReadCalbrationDataByCom(u16 calibAddr, u16 calibDepth)
{
MBBuf.StartAddr &= 0x0fff;
if((MBBuf.StartAddr >= MAX_OPERATE_DEPTH) || (MBBuf.ByteNumber > CALIB_WIDTH))
{
MBBuf.ByteNumber = 0;
MBBuf.BusError = ILLEGAL_DATA_ADDRESS;
return;
}
MBBuf.StartAddr *= CALIB_WIDTH;
MBBuf.StartAddr += calibAddr;
disable_interrupts();
ReadMultiByteFromEEPROM(MBBuf.StartAddr, &MBBuf.RxPointer[3], MBBuf.ByteNumber, PARA_EEPROM);
enable_interrupts();
//systemProcessing |= WRITE_DATA_HANDLE;
MBBuf.ByteNumber = 0;
}
/******************************************************************************/
void WriteCalbrationDataByCom(u16 calibAddr, u16 calibDepth)
{
//---------------------------------------------------------------------------
if((securityID != (u32)COM_PASSWORD1) && (securityID != (u32)COM_PASSWORD2)) return;
IDvalidTime = 0;
//---------------------------------------------------------------------------
MBBuf.StartAddr &= 0x0fff;
if((MBBuf.StartAddr >= calibDepth) || (MBBuf.ByteNumber > CALIB_WIDTH))
{
MBBuf.ByteNumber = 0;
MBBuf.BusError = ILLEGAL_DATA_ADDRESS;
return;
}
disable_interrupts();
if(MBBuf.StartAddr == 0) WriteMeterBasicData();
MBBuf.StartAddr *= CALIB_WIDTH;
MBBuf.StartAddr += calibAddr;
WriteMultiByteToEEPROM(MBBuf.StartAddr, &MBBuf.RxPointer[MBBuf.Index], MBBuf.ByteNumber, PARA_EEPROM);
enable_interrupts();
//systemProcessing |= WRITE_DATA_HANDLE;
MBBuf.ByteNumber = 0;
}
/******************************************************************************/
void ReadEEPROMByCom(u16 RDAdr, u16 RDLen)
{
ReadMultiByteFromEEPROM(RDAdr, &MBBuf.RxPointer[MBBuf.Index], MBBuf.ByteNumber, PARA_EEPROM);
MBBuf.DataByte = RDLen;
MBBuf.Index += RDLen;
ModbusVariablePointerDec();
}
/******************************************************************************/
void ReadFlashByCom(const unsigned char *RDAdr, u16 RDLen)
{
u16 I;
for(I=0; I<RDLen; I++) MBBuf.RxPointer[MBBuf.Index++] = *(RDAdr+I);
MBBuf.DataByte = RDLen;
ModbusVariablePointerDec();
}
/******************************************************************************/
void ReadArrayByCom(unsigned char *RDAdr, u16 RDLen)
{
u16 I;
for(I=0; I<RDLen; I++) MBBuf.RxPointer[MBBuf.Index++] = *(RDAdr+I);
MBBuf.DataByte = RDLen;
ModbusVariablePointerDec();
}
/******************************************************************************/
void ReadDByteByCom(u16 RDValue)
{
tempL.DWord = RDValue;
MBBuf.RxPointer[MBBuf.Index++] = tempL.Byte[1];
MBBuf.RxPointer[MBBuf.Index++] = tempL.Byte[0];
MBBuf.DataByte = 2;
ModbusVariablePointerDec();
}
/******************************************************************************/
void ReadDWordByCom(u32 RDValue)
{
tempL.DWord = RDValue;
MBBuf.RxPointer[MBBuf.Index++] = tempL.Byte[3];
MBBuf.RxPointer[MBBuf.Index++] = tempL.Byte[2];
MBBuf.RxPointer[MBBuf.Index++] = tempL.Byte[1];
MBBuf.RxPointer[MBBuf.Index++] = tempL.Byte[0];
MBBuf.DataByte = 4;
ModbusVariablePointerDec();
}
/******************************************************************************/
void WriteEEPROMByCom(u16 WRAdr, u16 WRLen)
{
MBBuf.DataByte = WRLen;
if(MBBuf.ByteNumber < WRLen)
{
MBBuf.ByteNumber = 0;
MBBuf.BusError = ILLEGAL_DATA_ADDRESS;
return;
}
WriteMultiByteToEEPROM(WRAdr, &MBBuf.RxPointer[MBBuf.Index], WRLen, PARA_EEPROM);
MBBuf.Index += WRLen;
}
/******************************************************************************/
void WriteWordByCom(u16 *WRSource, u16 WRAddr,
u16 WRLen, u16 WRMax,
u16 WRMin )
{
//---------------------------------------------------------------------------
parameterState = ABORD_OPERATION;
MBBuf.BusError = ILLEGAL_DATA_VALUE;
//---------------------------------------------------------------------------
MBBuf.DataByte = 2;
if(MBBuf.ByteNumber < MBBuf.DataByte)
{
MBBuf.ByteNumber = 0;
return;
}
u16 I;
tempL.Byte[1] = MBBuf.RxPointer[MBBuf.Index++];
tempL.Byte[0] = MBBuf.RxPointer[MBBuf.Index++];
//---------------------------------------------------------------------------
MBBuf.BusError = ACKNOWLEDGE;
if((securityID != 0x0000AA55) && (securityID != COM_PASSWORD1) && (securityID != COM_PASSWORD2)) return;
MBBuf.BusError = 0; //V2004
IDvalidTime = 0;
//---------------------------------------------------------------------------
if(tempL.Word[0] > WRMax) return;
else if(tempL.Word[0] < WRMin) return;
// MBBuf.BusError = 0; V2004
I = *WRSource;
if(tempL.Word[0] == I) return;
parameterState = NEED_OPERATION;
//I = WRLen - 1;
//tempL.Byte[I] = CRC8(tempL.Byte, I);
//WriteMultiByteToEEPROM(WRAddr, tempL.Byte, WRLen, PARA_EEPROM);
//ReadMultiByteFromEEPROM(WRAddr, tempL.Byte, WRLen, PARA_EEPROM);
//tempL.Byte[I] = 0;
//*WRSource = tempL.Word[0];
*WRSource = WriteShortParameterToEEPROM(WRAddr, WRLen);
}
/******************************************************************************/
void FreeWriteWordByCom(u16 *WRSource, u16 WRAddr,
u16 WRLen, u16 WRMax,
u16 WRMin )
{
//---------------------------------------------------------------------------
parameterState = ABORD_OPERATION;
MBBuf.BusError = ILLEGAL_DATA_VALUE;
//---------------------------------------------------------------------------
MBBuf.DataByte = 2;
if(MBBuf.ByteNumber < MBBuf.DataByte)
{
MBBuf.ByteNumber = 0;
return;
}
//---------------------------------------------------------------------------
MBBuf.BusError = 0; //V2004
u16 I;
tempL.Byte[1] = MBBuf.RxPointer[MBBuf.Index++];
tempL.Byte[0] = MBBuf.RxPointer[MBBuf.Index++];
if(tempL.Word[0] > WRMax) return;
else if(tempL.Word[0] < WRMin) return;
//MBBuf.BusError = 0; V2004
I = *WRSource;
if(tempL.Word[0] == I) return;
//---------------------------------------------------------------------------
parameterState = NEED_OPERATION;
//---------------------------------------------------------------------------
*WRSource = WriteShortParameterToEEPROM(WRAddr, WRLen);
}
/******************************************************************************/
void WriteDWordByCom(u32 *WRSource, u16 WRAddr,u16 WRLen,
u32 WRMax, u32 WRMin )
{
//---------------------------------------------------------------------------
parameterState = ABORD_OPERATION;
MBBuf.BusError = ILLEGAL_DATA_VALUE;
//---------------------------------------------------------------------------
MBBuf.DataByte = 4;
if(MBBuf.ByteNumber < MBBuf.DataByte)
{
MBBuf.ByteNumber = 0;
return;
}
//---------------------------------------------------------------------------
tempDev.Byte[3] = MBBuf.RxPointer[MBBuf.Index++];
tempDev.Byte[2] = MBBuf.RxPointer[MBBuf.Index++];
tempDev.Byte[1] = MBBuf.RxPointer[MBBuf.Index++];
tempDev.Byte[0] = MBBuf.RxPointer[MBBuf.Index++];
//---------------------------------------------------------------------------
MBBuf.BusError = ACKNOWLEDGE;
if((securityID != 0x0000AA55) && (securityID != COM_PASSWORD1) && (securityID != COM_PASSWORD2)) return;
MBBuf.BusError = 0; //V2004
IDvalidTime = 0;
//---------------------------------------------------------------------------
if(tempDev.DWord[0] > WRMax) return;
else if(tempDev.DWord[0] < WRMin) return;
//MBBuf.BusError = 0; V2004
tempDev.DWord[1] = *WRSource;
if(tempDev.DWord[0] == tempDev.DWord[1]) return;
//---------------------------------------------------------------------------
parameterState = NEED_OPERATION;
//---------------------------------------------------------------------------
*WRSource = WriteParameterToEEPROM(WRAddr, WRLen);
}
/******************************************************************************/
void SetWordCmdByCom(u16 *WRSource)
{
//---------------------------------------------------------------------------
parameterState = ABORD_OPERATION;
MBBuf.BusError = ILLEGAL_DATA_VALUE;
//---------------------------------------------------------------------------
MBBuf.DataByte = 2;
if(MBBuf.ByteNumber < 2)
{
MBBuf.ByteNumber = 0;
return;
}
//---------------------------------------------------------------------------
MBBuf.BusError = ACKNOWLEDGE;
if((securityID != 0x0000AA55) && (securityID != COM_PASSWORD1) && (securityID != COM_PASSWORD2)) return;
MBBuf.BusError = 0; //V2004
IDvalidTime = 0;
//---------------------------------------------------------------------------
parameterState = NEED_OPERATION;
//MBBuf.BusError = 0;
//---------------------------------------------------------------------------
tempL.Byte[1] = MBBuf.RxPointer[MBBuf.Index++];
tempL.Byte[0] = MBBuf.RxPointer[MBBuf.Index++];
*WRSource = tempL.Word[0];
}
/******************************************************************************/
void FreeSetWordCmdByCom(u16 *WRSource)
{
MBBuf.DataByte = 2;
if(MBBuf.ByteNumber < 2)
{
MBBuf.ByteNumber = 0;
parameterState = ABORD_OPERATION;
MBBuf.BusError = ILLEGAL_DATA_VALUE;
return;
}
parameterState = NEED_OPERATION;
MBBuf.BusError = 0;
tempL.Byte[1] = MBBuf.RxPointer[MBBuf.Index++];
tempL.Byte[0] = MBBuf.RxPointer[MBBuf.Index++];
*WRSource = tempL.Word[0];
}
/******************************************************************************/
void SetDWordCmdByCom(u32 *WRSource)
{
//---------------------------------------------------------------------------
parameterState = ABORD_OPERATION;
MBBuf.BusError = ILLEGAL_DATA_VALUE;
MBBuf.DataByte = 4;
if(MBBuf.ByteNumber < MBBuf.DataByte)
{
MBBuf.ByteNumber = 0;
return;
}
//---------------------------------------------------------------------------
MBBuf.BusError = ACKNOWLEDGE;
if((securityID != 0x0000AA55) && (securityID != COM_PASSWORD1) && (securityID != COM_PASSWORD2)) return;
MBBuf.BusError = 0; //V2004
IDvalidTime = 0;
//---------------------------------------------------------------------------
parameterState = NEED_OPERATION;
//MBBuf.BusError = 0;
//---------------------------------------------------------------------------
tempL.Byte[3] = MBBuf.RxPointer[MBBuf.Index++];
tempL.Byte[2] = MBBuf.RxPointer[MBBuf.Index++];
tempL.Byte[1] = MBBuf.RxPointer[MBBuf.Index++];
tempL.Byte[0] = MBBuf.RxPointer[MBBuf.Index++];
*WRSource = tempL.DWord;
}
/******************************************************************************/
void FreeSetDWordCmdByCom(u32 *WRSource)
{
MBBuf.DataByte = 4;
if(MBBuf.ByteNumber < MBBuf.DataByte)
{
MBBuf.ByteNumber = 0;
//---------------------------------------------------------------------------
parameterState = ABORD_OPERATION;
MBBuf.BusError = ILLEGAL_DATA_VALUE;
//---------------------------------------------------------------------------
return;
}
//---------------------------------------------------------------------------
parameterState = NEED_OPERATION;
MBBuf.BusError = 0;
//---------------------------------------------------------------------------
tempL.Byte[3] = MBBuf.RxPointer[MBBuf.Index++];
tempL.Byte[2] = MBBuf.RxPointer[MBBuf.Index++];
tempL.Byte[1] = MBBuf.RxPointer[MBBuf.Index++];
tempL.Byte[0] = MBBuf.RxPointer[MBBuf.Index++];
*WRSource = tempL.DWord;
return;
}
/******************************************************************************/
//<2F><><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void WriteMeterBasicData(void)
{
u16 I,J=0;
unsigned char temp[ANX*2];
for(I=0; I<ANX; I++)
{
temp[J++] = voltageDetected[I] & 0xFF;
temp[J++] = voltageDetected[I] >> 8;
}
WriteMultiByteToEEPROM(BASIC_DATA_BASE, temp, BASIC_DATA_BYTE_MAX, PARA_EEPROM);
}
/**************************************************************************************************************************/
void WriteWordByComToFlash(u16* word,u8 WriteParams)
{
tempL.Byte[1] = MBBuf.RxPointer[MBBuf.Index++];
tempL.Byte[0] = MBBuf.RxPointer[MBBuf.Index++];
*word = tempL.Word[0];
MBBuf.BusError = 0;
munWriteParams = WriteParams; //ָ<><D6B8><EFBFBD><EFBFBD>Ҫд<D2AA><D0B4><EFBFBD><EFBFBD>Flash<73><68><EFBFBD><EFBFBD>
if(mucSaveParams != PARAMS_ING) mucSaveParams = PARAMS_READY;//word <20><><EFBFBD><EFBFBD><EFBFBD>ڸ<EFBFBD><DAB8><EFBFBD>Flashʱ<68><CAB1>װ<EFBFBD><D7B0>
}