| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- #include "StdAfx.h"
- #include ".\st_3c3ex40ks.h"
- CST_3C3Ex40ks::CST_3C3Ex40ks(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_szQ2Msg, 0, sizeof(m_szQ2Msg));
- memset(m_szWAMsg, 0, sizeof(m_szWAMsg));
- m_dwOnlineTick = 0;
- m_devOnline = TRUE;
- }
- CST_3C3Ex40ks::~CST_3C3Ex40ks(void)
- {
- #if IS_USE_READMSG_CS
- DeleteCriticalSection(& m_csReadMsg);
- #endif
- MTVERIFY(CloseHandle(m_hSemComm));
- CloseComm();
- }
- BOOL CST_3C3Ex40ks::SanTakOpenComm(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 CST_3C3Ex40ks::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_szQ2Msg) == 0 && (iCmd >= 1 && iCmd <= 23)) || iCmd == 1 ||
- (strlen(m_szWAMsg) == 0 && (iCmd >= 24 && iCmd <= 35)) || iCmd == 24 )
- {
- nRet = GetDeviceParam(pComm, IniSendCMD,IniSendlen, szCmd );
- if( nRet != 0 )
- return nRet;
- }
- /*if(
- ( strlen(m_szQ2Msg) == 0 && (
- strcmp(szCmd,"cmd-1") == 0 || strcmp(szCmd,"cmd-2") == 0 || strcmp(szCmd,"cmd-3") == 0 ||
- strcmp(szCmd,"cmd-4") == 0 || strcmp(szCmd,"cmd-6") == 0 || strcmp(szCmd,"cmd-5") == 0 ||
- strcmp(szCmd,"cmd-7") == 0 || strcmp(szCmd,"cmd-8") == 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 || strcmp(szCmd,"cmd-23") == 0
- ) ) ||
- (strlen(m_szWAMsg) == 0 &&
- (strcmp(szCmd,"cmd-34") == 0 || strcmp(szCmd,"cmd-35") == 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-1") == 0 || strcmp(szCmd, "cmd-22") == 0
- ){
- 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_Q2Msg(szCmd, szMsg, nIndex, nLen, szType );
- nRet = GetSanTak_WAMsg(szCmd, szMsg, nIndex, nLen, szType );
- LOG4C((LOG_NOTICE, "%s=%s", szCmd, szMsg));
- return nRet;
- }
- int CST_3C3Ex40ks::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 CST_3C3Ex40ks::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 CST_3C3Ex40ks::Send_ReadDeviceData(CCommProcess *pComm,const char *IniSendCMD,const int &IniSendlen )
- {
- #if DEBUG_ST_3C3EX40KS
- 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 CST_3C3Ex40ks::Recv_ReadDeviceData(CCommProcess *pComm, char *szCmd)
- {
- #if DEBUG_ST_3C3EX40KS
- 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;
- } //长度校验出错 ;
- SetSanTakQ2Msg(szCmd, pBuff);
- SetSanTakWAMsg(szCmd, pBuff);
- m_dwOnlineTick = GetTickCount();
- SetEvent( m_hSemComm );
- if( pBuff != NULL) {
- delete[] pBuff;
- pBuff = NULL;
- }
- #else
- SimulationCommData();
- #endif
- return 0;
- }
- void CST_3C3Ex40ks::SetSanTakQ2Msg(char *szCmd, char *pBuff)
- {
- int iCmd = atoi(szCmd + 4);
- if ( iCmd >= 1 && iCmd <= 23)
- /*if (
- strcmp(szCmd,"cmd-1") == 0 || strcmp(szCmd,"cmd-2") == 0 || strcmp(szCmd,"cmd-3") == 0 ||
- strcmp(szCmd,"cmd-4") == 0 || strcmp(szCmd,"cmd-6") == 0 || strcmp(szCmd,"cmd-5") == 0 ||
- strcmp(szCmd,"cmd-7") == 0 || strcmp(szCmd,"cmd-8") == 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 || strcmp(szCmd,"cmd-23") == 0
- )*/
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- memcpy(m_szQ2Msg, pBuff, sizeof(m_szQ2Msg));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- void CST_3C3Ex40ks::SetSanTakWAMsg(char *szCmd, char *pBuff)
- {
- int iCmd = atoi(szCmd + 4);
- if ( iCmd >= 24 && iCmd <= 35)
- /*if (
- strcmp(szCmd,"cmd-34") == 0 || strcmp(szCmd,"cmd-35") == 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
- )*/
- {
- #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 CST_3C3Ex40ks::GetSanTak_Q2Msg(char *szCmd, char *szMsg, int &nIndex, int &nLen, char *szType )
- {
- int nRet = 0;
- int iCmd = atoi(szCmd + 4);
- if ( iCmd >= 1 && iCmd <= 23)
- //if ( strcmp(szCmd,"cmd-1") == 0 || strcmp(szCmd,"cmd-2") == 0 || strcmp(szCmd,"cmd-3") == 0 ||
- // strcmp(szCmd,"cmd-4") == 0 || strcmp(szCmd,"cmd-6") == 0 || strcmp(szCmd,"cmd-5") == 0 ||
- // strcmp(szCmd,"cmd-7") == 0 || strcmp(szCmd,"cmd-8") == 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 || strcmp(szCmd,"cmd-23") == 0
- // )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- DataConversion(szType, m_szQ2Msg + nIndex, szMsg, nLen);
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- int CST_3C3Ex40ks::GetSanTak_WAMsg(char *szCmd, char *szMsg, int &nIndex, int &nLen, char *szType )
- {
- int nRet = 0;
- int iCmd = atoi(szCmd + 4);
- if ( iCmd >= 24 && iCmd <= 35)
- /*if (strcmp(szCmd,"cmd-34") == 0 || strcmp(szCmd,"cmd-35") == 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
- )*/
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- DataConversion(szType, m_szWAMsg + nIndex, szMsg, nLen);
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- void CST_3C3Ex40ks::SimulationCommData(void)
- {
- memcpy(m_szQ2Msg, "(333.6 333.2 333.9 140.0 555.1 555.2 555.3 001 002 003 50.0 419.2 20.0 00010000 999.00 100 00 00 00 00 91 00004000 11.", sizeof("(223.6 224.2 223.9 140.0 000.0 000.0 000.0 000 000 000 50.0 419.2 20.0 00010000 000.00 100 00 00 00 00 91 00004000 11."));
- 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."));
- }
|