| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- #include "StdAfx.h"
- #include ".\UPSLan.h"
- RS232Model_UPSLan::RS232Model_UPSLan(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_szRS232_RSMsg, 0, sizeof(m_szRS232_RSMsg));
- memset(m_szRS232_RDMsg, 0, sizeof(m_szRS232_RDMsg));
- m_dwOnlineTick = 0;
- m_devOnline = TRUE;
- }
- RS232Model_UPSLan::~RS232Model_UPSLan(void)
- {
- #if IS_USE_READMSG_CS
- DeleteCriticalSection(& m_csReadMsg);
- #endif
- MTVERIFY(CloseHandle(m_hSemComm));
- CloseComm();
- }
- BOOL RS232Model_UPSLan::RS232ModelOpenComm(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 RS232Model_UPSLan::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 )
- {
- CCommProcess *pComm = FindComm(nCommPort);
- if( pComm == NULL ) return -1;
- int nRet = -1;
- int nIndex(0), nLen(0), nIniSendlen(0),iSBit(0), iEBit(0);
- char szIniSendCMD[MAX_CMD] = {0}, szDataType[CMD_TYPE] = {0};
- GetIniInfo(szPath,szIniName,szCmd,szIniSendCMD,nIniSendlen,szDataType,nIndex,nLen,iSBit,iEBit);
- if( (strlen(m_szRS232_RSMsg) == 0 && strcmp(szIniSendCMD,"RS") ==0 ) || strcmp(szCmd,"cmd-1") ==0 ||
- (strlen(m_szRS232_RDMsg) == 0 && strcmp(szIniSendCMD,"RD") ==0 ) || strcmp(szCmd,"cmd-6") ==0 )
- {
- nRet = GetDeviceParam(pComm, szIniSendCMD,nIniSendlen, 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 = GetRS232Model_RSMsg(szIniSendCMD, szCmd, szMsg, nIndex, nLen, szDataType );
- nRet = GetRS232Model_RDMsg(szIniSendCMD, szCmd, szMsg, nIndex, nLen, szDataType );
- //LOG4C((LOG_NOTICE, "%s=%s", szCmd, szMsg));
- return nRet;
- }
- int RS232Model_UPSLan::GetDeviceParam(CCommProcess *pComm,char *szIniSendCMD,const int &nIniSendlen ,char *szCmd)
- {
- int nRet = -1;
- nRet = Send_ReadDeviceData(pComm, szIniSendCMD, nIniSendlen);
- if( nRet != 0 )
- return nRet;
- nRet = Recv_ReadDeviceData(pComm, szIniSendCMD, szCmd);
- return nRet;
- }
- int RS232Model_UPSLan::Send_ReadDeviceData(CCommProcess *pComm,const char *szIniSendCMD,const int &nIniSendlen )
- {
- #if DEBGU_RS232MODEL_UPSLAN
- /************************************************************************/
- /* do something you want */
- /************************************************************************/
- int iSendLen = nIniSendlen +1;
- BYTE byArySend[5] = {0};
- memcpy(byArySend,szIniSendCMD,nIniSendlen);
- byArySend[nIniSendlen] = 0x0D;
- if (WaitForSingleObject(m_hSemComm,0) == WAIT_OBJECT_0 )
- {
- ResetEvent(m_hSemComm);
- int nResult = pComm->Write(byArySend, iSendLen);
- if (nResult != iSendLen)
- {
- SetEvent(m_hSemComm);
- LOG4C((LOG_NOTICE,"RS232MODEL 串口返回数据长度不对"));
- return EER_CODE_COM_REGNUM;
- }
- }
- else
- {
- LOG4C((LOG_NOTICE,"RS232MODEL 串口线程处于无信号当中,无法处理"));
- return ERR_CODE_COM_BUSY;
- }
- #endif
- return 0;
- }
- int RS232Model_UPSLan::Recv_ReadDeviceData(CCommProcess *pComm,const char *szIniSendCMD, char *szCmd)
- {
- #if DEBGU_RS232MODEL_UPSLAN
- BYTE *byAryRecv = new BYTE[1024];
- ZeroMemory(byAryRecv, 1024);
- int nReadLen = pComm->Read(byAryRecv,MAX_RECE_MSG);
- if (nReadLen <= 0)
- {
- SetEvent(m_hSemComm);
- if (byAryRecv != NULL)
- {
- delete[] byAryRecv;
- byAryRecv = NULL;
- }
- LOG4C((LOG_NOTICE,"RS232MODEL 设备无返回数据"));
- return ERR_CODE_COM_READ_NO_DATA;
- } //长度校验出错 ;
- SetRS232Model_RSMsg(szIniSendCMD, szCmd, (char*)byAryRecv);
- SetRS232Model_RDMsg(szIniSendCMD, szCmd, (char*)byAryRecv);
- m_dwOnlineTick = GetTickCount();
- SetEvent( m_hSemComm );
- if( byAryRecv != NULL)
- {
- delete[] byAryRecv;
- byAryRecv = NULL;
- }
- #else
- SimulationCommData();
- #endif
- return 0;
- }
- void RS232Model_UPSLan::SetRS232Model_RSMsg(const char *szIniSendCMD, char *szCmd, char *pBuff)
- {
- if ( strcmp(szIniSendCMD,"RS") == 0)
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- memcpy(m_szRS232_RSMsg, pBuff, sizeof(m_szRS232_RSMsg));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- void RS232Model_UPSLan::SetRS232Model_RDMsg(const char *szIniSendCMD, char *szCmd, char *pBuff)
- {
- if ( strcmp(szIniSendCMD,"RD") == 0)
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- memcpy(m_szRS232_RDMsg, pBuff, sizeof(m_szRS232_RDMsg));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- int RS232Model_UPSLan::GetRS232Model_RSMsg(const char *szIniSendCMD, char *szCmd, char *szMsg, int &nIndex, int &nLen, char *szType )
- {
- int nRet = 0;
- if ( strcmp(szIniSendCMD,"RS") == 0)
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- DataConversion(szType, m_szRS232_RSMsg + nIndex, szMsg, nLen);
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- int RS232Model_UPSLan::GetRS232Model_RDMsg(const char *szIniSendCMD, char *szCmd, char *szMsg, int &nIndex, int &nLen, char *szType )
- {
- int nRet = 0;
- if ( strcmp(szIniSendCMD,"RD") == 0)
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- DataConversion(szType, m_szRS232_RDMsg + nIndex, szMsg, nLen);
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- void RS232Model_UPSLan::SimulationCommData(void)
- {
- //DVIA0240.80..DVIB0245.90..DVIC0238.70..DVOA0219.40..DVOB0000.00..DVOC0000.00..DLOA0000.60..DLOB0000.00..DLOC0000.00..DVBT0434.60..DVBN0434.90..DVBL0100.00..DHZF0050.00..DTMB0120.00..DTMP0025.1
- memcpy(m_szRS232_RDMsg,"DVIA0240.80..DVIB0245.90..DVIC0238.70..DVOA0219.40..DVOB0000.00..DVOC0000.00..DLOA0000.60..DLOB0000.00..DLOC0000.00..DVBT0434.60..DVBN0434.90..DVBL0100.00..DHZF0050.00..DTMB0120.00..DTMP0025.1",
- sizeof("DVIA0240.80..DVIB0245.90..DVIC0238.70..DVOA0219.40..DVOB0000.00..DVOC0000.00..DLOA0000.60..DLOB0000.00..DLOC0000.00..DVBT0434.60..DVBN0434.90..DVBL0100.00..DHZF0050.00..DTMB0120.00..DTMP0025.1"));
- //SOK0..SUF0..SBP0..SBM0..SBL0..
- memcpy(m_szRS232_RSMsg,"SOK0..SUF0..SBP0..SBM0..SBL0..",sizeof("SOK0..SUF0..SBP0..SBM0..SBL0.."));
- }
-
|