| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- #include "StdAfx.h"
- #include ".\eam7000.h"
- eam7000::eam7000(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) ); //CreateEvent()创建或打开一个命名的或无名的事件对象
- for( int i = 0; i < MAX_ADDR; i++ )
- {
- memset(m_szMsg[i], 0, sizeof(m_szMsg[i]));
- m_devOnline[i] = TRUE;
- m_dwOnlineTick[i] = 0;
- }
- }
- eam7000::~eam7000()
- {
- #if IS_USE_READMSG_CS
- DeleteCriticalSection( &m_csReadMsg );
- #endif
- MTVERIFY( CloseHandle( m_hSemComm ) );
- CloseComm();
- }
- BOOL eam7000::eam7000_OpenComm(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 eam7000::GetIniInfo(char *szPath,char *szIniName,char *szCmd,char *IniSendCMD,int &IniSendlen,char *szDataType,int &nIndex,int &nLen, int &iSBit, int &iEBit)
- {
- CHAR szFile[MAX_PATH + 1] = "";
- wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
- IniSendlen = GetPrivateProfileString(szCmd, "SendCmd", "", IniSendCMD, 10, szFile); // 返回的字符串是以\0结束的;
- GetPrivateProfileString(szCmd, "type", "", szDataType, 10, szFile);
- szDataType[strlen(szDataType)] = '\0';
- nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile);
- nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile);
- iSBit = GetPrivateProfileInt(szCmd, "iSBit", 0, szFile);//从配置文件中取值
- iEBit = GetPrivateProfileInt(szCmd, "iEBit", 0, szFile);
- return 0;
- }
- // 发送读取设备参数请求
- int eam7000::SendReadRequest(
- char szPath[MAX_PATH], // 程序所在路径
- char szIniName[MAX_PATH], // 配置文件名称
- int nCommPort, // 串行端口
- int nAddr, // 设备地址
- char szCmd[MAX_CMD], // 请求命令
- char szMsg[VAR_MSG], // 响应的值
- 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[MAX_RESERVED1], // 预留字符数组参数1接口
- char szReversed2[MAX_RESERVED2], // 预留字符数组参数2接口
- char szReversed3[MAX_RESERVED3], // 预留字符数组参数3接口
- char szReversed4[MAX_RESERVED4], // 预留字符数组参数4接口
- char szReversed5[MAX_RESERVED5] // 预留字符数组参数5接口
- )
- {
- 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( nAddr < 1 )
- return -1;
- int iCmd = atoi(szCmd + 4);
- if( (strlen(m_szMsg[nAddr -1]) == 0 && (iCmd >= 1 && iCmd <= 16) || iCmd == 1 ) ||
- (strlen(m_szMsg[nAddr -1]) == 0 && (iCmd >=110 && iCmd <=117) || iCmd == 110 || iCmd == 115) )
- {
- nRet = GetDeviceParam( nAddr,pComm, szCmd, IniSendCMD, IniSendlen );
- if ( nRet != 0)
- {
- LOG4C((LOG_NOTICE,"GetDeviceParam::error"));
- return nRet;
- }
- }
- if( GetTickCount() - m_dwOnlineTick[nAddr - 1] > 60 *1000 && m_dwOnlineTick[nAddr - 1] > 0 )
- m_devOnline[nAddr - 1] = FALSE;
- else if( GetTickCount() - m_dwOnlineTick[nAddr - 1] < 60 *1000 && m_dwOnlineTick[nAddr - 1] > 0 )
- m_devOnline[nAddr - 1] = TRUE;
- if( m_devOnline[nAddr - 1] == FALSE )
- return -1;
- nRet = GetLDSVarMsg( nAddr, szCmd, szMsg, nIndex, nLen, szDataType,iSBit,iEBit);
- //LOG4C((LOG_NOTICE," %s = %s",szCmd,szMsg));
- return nRet;
- }
- int eam7000::GetDeviceParam(int nAddr,CCommProcess *pComm,char *szCmd,char *IniSendCMD,const int &IniSendlen)
- {
- int nRet = -1;
- nRet = Send_ReadDeviceData(nAddr, pComm, szCmd, IniSendCMD, IniSendlen);
- if( nRet != 0 )
- {
- LOG4C((LOG_NOTICE,"Send_ReadDeviceData::error"));
- return nRet;
- }
- nRet = Recv_ReadDeviceData( nAddr, pComm, szCmd);
- return nRet;
- }
- int eam7000::SendSetReuest(
- 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接口
- )
- {
- return 0;
- }
- int eam7000::Send_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd, const char *IniSendCMD,const int &IniSendlen)
- {
- #if DEBUG_EAM7000
- int iSendLen = 0;
-
- BYTE byArySend[5] = {0};
- // SOI
- byArySend[0] = 0x24;
- // 地址;
- byArySend[1] = ByteToAscii((nAddr>>4) & 0x0f);
- byArySend[2] = ByteToAscii(nAddr & 0x0f);
- // EOI;
- byArySend[3] = 0x36;
- byArySend[4] = 0x0D;
-
- iSendLen = 5;
- if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口
- {
- ResetEvent( m_hSemComm );
- int nResult = pComm->Write(byArySend, iSendLen);
- if( nResult != iSendLen )
- {
- LOG4C((LOG_NOTICE,"Send_ReadDeviceData::发送命令失败"));
- SetEvent( m_hSemComm );
- return ERR_CODE_COM_FAULT;
- }
- }
- else
- {
- return ERR_CODE_COM_BUSY;
- }
- #endif
- return 0;
- }
- int eam7000::Recv_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd)
- {
- #if DEBUG_EAM7000
- BYTE *byAryRecv = new BYTE[20];
- ZeroMemory(byAryRecv, 20);
- int iRecvLen = pComm->Read(byAryRecv, 20);
- if( iRecvLen <= 0)
- {
- SetEvent( m_hSemComm );
- if( iRecvLen != NULL)
- {
- LOG4C((LOG_NOTICE,"Recv_ReadDeviceData::返回长度为0"));
- delete[] byAryRecv;
- byAryRecv = NULL;
- }
- return ERR_CODE_COM_READ_NO_DATA;
- }
- SetLDSVarMsg( nAddr,szCmd, (char*)byAryRecv);
- m_dwOnlineTick[nAddr -1] = GetTickCount();
- SetEvent( m_hSemComm );
- if( byAryRecv != NULL)
- {
- delete[] byAryRecv;
- byAryRecv = NULL;
- }
- #else
- SimulationCommData();
- #endif
- return 0;
- }
- void eam7000::SetLDSVarMsg( int nAddr,char *szCmd, char *pBuffer)
- {
- int iCmd = atoi(szCmd + 4);
- if ( iCmd >= 1 && iCmd <=16)
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- memcpy(m_szMsg[nAddr-1], pBuffer, sizeof(m_szMsg[nAddr-1]));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }else if ( iCmd >= 110 && iCmd <=117)
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- memcpy(m_szMsg[nAddr-1], pBuffer, sizeof(m_szMsg[nAddr-1]));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- //LOG4C((LOG_NOTICE,"%s,iSour = %d, nIndex = %d, nLen = %d, iSBit = %d, iEBit = %d",szCmd,iSour, nIndex, nLen, iSBit, iEBit));
- int eam7000::GetLDSVarMsg(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 <=16)
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- char Cable[5] ={0};
- memcpy(Cable, m_szMsg[nAddr - 1] + 1,4);
- int iSour = HexStr2Dec(Cable);
- //LOG4C((LOG_NOTICE,"%s,iSour = %d, nIndex = %d, nLen = %d, iSBit = %d, iEBit = %d",szCmd,iSour, nIndex, nLen, iSBit, iEBit));
- GetWORDBit(szRecvMsg,iSour,iSBit,iEBit);
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- nRet = 0;
- #endif
- }else if( iCmd >= 110 && iCmd <= 117)
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- char Cable[3] ={0};
- memcpy(Cable, m_szMsg[nAddr - 1] + 1,2);
- int iSour = HexStr2Dec(Cable);
- //LOG4C((LOG_NOTICE,"%s,iSour = %d, nIndex = %d, nLen = %d, iSBit = %d, iEBit = %d",szCmd,iSour, nIndex, nLen, iSBit, iEBit));
- GetBYTEBit(szRecvMsg,iSour,iSBit,iEBit);
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- nRet = 0;
- #endif
- }
- return nRet;
- }
- void eam7000::SimulationCommData(void)
- {
- memcpy(m_szMsg,":0104061F0801030109EF..",sizeof(":010406120801030109EF.."));
- }
|