#include "StdAfx.h"
#include "RealDb.h"
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
/***************************************************************************
*********************** MemorySYSPARA ***************************
****************************************************************************/
int CreateSysPara(ADDR *pAddr) //创建系统参数共享内存
{
ASSERT(pAddr);
int SizeSysPara = sizeof(SYSTEMPARA);
pAddr->hSysparaMem = CreateFileMapping((HANDLE)0xFFFFFFFF,
NULL,PAGE_READWRITE,0,
SizeSysPara,"NZT3SYSPARA_FILE");
//pSysparaAddr->pSysPara 得到的是内存的首地址
pAddr->pSysPara = (SYSTEMPARA *)MapViewOfFile(pAddr->hSysparaMem,
FILE_MAP_ALL_ACCESS,0,0,0);
if(NULL == pAddr->pSysPara)
return -1;
else
{
memset(pAddr->pSysPara, 0, sizeof(SYSTEMPARA));
pAddr->pSysPara->maxchannum = 32; //默认值
pAddr->pSysPara->maxdevcnum = 256;
pAddr->pSysPara->maxprotoinfonum = 32;
pAddr->pSysPara->channum = 0;
pAddr->pSysPara->devcnum = 0;
pAddr->pSysPara->protoinfonum = 0;
pAddr->pSysPara->redundancy.bMaster = 0;
pAddr->pSysPara->redundancy.bValid = 0;
}
return 0;
}
int SaveSysPara(ADDR *pAddr,CString sProjPath) //将系统参数保存在ini文件中
{
ASSERT(pAddr);
char chkeyval[16];
int val = pAddr->pSysPara->maxchannum;
itoa(val,chkeyval,10);
BOOL bResult = ::WritePrivateProfileString(_T("SYSPARA"),_T("maxchannum"),chkeyval,sProjPath+_T("syspara.ini"));
val = pAddr->pSysPara->maxdevcnum;
itoa(val,chkeyval,10);
::WritePrivateProfileString(_T("SYSPARA"),_T("maxdevcnum"),chkeyval,sProjPath+_T("syspara.ini"));
val = pAddr->pSysPara->maxprotoinfonum;
itoa(val,chkeyval,10);
::WritePrivateProfileString(_T("SYSPARA"),_T("maxprotoinfonum"),chkeyval,sProjPath+_T("syspara.ini"));
val = pAddr->pSysPara->channum;
itoa(val,chkeyval,10);
::WritePrivateProfileString(_T("SYSPARA"),_T("channum"),chkeyval,sProjPath+_T("syspara.ini"));
val = pAddr->pSysPara->devcnum;
itoa(val,chkeyval,10);
::WritePrivateProfileString(_T("SYSPARA"),_T("devcnum"),chkeyval,sProjPath+_T("syspara.ini"));
val = pAddr->pSysPara->protoinfonum = pAddr->pSysPara->channum;
itoa(val,chkeyval,10);
::WritePrivateProfileString(_T("SYSPARA"),_T("protoinfonum"),chkeyval,sProjPath+_T("syspara.ini"));
itoa(pAddr->pSysPara->redundancy.bValid,chkeyval,10);
::WritePrivateProfileString(_T("REDUNDANT"),_T("valible"),chkeyval,sProjPath+_T("syspara.ini"));
itoa(pAddr->pSysPara->redundancy.bMaster,chkeyval,10);
::WritePrivateProfileString(_T("REDUNDANT"),_T("preset"),chkeyval,sProjPath+_T("syspara.ini"));
::WritePrivateProfileString(_T("REDUNDANT"),_T("RDIP"),pAddr->pSysPara->redundancy.ip,sProjPath+_T("syspara.ini"));
return 0;
}
int LoadSysPara(ADDR *pAddr,CString sProjPath) //从ini文件中装载系统参数
{
ASSERT(pAddr);
char chkeyval[16];
::GetPrivateProfileString(_T("SYSPARA"),_T("maxchannum"),"",chkeyval,16,sProjPath+_T("syspara.ini"));
pAddr->pSysPara->maxchannum = (short)atoi(chkeyval);
::GetPrivateProfileString(_T("SYSPARA"),_T("maxdevcnum"),"",chkeyval,16,sProjPath+_T("syspara.ini"));
pAddr->pSysPara->maxdevcnum = (short)atoi(chkeyval);
::GetPrivateProfileString(_T("SYSPARA"),_T("maxprotoinfonum"),"",chkeyval,16,sProjPath+_T("syspara.ini"));
pAddr->pSysPara->maxprotoinfonum = (short)atoi(chkeyval);
::GetPrivateProfileString(_T("SYSPARA"),_T("channum"),"",chkeyval,16,sProjPath+_T("syspara.ini"));
pAddr->pSysPara->channum = (short)atoi(chkeyval);
::GetPrivateProfileString(_T("SYSPARA"),_T("devcnum"),"",chkeyval,16,sProjPath+_T("syspara.ini"));
pAddr->pSysPara->devcnum = (short)atoi(chkeyval);
::GetPrivateProfileString(_T("SYSPARA"),_T("protoinfonum"),"",chkeyval,16,sProjPath+_T("syspara.ini"));
pAddr->pSysPara->protoinfonum = (short)atoi(chkeyval);
if(pAddr->pSysPara->channum>pAddr->pSysPara->maxchannum ||
pAddr->pSysPara->devcnum>pAddr->pSysPara->maxdevcnum ||
pAddr->pSysPara->protoinfonum>pAddr->pSysPara->maxprotoinfonum)
return -1;
::GetPrivateProfileString(_T("REDUNDANT"),_T("valible"),"0",chkeyval,16,sProjPath+_T("syspara.ini"));
pAddr->pSysPara->redundancy.bValid = (BOOL)atoi(chkeyval);
::GetPrivateProfileString(_T("REDUNDANT"),_T("preset"),"0",chkeyval,16,sProjPath+_T("syspara.ini"));
pAddr->pSysPara->redundancy.bMaster = (BOOL)atoi(chkeyval);
::GetPrivateProfileString(_T("REDUNDANT"),_T("RDIP"),"",pAddr->pSysPara->redundancy.ip,32,sProjPath+_T("syspara.ini"));
return 0;
}
int CloseSysPara(ADDR *pAddr) //关闭系统参数共享内存
{
ASSERT(pAddr);
if(NULL == pAddr->pSysPara)
return -1;
if(FALSE == UnmapViewOfFile(pAddr->pSysPara))
return -1;
CloseHandle(pAddr->hSysparaMem);
pAddr->pSysPara = NULL;
return 0;
}
/***************************************************************************
*********************** MemoryCHANNELPARA *****************************
****************************************************************************/
int CreateChan(ADDR *pAddr)
{
ASSERT(pAddr);
int SizeChanPara = sizeof(CHANNELPARA)*(pAddr->pSysPara->maxchannum+1);
pAddr->hChanparaMem = CreateFileMapping((HANDLE)0xFFFFFFFF,
NULL,PAGE_READWRITE,0,
SizeChanPara,"NZT3CHANPARA_FILE");
if( NULL == pAddr->hChanparaMem)
return -1;
//pSysparaAddr->pChanPara 得到的是内存的首地址
pAddr->pChanPara = (CHANNELPARA *)MapViewOfFile(pAddr->hChanparaMem,
FILE_MAP_ALL_ACCESS,0,0,0);
if(NULL == pAddr->pChanPara)
return -1;
return 0;
}
int CloseChan(ADDR *pAddr) //关闭系统参数共享内存
{
ASSERT(pAddr);
if( NULL == pAddr->pChanPara )
return -1;
if(FALSE == UnmapViewOfFile(pAddr->pChanPara))
return -1;
CloseHandle(pAddr->hChanparaMem);
pAddr->pChanPara = NULL;
return 0;
}
int SaveChan(ADDR *pAddr,CString sProjPath)
{
ClearIniFile(sProjPath+_T("chanpara.ini"));
ASSERT(pAddr);
if(NULL == pAddr->pChanPara)
return -1;
if(pAddr->pChanPara->devcnum >32) //设备数必须小于32
return -1;
CHANNELPARA * pChanParaAddr = pAddr->pChanPara;
char chIniKeyName[32];
char channum[8];
char chandescr[40];
char chantype[8];
char chancommpara[64];
char chanprotname[32];
char chaniszf[8];
char chandevcnum[8];
char chanvalidf[8];
char devcpara[256];
itoa(pAddr->pSysPara->channum,channum,10);
::WritePrivateProfileString(_T("CHANNEL"),_T("channum"),channum,sProjPath+_T("chanpara.ini"));
//写通道参数
for(int nChanNum=0; nChanNum<pAddr->pSysPara->channum; nChanNum++)
{
ASSERT(pChanParaAddr);
strcpy(chandescr, pChanParaAddr->descrb);
itoa(pChanParaAddr->chantype,chantype,10);
strcpy(chancommpara,pChanParaAddr->commpara);
strcpy(chanprotname,pChanParaAddr->protoname);
itoa(pChanParaAddr->iszf,chaniszf,10);
itoa(pChanParaAddr->devcnum,chandevcnum,10);
itoa(pChanParaAddr->validf,chanvalidf,10);
sprintf(chIniKeyName,"chan%d",nChanNum);
sprintf(devcpara,_T("%s;%s;%s;%s;%s;%s;%s;"),chandescr,chantype,chancommpara,
chanprotname,chaniszf,chandevcnum,chanvalidf);
::WritePrivateProfileString(_T("CHANSTYLE"),chIniKeyName,devcpara,sProjPath+_T("chanpara.ini"));
pChanParaAddr++;
}
pChanParaAddr = pAddr->pChanPara;
//写通道下设备序号
CString strDevcNo,str;
for(nChanNum=0; nChanNum<pAddr->pSysPara->channum; nChanNum++)
{
for(int nDevcNum=0; nDevcNum<pChanParaAddr->devcnum; nDevcNum++)
{
str.Format(_T("%d;"),pChanParaAddr->devcs[nDevcNum]);
strDevcNo+=str;
}
sprintf(chIniKeyName,"chandevc%d",nChanNum);
::WritePrivateProfileString(_T("CHANDEVCNO"),chIniKeyName,strDevcNo,sProjPath+_T("chanpara.ini"));
pChanParaAddr++;
strDevcNo = "";
}
return 0;
}
int LoadChan(ADDR *pAddr,CString sProjPath)
{
ASSERT(pAddr);
if(NULL == pAddr->pChanPara)
return -1;
char strKeyVal[256];
c