| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- #include "StdAfx.h"
- #include ".\st_3c20ks.h"
- #include "CommProcess.h"
- CST_3C20KS::CST_3C20KS(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_szQMsg, 0, sizeof(m_szQMsg));
- //memset(m_szQ1Msg, 0, sizeof(m_szQ1Msg));
- m_dwOnlineTick = 0;
- m_devOnline = TRUE;
- }
- CST_3C20KS::~CST_3C20KS()
- {
- #if IS_USE_READMSG_CS
- DeleteCriticalSection(& m_csReadMsg);
- #endif
- MTVERIFY(CloseHandle(m_hSemComm));
- CloseComm();
- }
- BOOL CST_3C20KS::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_3C20KS::SendReadRequest(char *szPath, // 程序所在路径
- char *szIniName, // 配置文件名称
- int nCommPort, // 串行端口
- int nAddr, // 设备地址
- char *szCmd, // 请求命令
- char *szMsg, // 响应的值
- 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, // 预留字符数组参数1接口
- char *szReversed2, // 预留字符数组参数2接口
- char *szReversed3, // 预留字符数组参数3接口
- char *szReversed4, // 预留字符数组参数4接口
- char *szReversed5 // 预留字符数组参数5接口
- )
- {
- CCommProcess *pComm = FindComm(nCommPort);
- if( pComm == NULL )
- {
- LOG4C((LOG_NOTICE,"3C20KS pComm=NULL"));
- return -1;
- }
- int nRet = -1;
- int nIndex(0), nLen(0), IniSendlen(0),iSBit(0), iEBit(0);
- char IniSendCMD[MAX_CMD] = {0}, szDataType[CMD_TYPE] = {0};
- GetIniInfo(szPath,szIniName,szCmd,IniSendCMD,IniSendlen,szDataType,nIndex,nLen,iSBit,iEBit);
- int iCmd = atoi(szCmd + 4);
- if( (strlen(m_szQMsg) == 0 && (iCmd >= 1 && iCmd <= 14)) || iCmd == 1 )
- {
- nRet = GetDeviceParam( nAddr,pComm, szCmd, IniSendCMD, IniSendlen );
- 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_QMsg(nAddr,szCmd, szMsg, nIndex, nLen, szDataType,iSBit,iEBit );
- return nRet;
- }
- int CST_3C20KS::GetSanTak_QMsg(int nAddr,char *szCmd, char *szRecvMsg,int &nIndex,int &nLen,char *szDataType,const int &iSBit, const int &iEBit)
- {
- int nRet = 0;
- int iCmd = atoi(szCmd + 4);
- if ( iCmd >=1 && iCmd <= 14)
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- DataConversion(szDataType, m_szQMsg + nIndex, szRecvMsg, nLen, 0, 0);
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- int CST_3C20KS::GetDeviceParam(int nAddr,CCommProcess *pComm,char *szCmd,char *IniSendCMD,const int &IniSendlen)
- {
- //LOG4C((LOG_NOTICE,"PDU::GetDeviceParam"));
- int nRet = -1;
- nRet = Send_ReadDeviceData(nAddr, pComm, szCmd, IniSendCMD, IniSendlen);
- if( nRet != 0 )
- {
- LOG4C((LOG_NOTICE,"山特3C20KS发送命令失败"));
- return nRet;
- }
- nRet = Recv_ReadDeviceData( nAddr, pComm, szCmd);
- return nRet;
- }
- int CST_3C20KS::Send_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd, const char *IniSendCMD,const int &IniSendlen)
- {
- #if DEBUG_ST3C20KS
- char szSend[3] = {0};
- szSend[0] = 0x51;
- szSend[1] = 0x31;
- szSend[2] = 0x0D;
- if (WaitForSingleObject(m_hSemComm,0) == WAIT_OBJECT_0 ) {
- int nDatalen = (int)sizeof(szSend);
- ResetEvent(m_hSemComm);
- int nResult = pComm->Write((BYTE *)&szSend, nDatalen);
- if (nResult != nDatalen){
- SetEvent(m_hSemComm);
- return EER_CODE_SANTAK_COM_REGNUM;
- }
- }
- else{
- return ERR_CODE_SANTAK_COM_BUSY;
- }
- #endif
- return 0;
- }
- int CST_3C20KS::Recv_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd)
- {
- #if DEBUG_ST3C20KS
- int nReceiveLen = 0;
- int nProcessLen = 0;
- int nReadLen = 0;
- RESPONSE_STRUCT strcutResponse;
- memset(&strcutResponse, 0, sizeof(RESPONSE_STRUCT));
- nReceiveLen = sizeof(RESPONSE_STRUCT);
- char *pBuff = new char[nReceiveLen];
- memset(pBuff, 0 , nReceiveLen);
- nReadLen = pComm->Read((BYTE *)pBuff,nReceiveLen);
- //LOG4C((LOG_NOTICE, "CSanTak::ResponseStatus pBuff = %s, nReadLen = %d, nReceiveLen = %d", pBuff, nReadLen, nReceiveLen));
- if (nReadLen <= 0){
- SetEvent(m_hSemComm);
- if (pBuff != NULL) {
- delete [] pBuff;
- pBuff = NULL;
- }
- return ERR_CODE_SANTAK_COM_READ_NO_DATA;
- } //长度校验出错 ;
- SetSanTakQMsg(nAddr,szCmd, pBuff);
- m_dwOnlineTick = GetTickCount();
- SetEvent( m_hSemComm );
- if( pBuff != NULL) {
- delete[] pBuff;
- pBuff = NULL;
- }
- #else
- SimulationCommData();
- #endif
- return 0;
- }
- void CST_3C20KS::SetSanTakQMsg( int nAddr,char *szCmd, char *pBuffer)
- {
- int iCmd = atoi(szCmd + 4);
- if ( iCmd >=1 && iCmd <= 14){
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- memcpy(m_szQMsg, pBuffer, sizeof(m_szQMsg));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- void CST_3C20KS::SimulationCommData(void)
- {
- memcpy(m_szQMsg, "(215.4 216.8 221.0 056 50.0 2.29 44.5 01111100.", sizeof("(215.4 216.8 221.0 056 50.0 2.29 44.5 00000000."));
- }
-
|