| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934 |
- // SecUPS.cpp: implementation of the CSecUPS class.
- ////////////////////////////////////////////////////////////////////////////////
- ////// //////
- ////// 文 件: secups.cpp //////
- ////// 作 者: wangjianfeng //////
- ////// 创建时间: //////
- ////// 说 明: sec协议 //////
- ////// //////
- ////// 修改时间:2010-06-26 //////
- ////// 修改说明:新版本 //////
- ////// //////
- //////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "CommProcess.h"
- #include "Global.h"
- #include "SecUPS.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CSecUPS::CSecUPS( char szPath[MAX_PATH], // 程序所在路径
- char szIniName[MAX_PATH], // 配置文件名称
- int nCommPort, // 串行端口
- int nAddr, // 设备地址
- int nRate, // 波特率
- int nDataBit, // 数据位
- int nStopBit, // 停止位
- int nParity, // 校验位
- int nInterval // 时间间隔
- )
- {
- #if IS_USE_READMSG_CS
- InitializeCriticalSection( &m_csReadMsg ); //初始化一个临界资源对象
- #endif
- MTVERIFY( m_hSemComm = CreateEvent( NULL, TRUE, TRUE, 0 ) ); //CreateEvent()创建或打开一个命名的或无名的事件对象
-
- memset(m_szSEC_ST1Msg, 0, sizeof(m_szSEC_ST1Msg));
- memset(m_szSEC_ST2Msg, 0, sizeof(m_szSEC_ST2Msg));
- memset(m_szSEC_ST3Msg, 0, sizeof(m_szSEC_ST3Msg));
- memset(m_szSEC_ST4Msg, 0, sizeof(m_szSEC_ST4Msg));
- memset(m_szSEC_ST5Msg, 0, sizeof(m_szSEC_ST5Msg));
- memset(m_szSEC_NOMMsg, 0, sizeof(m_szSEC_NOMMsg));
- memset(m_szSEC_ATRMsg, 0, sizeof(m_szSEC_ATRMsg));
- }
- CSecUPS::~CSecUPS()
- {
- #if IS_USE_READMSG_CS
- DeleteCriticalSection( &m_csReadMsg );
- #endif
- MTVERIFY( CloseHandle( m_hSemComm ) );
- CloseComm();
- }
- BOOL CSecUPS::UpsSECOpenComm(int nCommPort, int nAddr, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval)
- {
- BOOL bResult = FALSE;
- bResult = OpenComm( nCommPort, nAddr, nRate, nDataBit, nStopBit, nParity, nInterval );
- return bResult;
- }
- int CSecUPS::SendReadRequest(
- char szPath[MAX_PATH],
- char szIniName[MAX_PATH],
- int nCommPort,
- int nAddr,
- char szCmd[MAX_CMD],
- char szRecvMsg[VAR_MSG],
- int nReversed1,
- int nReversed2,
- int nReversed3,
- int nReversed4,
- int nReversed5,
- float fReversed1,
- float fReversed2,
- float fReversed3,
- char szReversed1[MAX_RESERVED1],
- char szReversed2[MAX_RESERVED2],
- char szReversed3[MAX_RESERVED3],
- char szReversed4[MAX_RESERVED4],
- char szReversed5[MAX_RESERVED5]
- )
- {
- int nIndex = 0;
- char szSendMsg[SEC_SEND_MSG] = {0};
- GetSECFromIni(szPath, szIniName, szCmd,szSendMsg, nIndex);
- CCommProcess *pComm = FindComm(nCommPort);
- int nRet = -1;
- if (
- (strlen(m_szSEC_ST1Msg) == 0 &&
- (
- strcmp(szCmd, "cmd-1")==0||
- strcmp(szCmd, "cmd-2")==0||
- strcmp(szCmd, "cmd-3")==0||
- strcmp(szCmd, "cmd-4")==0||
- strcmp(szCmd, "cmd-5")==0||
- strcmp(szCmd, "cmd-6")==0||
- strcmp(szCmd, "cmd-7")==0||
- strcmp(szCmd, "cmd-8")==0
- )) ||
- (strlen(m_szSEC_ST2Msg) == 0 &&
- (strcmp(szCmd, "cmd-9")==0||
- strcmp(szCmd, "cmd-10")==0||
- strcmp(szCmd, "cmd-11")==0||
- strcmp(szCmd, "cmd-12")==0||
- strcmp(szCmd, "cmd-13")==0||
- strcmp(szCmd, "cmd-14")==0||
- strcmp(szCmd, "cmd-15")==0||
- strcmp(szCmd, "cmd-16")==0||
- strcmp(szCmd, "cmd-17")==0||
- strcmp(szCmd, "cmd-18")==0||
- strcmp(szCmd, "cmd-19")==0||
- strcmp(szCmd, "cmd-20")==0||
- strcmp(szCmd, "cmd-21")==0||
- strcmp(szCmd, "cmd-22")==0
- )) ||
- (strlen(m_szSEC_ST3Msg) == 0 &&
- (strcmp(szCmd, "cmd-23")==0||
- strcmp(szCmd, "cmd-24")==0||
- strcmp(szCmd, "cmd-25")==0||
- strcmp(szCmd, "cmd-26")==0||
- strcmp(szCmd, "cmd-27")==0||
- strcmp(szCmd, "cmd-28")==0||
- strcmp(szCmd, "cmd-29")==0||
- strcmp(szCmd, "cmd-30")==0||
- strcmp(szCmd, "cmd-31")==0||
- strcmp(szCmd, "cmd-32")==0||
- strcmp(szCmd, "cmd-33")==0||
- strcmp(szCmd, "cmd-34")==0||
- strcmp(szCmd, "cmd-35")==0||
- strcmp(szCmd, "cmd-36")==0||
- strcmp(szCmd, "cmd-37")==0
- )) ||
- (strlen(m_szSEC_ST4Msg) == 0 &&
- (strcmp(szCmd, "cmd-38")==0||
- strcmp(szCmd, "cmd-39")==0||
- strcmp(szCmd, "cmd-40")==0||
- strcmp(szCmd, "cmd-41")==0||
- strcmp(szCmd, "cmd-42")==0||
- strcmp(szCmd, "cmd-43")==0||
- strcmp(szCmd, "cmd-44")==0||
- strcmp(szCmd, "cmd-45")==0||
- strcmp(szCmd, "cmd-46")==0||
- strcmp(szCmd, "cmd-47")==0||
- strcmp(szCmd, "cmd-48")==0
- )) ||
- (strlen(m_szSEC_ST5Msg) == 0 &&
- (strcmp(szCmd, "cmd-49")==0||
- strcmp(szCmd, "cmd-50")==0||
- strcmp(szCmd, "cmd-51")==0||
- strcmp(szCmd, "cmd-52")==0||
- strcmp(szCmd, "cmd-53")==0||
- strcmp(szCmd, "cmd-54")==0||
- strcmp(szCmd, "cmd-55")==0||
- strcmp(szCmd, "cmd-56")==0||
- strcmp(szCmd, "cmd-57")==0||
- strcmp(szCmd, "cmd-58")==0||
- strcmp(szCmd, "cmd-59")==0||
- strcmp(szCmd, "cmd-60")==0||
- strcmp(szCmd, "cmd-61")==0||
- strcmp(szCmd, "cmd-62")==0||
- strcmp(szCmd, "cmd-63")==0
- )) ||
- (strlen(m_szSEC_NOMMsg) == 0 &&
- (strcmp(szCmd, "cmd-64")==0||
- strcmp(szCmd, "cmd-65")==0||
- strcmp(szCmd, "cmd-66")==0||
- strcmp(szCmd, "cmd-67")==0||
- strcmp(szCmd, "cmd-68")==0||
- strcmp(szCmd, "cmd-69")==0||
- strcmp(szCmd, "cmd-70")==0||
- strcmp(szCmd, "cmd-71")==0||
- strcmp(szCmd, "cmd-72")==0
- )) ||
- (strlen(m_szSEC_ATRMsg) == 0 &&
- (strcmp(szCmd, "cmd-73")==0))
- ||
- strcmp(szCmd, "cmd-1") == 0 ||
- strcmp(szCmd, "cmd-9") == 0 ||
- strcmp(szCmd, "cmd-23") == 0 ||
- strcmp(szCmd, "cmd-38") == 0 ||
- strcmp(szCmd, "cmd-49") == 0 ||
- strcmp(szCmd, "cmd-64") == 0 ||
- strcmp(szCmd, "cmd-73") == 0
- )
- {
- nRet = GetDeviceParam( pComm, szSendMsg, szCmd, szRecvMsg,nIndex );
- if (nRet != 0)
- {
- return nRet;
- }
- }
- nRet = GetSECST1VarMsg(szCmd, szRecvMsg, nIndex);
- nRet = GetSECST2VarMsg(szCmd, szRecvMsg, nIndex);
- nRet = GetSECST3VarMsg(szCmd, szRecvMsg, nIndex);
- nRet = GetSECST4VarMsg(szCmd, szRecvMsg, nIndex);
- nRet = GetSECST5VarMsg(szCmd, szRecvMsg, nIndex);
- nRet = GetSECNOMVarMsg(szCmd, szRecvMsg, nIndex);
- nRet = GetSECATRVarMsg(szCmd, szRecvMsg, nIndex);
- return nRet;
- }
- int CSecUPS::GetDeviceParam(
- CCommProcess *pComm, //串口对象指针
- char szSendMsg[SEC_SEND_MSG], //发送Buffer
- char szCmd[MAX_CMD], // 命令
- char szRecvMsg[VAR_MSG], // 接收Buffer
- int &nIndex) // 变量数据类型
- {
- int nRet = -1;
- nRet = RequestReadStatus(pComm, szSendMsg);
- if( nRet != 0 )
- {
- return nRet; // 串口忙
- }
- nRet = ResponseReadStatus(pComm, szCmd, szRecvMsg, nIndex);
- return nRet;
- }
- void CSecUPS::ReturnData(char *szSource, char *szDestion, int nIndex)
- {
- char nlen[3] = {0};
- nlen[0] = szSource[2];
- nlen[1] = szSource[3];
- nlen[2] = szSource[4];
- int len = atoi(nlen);
- BOOL bSi = FALSE;
- int i = 0;
- int index = 0;
- if (szSource[5] == 0x2c)
- {
- index = 1;
- }
- while(i < len)
- {
- if (index == nIndex)
- {
- int datalen = 0;
- for (int k = 0;i < len ; k++)
- {
- if (szSource[i+5] != 0x2C)
- szDestion[k] = szSource[i+5];
- else
- break;
- i++;
- }
- break;
- }
- if (szSource[i+5] == 0x2C)
- {
- int j = 0;
- for (;i<len;i++)
- {
- if (szSource[i+5] != 0x2C)
- break;
- j++;
- if (j > 1)
- index += 1 ;
- if (index == nIndex)
- break;
- }
- }
- if (index == nIndex)
- {
- int datalen = 0;
- for (int k = 0;i < len ; k++)
- {
- if (szSource[i+5] != 0x2C)
- szDestion[k] = szSource[i+5];
- else
- break;
- i++;
- }
- break;
- }
- if (szSource[i+5] != 0x2C)
- {
- index++;
- if (index == nIndex)
- continue;
- for (;i < len ;i++)
- if (szSource[i+5] == 0x2C)
- {
- --i;
- bSi = FALSE;
- break;
- }
- }
- i++;
- }
- }
- int CSecUPS::GetSECST1VarMsg(char szCmd[MAX_CMD], char szRecvMsg[VAR_MSG], int &nIndex)
- {
- int nRet =0;
- if( strcmp(szCmd, "cmd-1")==0||
- strcmp(szCmd, "cmd-2")==0||
- strcmp(szCmd, "cmd-3")==0||
- strcmp(szCmd, "cmd-4")==0||
- strcmp(szCmd, "cmd-5")==0||
- strcmp(szCmd, "cmd-6")==0||
- strcmp(szCmd, "cmd-7")==0||
- strcmp(szCmd, "cmd-8")==0
- )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- ReturnData(m_szSEC_ST1Msg, szRecvMsg, nIndex);
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- int CSecUPS::GetSECST2VarMsg(char szCmd[MAX_CMD], char szRecvMsg[VAR_MSG], int &nIndex)
- {
- int nRet = 0;
- if(
- strcmp(szCmd, "cmd-9")==0||
- strcmp(szCmd, "cmd-10")==0||
- strcmp(szCmd, "cmd-11")==0||
- strcmp(szCmd, "cmd-12")==0||
- strcmp(szCmd, "cmd-13")==0||
- strcmp(szCmd, "cmd-14")==0||
- strcmp(szCmd, "cmd-15")==0||
- strcmp(szCmd, "cmd-16")==0||
- strcmp(szCmd, "cmd-17")==0||
- strcmp(szCmd, "cmd-18")==0||
- strcmp(szCmd, "cmd-19")==0||
- strcmp(szCmd, "cmd-20")==0||
- strcmp(szCmd, "cmd-21")==0||
- strcmp(szCmd, "cmd-22")==0 )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- ReturnData(m_szSEC_ST2Msg, szRecvMsg, nIndex);
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- int CSecUPS::GetSECST3VarMsg(char szCmd[MAX_CMD], char szRecvMsg[VAR_MSG], int &nIndex)
- {
- int nRet = 0;
- if(
- strcmp(szCmd, "cmd-23")==0||
- strcmp(szCmd, "cmd-24")==0||
- strcmp(szCmd, "cmd-25")==0||
- strcmp(szCmd, "cmd-26")==0||
- strcmp(szCmd, "cmd-27")==0||
- strcmp(szCmd, "cmd-28")==0||
- strcmp(szCmd, "cmd-29")==0||
- strcmp(szCmd, "cmd-30")==0||
- strcmp(szCmd, "cmd-31")==0||
- strcmp(szCmd, "cmd-32")==0||
- strcmp(szCmd, "cmd-33")==0||
- strcmp(szCmd, "cmd-34")==0||
- strcmp(szCmd, "cmd-35")==0||
- strcmp(szCmd, "cmd-36")==0||
- strcmp(szCmd, "cmd-37")==0)
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- ReturnData(m_szSEC_ST3Msg, szRecvMsg, nIndex);
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- int CSecUPS::GetSECST4VarMsg(char szCmd[MAX_CMD], char szRecvMsg[VAR_MSG], int &nIndex)
- {
- int nRet = 0;
- if(
- strcmp(szCmd, "cmd-38")==0||
- strcmp(szCmd, "cmd-39")==0||
- strcmp(szCmd, "cmd-40")==0||
- strcmp(szCmd, "cmd-41")==0||
- strcmp(szCmd, "cmd-42")==0||
- strcmp(szCmd, "cmd-43")==0||
- strcmp(szCmd, "cmd-44")==0||
- strcmp(szCmd, "cmd-45")==0||
- strcmp(szCmd, "cmd-46")==0||
- strcmp(szCmd, "cmd-47")==0||
- strcmp(szCmd, "cmd-48")==0
- )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- ReturnData(m_szSEC_ST4Msg, szRecvMsg, nIndex);
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- int CSecUPS::GetSECST5VarMsg(char szCmd[MAX_CMD], char szRecvMsg[VAR_MSG], int &nIndex)
- {
- int nRet = 0;
- if( strcmp(szCmd, "cmd-49")==0||
- strcmp(szCmd, "cmd-50")==0||
- strcmp(szCmd, "cmd-51")==0||
- strcmp(szCmd, "cmd-52")==0||
- strcmp(szCmd, "cmd-53")==0||
- strcmp(szCmd, "cmd-54")==0||
- strcmp(szCmd, "cmd-55")==0||
- strcmp(szCmd, "cmd-56")==0||
- strcmp(szCmd, "cmd-57")==0||
- strcmp(szCmd, "cmd-58")==0||
- strcmp(szCmd, "cmd-59")==0||
- strcmp(szCmd, "cmd-60")==0||
- strcmp(szCmd, "cmd-61")==0||
- strcmp(szCmd, "cmd-62")==0||
- strcmp(szCmd, "cmd-63")==0
- )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- ReturnData(m_szSEC_ST5Msg, szRecvMsg, nIndex);
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- int CSecUPS::GetSECNOMVarMsg(char szCmd[MAX_CMD], char szRecvMsg[VAR_MSG], int &nIndex)
- {
- int nRet = 0;
- if(strcmp(szCmd, "cmd-64")==0||
- strcmp(szCmd, "cmd-65")==0||
- strcmp(szCmd, "cmd-66")==0||
- strcmp(szCmd, "cmd-67")==0||
- strcmp(szCmd, "cmd-68")==0||
- strcmp(szCmd, "cmd-69")==0||
- strcmp(szCmd, "cmd-70")==0||
- strcmp(szCmd, "cmd-71")==0||
- strcmp(szCmd, "cmd-72")==0
- )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- ReturnData(m_szSEC_NOMMsg, szRecvMsg, nIndex);
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- int CSecUPS::GetSECATRVarMsg(char szCmd[MAX_CMD], char szRecvMsg[VAR_MSG], int &nIndex)
- {
- int nRet = 0;
- if(strcmp(szCmd, "cmd-73")==0 )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- ReturnData(m_szSEC_ATRMsg, szRecvMsg, nIndex);
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- // 发送设置设备参数请求
- int CSecUPS::SendSetReuest(
- char szPath[MAX_PATH], // 程序所在路径
- char szIniName[MAX_PATH], // 配置文件名称
- int nCommPort, // 串行端口
- int nAddr, // 设备地址
- char szCmd[MAX_CMD], // 请求命令
- char szRecvMsg[VAR_MSG], // 响应的值
- int nReversed1, // 预留整形参数1接口
- int nReversed2, // 预留整形参数2接口
- int nReversed3, // 预留整形参数3接口
- int nReversed4, // 预留整形参数4接口
- int nReversed5, // 预留整形参数5接口
- float fReversed1, // 预留float参数1接口
- float fReversed2, // 预留float参数2接口
- float fReversed3, // 预留float参数3接口
- char szReversed1[MAX_RESERVED1], // 预留字符数组参数1接口
- char szReversed2[MAX_RESERVED2], // 预留字符数组参数2接口
- char szReversed3[MAX_RESERVED3], // 预留字符数组参数3接口
- char szReversed4[MAX_RESERVED4], // 预留字符数组参数4接口
- char szReversed5[MAX_RESERVED5] // 预留字符数组参数5接口
- )
- {
- int nIndex = 0;
- char szSendMsg[SEC_SEND_MSG] = {0};
- GetSECFromIni(szPath, szIniName, szCmd,szSendMsg, nIndex);
- CCommProcess *pComm = FindComm(nCommPort);
- int nRet = -1;
- //if( (strlen(m_szSEC_ATRMsg) == 0 && strcmp(szCmd, "cmd-73")==0) || (strlen(m_szSEC_NOMMsg) == 0 &&(strcmp(szCmd, "cmd-72")) ))
- //{
- // nRet = RequestWriteStatus(pComm, szSendMsg);
- // if( nRet != 0 )
- // {
- // return nRet; // 串口忙
- // }
- // nRet = ResponseWriteStatus(pComm, szCmd, szRecvMsg, nIndex);
- //}
- return nRet;
- }
- //int CSecUPS::RequestWriteStatus( CCommProcess *pComm,char szSendMsg[SEC_SEND_MSG] )
- //{
- //#if DEBUG_SEC
- //
- // int iLen = sizeof(POTOCOLPARAM_STRUCT);
- // char chLength[4] = {0};
- // char chChkSum[5] = {0};
- //
- // POTOCOLPARAM_STRUCT RequestPara;
- //
- // memset( &RequestPara, 0, iLen );
- //
- // RequestPara.SendHead = 0x5E;
- //
- // RequestPara.SendType = 0x53;
- // RequestPara.SendLength[0] = 0x30;
- // RequestPara.SendLength[1] = 0x30;
- // RequestPara.SendLength[2] = 0x33;
- //
- // memcpy(RequestPara.SendData, szSendMsg, 3);
- //
- // //WaitForSingObject();等待有信号;
- // if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口
- // {
- // //求得数组字符实际长度;
- // int nDataLen = (int)strlen(szSendMsg);
- //
- // //ResetEvent()设置无信号状态
- // ResetEvent( m_hSemComm );
- //
- // int nResult = pComm->Write((unsigned char *)&RequestPara, nDataLen);
- // if( nResult == nDataLen )
- // {
- // }
- // else
- // {
- // //SetEvent设置有信号;
- // SetEvent( m_hSemComm );
- //
- // //返回 错误值;
- // return EER_CODE_COM_REGNUM;
- // }
- // }
- // else
- // {
- // //如果m_hSemCom无信号,返回错误;
- // return ERR_CODE_COM_BUSY;
- // }
- //#endif
- // return 0;
- //}
- //
- //int CSecUPS::ResponseWriteStatus( CCommProcess *pComm,char szCmd[MAX_CMD], char szRecvMsg[SEC_SEND_MSG],int &nIndex )
- //{
- // return 0;
- //}
- int CSecUPS::ResponseReadStatus(
- CCommProcess *pComm,
- char szCmd[MAX_CMD],
- char szRecvMsg[VAR_MSG],
- int &nIndex)
- {
- #if DEBUG_SEC
- //这个整型没用到;
- int nProcessLen = 0;
- POTOCOLPARAM_STRUCT structResponse;
- memset( &structResponse, 0, sizeof(POTOCOLPARAM_STRUCT) );
- int nReceiveLen = 0;
- nReceiveLen = sizeof(POTOCOLPARAM_STRUCT);
- char *pBuffer = new char[ nReceiveLen ];
- memset(pBuffer, 0, nReceiveLen);
- int nReadLen = 0;
- nReadLen = pComm->Read((BYTE *)pBuffer, nReceiveLen);//pBuffer整串协议 数据
- if( nReadLen <= 0)
- {
- // 串口没有读到数据
- TRACE("串口没有读到数据!\r\n");
- SetEvent( m_hSemComm );
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- return ERR_CODE_COM_READ_NO_DATA;
- }
-
- SetSECST1VarMsg( szCmd, pBuffer);
- SetSECST2VarMsg( szCmd, pBuffer);
- SetSECST3VarMsg( szCmd, pBuffer);
- SetSECST4VarMsg( szCmd, pBuffer);
- SetSECST5VarMsg( szCmd, pBuffer);
- SetSECNOMVarMsg( szCmd, pBuffer);
- SetSECATRVarMsg( szCmd, pBuffer);
- SetEvent( m_hSemComm );
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- #else
- SimulationCommData();
- #endif
- return 0;
- }
- int CSecUPS::RequestReadStatus(CCommProcess *pComm,char szSendMsg[SEC_SEND_MSG])
- {
- #if DEBUG_SEC
- int iLen = sizeof(POTOCOLPARAM_STRUCT);
- char chLength[4] = {0};
- char chChkSum[5] = {0};
- POTOCOLPARAM_STRUCT RequestPara;
- memset( &RequestPara, 0, iLen );
- RequestPara.SendHead = 0x5E;
- RequestPara.SendType = 0x50;
- RequestPara.SendLength[0] = 0x30;
- RequestPara.SendLength[1] = 0x30;
- RequestPara.SendLength[2] = 0x33;
- memcpy(RequestPara.SendData, szSendMsg, 3);
- //WaitForSingObject();等待有信号;
- if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口
- {
- //求得数组字符实际长度;
- int nDataLen = (int)strlen(szSendMsg);
- //ResetEvent()设置无信号状态
- ResetEvent( m_hSemComm );
- int nResult = pComm->Write((unsigned char *)&RequestPara, nDataLen);
- if( nResult == nDataLen )
- {
- }
- else
- {
- //SetEvent设置有信号;
- SetEvent( m_hSemComm );
- //返回 错误值;
- return EER_CODE_COM_REGNUM;
- }
- }
- else
- {
- //如果m_hSemCom无信号,返回错误;
- return ERR_CODE_COM_BUSY;
- }
- #endif
- return 0;
- }
- void CSecUPS::SetSECST1VarMsg(char szCmd[MAX_CMD], char *pBuff)
- {
- if( strcmp(szCmd, "cmd-1") == 0 ||
- strcmp(szCmd, "cmd-2") == 0 ||
- strcmp(szCmd, "cmd-3") == 0 ||
- strcmp(szCmd, "cmd-4") == 0 ||
- strcmp(szCmd, "cmd-5") == 0 ||
- strcmp(szCmd, "cmd-6") == 0 ||
- strcmp(szCmd, "cmd-7") == 0 ||
- strcmp(szCmd, "cmd-8") == 0
- )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- memcpy(m_szSEC_ST1Msg, pBuff, sizeof(m_szSEC_ST1Msg));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- void CSecUPS::SetSECST2VarMsg(char szCmd[MAX_CMD], char *pBuff)
- {
- if(
- strcmp(szCmd, "cmd-9")==0||
- strcmp(szCmd, "cmd-10")==0||
- strcmp(szCmd, "cmd-11")==0||
- strcmp(szCmd, "cmd-12")==0||
- strcmp(szCmd, "cmd-13")==0||
- strcmp(szCmd, "cmd-14")==0||
- strcmp(szCmd, "cmd-15")==0||
- strcmp(szCmd, "cmd-16")==0||
- strcmp(szCmd, "cmd-17")==0||
- strcmp(szCmd, "cmd-18")==0||
- strcmp(szCmd, "cmd-19")==0||
- strcmp(szCmd, "cmd-20")==0||
- strcmp(szCmd, "cmd-21")==0||
- strcmp(szCmd, "cmd-22")==0
- )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- memcpy(m_szSEC_ST2Msg, pBuff, sizeof(m_szSEC_ST2Msg));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- void CSecUPS::SetSECST3VarMsg(char szCmd[MAX_CMD], char *pBuff)
- {
- if( strcmp(szCmd, "cmd-23")==0||
- strcmp(szCmd, "cmd-24")==0||
- strcmp(szCmd, "cmd-25")==0||
- strcmp(szCmd, "cmd-26")==0||
- strcmp(szCmd, "cmd-27")==0||
- strcmp(szCmd, "cmd-28")==0||
- strcmp(szCmd, "cmd-29")==0||
- strcmp(szCmd, "cmd-30")==0||
- strcmp(szCmd, "cmd-31")==0||
- strcmp(szCmd, "cmd-32")==0||
- strcmp(szCmd, "cmd-33")==0||
- strcmp(szCmd, "cmd-34")==0||
- strcmp(szCmd, "cmd-35")==0||
- strcmp(szCmd, "cmd-36")==0||
- strcmp(szCmd, "cmd-37")==0
- )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- memcpy(m_szSEC_ST3Msg, pBuff, sizeof(m_szSEC_ST3Msg));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- void CSecUPS::SetSECST4VarMsg(char szCmd[MAX_CMD], char *pBuff)
- {
- if( strcmp(szCmd, "cmd-38")==0||
- strcmp(szCmd, "cmd-39")==0||
- strcmp(szCmd, "cmd-40")==0||
- strcmp(szCmd, "cmd-41")==0||
- strcmp(szCmd, "cmd-42")==0||
- strcmp(szCmd, "cmd-43")==0||
- strcmp(szCmd, "cmd-44")==0||
- strcmp(szCmd, "cmd-45")==0||
- strcmp(szCmd, "cmd-46")==0||
- strcmp(szCmd, "cmd-47")==0||
- strcmp(szCmd, "cmd-48")==0
- )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- memcpy(m_szSEC_ST4Msg, pBuff, sizeof(m_szSEC_ST4Msg));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- void CSecUPS::SetSECST5VarMsg(char szCmd[MAX_CMD], char *pBuff)
- {
- if(strcmp(szCmd, "cmd-49")==0||
- strcmp(szCmd, "cmd-50")==0||
- strcmp(szCmd, "cmd-51")==0||
- strcmp(szCmd, "cmd-52")==0||
- strcmp(szCmd, "cmd-53")==0||
- strcmp(szCmd, "cmd-54")==0||
- strcmp(szCmd, "cmd-55")==0||
- strcmp(szCmd, "cmd-56")==0||
- strcmp(szCmd, "cmd-57")==0||
- strcmp(szCmd, "cmd-58")==0||
- strcmp(szCmd, "cmd-59")==0||
- strcmp(szCmd, "cmd-60")==0||
- strcmp(szCmd, "cmd-61")==0||
- strcmp(szCmd, "cmd-62")==0||
- strcmp(szCmd, "cmd-63")==0
- )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- memcpy(m_szSEC_ST5Msg, pBuff, sizeof(m_szSEC_ST5Msg));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- void CSecUPS::SetSECNOMVarMsg(char szCmd[MAX_CMD], char *pBuff)
- {
- if( strcmp(szCmd, "cmd-64")==0||
- strcmp(szCmd, "cmd-65")==0||
- strcmp(szCmd, "cmd-66")==0||
- strcmp(szCmd, "cmd-67")==0||
- strcmp(szCmd, "cmd-68")==0||
- strcmp(szCmd, "cmd-69")==0||
- strcmp(szCmd, "cmd-70")==0||
- strcmp(szCmd, "cmd-71")==0||
- strcmp(szCmd, "cmd-72")==0
- )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- memcpy(m_szSEC_NOMMsg, pBuff, sizeof(m_szSEC_NOMMsg));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- void CSecUPS::SetSECATRVarMsg(char szCmd[MAX_CMD], char *pBuff)
- {
- if( strcmp(szCmd, "cmd-73")==0 )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- memcpy(m_szSEC_ATRMsg, pBuff, sizeof(m_szSEC_ATRMsg));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- int CSecUPS::GetSECFromIni(
- char szPath[MAX_PATH], //服务器程序所在目录
- char szIniName[MAX_PATH], //配置文件名
- char szCmd[MAX_CMD], //命令
- char szSendMsg[MAX_CMD], //发送Buffer
- int &nIndex
- )
- {
- CHAR szFile[MAX_PATH + 1] = "";
- wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
- GetPrivateProfileString(szCmd, "SenCmd", "", szSendMsg, 10, szFile);
- szSendMsg[strlen(szSendMsg)] = '\0';
- nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile);
- return 0;
- }
- void CSecUPS::SimulationCommData(void)
- {
- strcpy(m_szSEC_ST1Msg,"^D015,0,,,10,90,,,35");
- strcpy(m_szSEC_ST2Msg,"^D007,,,1180");
- strcpy(m_szSEC_ST3Msg,"^D007,,,1200");
- strcpy(m_szSEC_ST4Msg,"");
- strcpy(m_szSEC_ST5Msg,"^D013,,,1,,,,,,,,0");
- strcpy(m_szSEC_NOMMsg,"");
- strcpy(m_szSEC_ATRMsg,"");
- }
|