| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- #include "StdAfx.h"
- #include ".\ST_C620KS_Q6.h"
- ST_C620KS_Q6::ST_C620KS_Q6(char *szPath,char *szIniName,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));
- memset(m_szQ6Msg, 0, sizeof(m_szQ6Msg));
- memset(m_szWAMsg, 0, sizeof(m_szWAMsg));
- m_dwOnlineTick = 0;
- m_devOnline = TRUE;
- }
- ST_C620KS_Q6::~ST_C620KS_Q6(void)
- {
- #if IS_USE_READMSG_CS
- DeleteCriticalSection(& m_csReadMsg);
- #endif
- MTVERIFY(CloseHandle(m_hSemComm));
- CloseComm();
- }
- BOOL ST_C620KS_Q6::Santak_Q6OpenComm(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 ST_C620KS_Q6::SendReadRequest( char *szPath,char *szIniName,int nCommPort,int nAddr,char *szCmd,char *szMsg,
- int nReversed1,int nReversed2,int nReversed3,int nReversed4,int nReversed5,
- float fReversed1,float fReversed2,float fReversed3,char *szReversed1,
- char *szReversed2,char *szReversed3,char *szReversed4,char *szReversed5
- )
- {
- int nIndex = 0, nLen = 0,IniSendlen = 0;
- char IniSendCMD[MAX_CMD] = {0};
- char szType[CMD_TYPE] = {0};
- GetIniInfo(szPath, szIniName, szCmd, IniSendCMD, IniSendlen ,szType, nIndex, nLen);
- CCommProcess *pComm = FindComm(nCommPort);
- if( pComm == NULL ) return -1;
- int nRet = -1;
- int iCmd = atoi(szCmd + 4);
- if( (strlen(m_szQ6Msg) == 0 && (iCmd >= 1 && iCmd <= 18)) || iCmd == 1 ||
- (strlen(m_szWAMsg) == 0 && (iCmd >= 24 && iCmd <= 43)) || iCmd == 24 )
- {
- nRet = GetDeviceParam(pComm, IniSendCMD,IniSendlen, szCmd );
- if( nRet != 0 )
- return nRet;
- }
- if( GetTickCount() - m_dwOnlineTick > 60 * 1000 && m_dwOnlineTick > 0 )
- m_devOnline = FALSE;
- else if( GetTickCount() - m_dwOnlineTick < 60 * 1000 && m_dwOnlineTick > 0 )
- m_devOnline = TRUE;
- if( m_devOnline == FALSE )
- return -1;
- nRet = GetSantak_Q6Msg(szCmd, szMsg, nIndex, nLen, szType );
- nRet = GetSanTak_WAMsg(szCmd, szMsg, nIndex, nLen, szType );
- //LOG4C((LOG_NOTICE, "%s=%s", szCmd, szMsg));
- return nRet;
- }
- int ST_C620KS_Q6::GetDeviceParam(CCommProcess *pComm,char *IniSendCMD,const int &IniSendlen ,char *szCmd)
- {
- int nRet = -1;
- nRet = Send_ReadDeviceData(pComm, IniSendCMD, IniSendlen);
- if( nRet != 0 ) {
- return nRet;
- }
- nRet = Recv_ReadDeviceData(pComm, szCmd);
- return nRet;
- }
- int ST_C620KS_Q6::GetIniInfo(char *szPath,char *szIniName,char *szCmd,char *IniSendCMD, int &IniSendlen, char *nType,int &nIndex,int &nLen)
- {
- CHAR szFile[MAX_PATH + 1] = "";
- wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
- TRACE("szFile =%s\n",szFile);
- IniSendlen = GetPrivateProfileString(szCmd, "SendCmd", "", IniSendCMD, 10, szFile); // 返回的字符串是以\0结束的;
- IniSendCMD[IniSendlen] = 0x0D;
- ++IniSendlen;
- GetPrivateProfileString(szCmd, "type", "", nType, 10, szFile);
- nType[strlen(nType)] = '\0';
- nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile);
- nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile);
- return 0;
- }
- int ST_C620KS_Q6::Send_ReadDeviceData(CCommProcess *pComm,const char *IniSendCMD,const int &IniSendlen )
- {
- #if DEBUG_Santak_Q6
- if (WaitForSingleObject(m_hSemComm,0) == WAIT_OBJECT_0 ) {
- ResetEvent(m_hSemComm);
- int nResult = pComm->Write((BYTE *)IniSendCMD, IniSendlen);
- if (nResult != IniSendlen){
- SetEvent(m_hSemComm);
- return EER_CODE_COM_REGNUM;
- }
- }
- else{
- return ERR_CODE_COM_BUSY;
- }
- #endif
- return 0;
- }
- int ST_C620KS_Q6::Recv_ReadDeviceData(CCommProcess *pComm, char *szCmd)
- {
- #if DEBUG_Santak_Q6
- int nProcessLen = 0;
- int nReadLen = 0;
- char *pBuff = new char[MAX_RECE_MSG];
- memset(pBuff, 0 , MAX_RECE_MSG);
- nReadLen = pComm->Read((BYTE *)pBuff,MAX_RECE_MSG);
- if (nReadLen <= 0){
- SetEvent(m_hSemComm);
- if (pBuff != NULL) {
- delete [] pBuff;
- pBuff = NULL;
- }
- return ERR_CODE_COM_READ_NO_DATA;
- } //长度校验出错 ;
- SetSanTakQ6Msg(szCmd, pBuff);
- SetSanTakWAMsg(szCmd, pBuff);
- m_dwOnlineTick = GetTickCount();
-
- SetEvent( m_hSemComm );
- if( pBuff != NULL) {
- delete[] pBuff;
- pBuff = NULL;
- }
- #else
- SimulationCommData();
- #endif
- return 0;
- }
- void ST_C620KS_Q6::SetSanTakQ6Msg(char *szCmd, char *pBuff)
- {
- int iCmd = atoi(szCmd + 4);
- if ( iCmd >= 1 && iCmd <= 18)
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- memcpy(m_szQ6Msg, pBuff, sizeof(m_szQ6Msg));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- void ST_C620KS_Q6::SetSanTakWAMsg(char *szCmd, char *pBuff)
- {
- int iCmd = atoi(szCmd + 4);
- if ( iCmd >= 24 && iCmd <= 43)
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- memcpy(m_szWAMsg, pBuff, sizeof(m_szWAMsg));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- int ST_C620KS_Q6::GetSantak_Q6Msg(char *szCmd, char *szMsg, int &nIndex, int &nLen, char *szType )
- {
- int nRet = 0;
- int iCmd = atoi(szCmd + 4);
- if ( iCmd >= 1 && iCmd <= 18)
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- memcpy(szMsg, m_szQ6Msg + nIndex, nLen);
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- int ST_C620KS_Q6::GetSanTak_WAMsg(char *szCmd, char *szMsg, int &nIndex, int &nLen, char *szType )
- {
- int nRet = 0;
- int iCmd = atoi(szCmd + 4);
- if ( iCmd >= 24 && iCmd <= 43)
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- memcpy(szMsg, m_szWAMsg + nIndex, nLen);
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- void ST_C620KS_Q6::SimulationCommData(void)
- {
- memcpy(m_szQ6Msg, "(MMM.M MMM.M MMM.M NN.N PPP.P PPP.P PPP.P RR.R QQQ QQQ QQQ SSS.S VVV.V TT.T ttttt CCC KB ffffffff wwwwwwww YO.",
- sizeof("(MMM.M MMM.M MMM.M NN.N PPP.P PPP.P PPP.P RR.R QQQ QQQ QQQ SSS.S VVV.V TT.T ttttt CCC KB ffffffff wwwwwwww YO."));
- memcpy(m_szWAMsg, "(111.4 111.8 111.0 222.4 222.8 222.0 444.8 444.0 001.4 001.8 001.5 019 00010000.",
- sizeof("(215.4 216.8 221.0 215.4 216.8 221.0 216.8 221.0 215.4 216.8 221.0 019 01111100."));
- }
|