| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- #include "StdAfx.h"
- #include ".\RS232Model.h"
- RS232Model::RS232Model(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;
- }
- RS232Model::~RS232Model(void)
- {
- #if IS_USE_READMSG_CS
- DeleteCriticalSection(& m_csReadMsg);
- #endif
- MTVERIFY(CloseHandle(m_hSemComm));
- CloseComm();
- }
- BOOL RS232Model::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::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), 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);
- if( (strlen(m_szQ2Msg) == 0 && strcmp(IniSendCMD,"Q2") ==0 ) || strcmp(szCmd,"cmd-1") ==0 ||
- (strlen(m_szWAMsg) == 0 && strcmp(IniSendCMD,"WA") ==0 ) || strcmp(szCmd,"cmd-2") ==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(IniSendCMD, szCmd, szMsg, nIndex, nLen, szDataType );
- //LOG4C((LOG_NOTICE, "%s=%s", szCmd, szMsg));
- return nRet;
- }
- int RS232Model::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, IniSendCMD, szCmd);
- return nRet;
- }
- //int RS232Model::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 RS232Model::Send_ReadDeviceData(CCommProcess *pComm,const char *IniSendCMD,const int &IniSendlen )
- {
- #if DEBGU_RS232MODEL
- /************************************************************************/
- /* */
- /************************************************************************/
- if (WaitForSingleObject(m_hSemComm,0) == WAIT_OBJECT_0 )
- {
- ResetEvent(m_hSemComm);
- int nResult = pComm->Write((BYTE *)IniSendCMD, IniSendlen);
- if (nResult != IniSendlen)
- {
- 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::Recv_ReadDeviceData(CCommProcess *pComm,const char *IniSendCMD, char *szCmd)
- {
- #if DEBGU_RS232MODEL
- BYTE *byAryRecv = new BYTE[90];
- ZeroMemory(byAryRecv, 90);
- 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;
- } //长度校验出错 ;
- SetSanTakQ2Msg(IniSendCMD, szCmd, (char*)byAryRecv);
- m_dwOnlineTick = GetTickCount();
- SetEvent( m_hSemComm );
- if( byAryRecv != NULL)
- {
- delete[] byAryRecv;
- byAryRecv = NULL;
- }
- #else
- SimulationCommData();
- #endif
- return 0;
- }
- void RS232Model::SetSanTakQ2Msg(const char *IniSendCMD, char *szCmd, char *pBuff)
- {
- if ( strcmp(IniSendCMD,"Q2") == 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
- }
- }
- int RS232Model::GetSanTak_Q2Msg(const char *IniSendCMD, char *szCmd, char *szMsg, int &nIndex, int &nLen, char *szType )
- {
- int nRet = 0;
- if ( strcmp(IniSendCMD,"WA") == 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;
- }
- void RS232Model::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."));
- }
|