#include "StdAfx.h" #include ".\lds2011.h" lds2011::lds2011(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; } } lds2011::~lds2011() { #if IS_USE_READMSG_CS DeleteCriticalSection( &m_csReadMsg ); #endif MTVERIFY( CloseHandle( m_hSemComm ) ); CloseComm(); } BOOL lds2011::LDS2011_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 lds2011::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; } BYTE lds2011::GetCheckCode(const BYTE *pSendBuf, const int &ilen) { BYTE byLrc = 0; char pBuf[4]; int nData = 0; for(int i=1; i= 1 && iCmd <= 4)) || iCmd == 1 ) { nRet = GetDeviceParam( nAddr,pComm, szCmd, IniSendCMD, IniSendlen ); if ( nRet != 0) 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,"nRet = %d,cmd = %s,值 = %s",nRet,szCmd,szMsg)); return nRet; } int lds2011::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 ) return nRet; nRet = Recv_ReadDeviceData( nAddr, pComm, szCmd); return nRet; } int lds2011::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 lds2011::Send_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd, const char *IniSendCMD,const int &IniSendlen) { #if DEBUG_LDS2011 int iSendLen = 0; BYTE byArySend[17] = {0}; // [:]; byArySend[0] = 0x3A; // 地址; byArySend[1] = ByteToAscii((nAddr>>4) & 0x0f); byArySend[2] = ByteToAscii(nAddr & 0x0f); // 功能码; byArySend[3] = 0x30; byArySend[4] = 0x34; // 寄存器地址高位; byArySend[5] = 0x30; byArySend[6] = 0x30; // 寄存器地址低位 byArySend[7] = 0x30; byArySend[8] = 0x32; // 寄存器长度高低位; byArySend[9] = 0x30; byArySend[10] = 0x30; byArySend[11] = 0x30; byArySend[12] = 0x33; // 校验; BYTE byCrc = GetCheckCode(byArySend,13); byArySend[13] = ByteToAscii((byCrc>>4) & 0x0f); byArySend[14] = ByteToAscii(byCrc& 0x0f); // 结束; byArySend[15] = 0x0D; byArySend[16] = 0x0A; iSendLen = 17; if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口 { ResetEvent( m_hSemComm ); int nResult = pComm->Write(byArySend, iSendLen); if( nResult != iSendLen ) { SetEvent( m_hSemComm ); return ERR_CODE_COM_FAULT; } } else { return ERR_CODE_COM_BUSY; } #endif return 0; } int lds2011::Recv_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd) { #if DEBUG_LDS2011 BYTE *byAryRecv = new BYTE[23]; ZeroMemory(byAryRecv, 23); int iRecvLen = pComm->Read(byAryRecv, 23); if( iRecvLen <= 0) { SetEvent( m_hSemComm ); if( iRecvLen != NULL) { delete[] byAryRecv; byAryRecv = NULL; } return ERR_CODE_COM_READ_NO_DATA; } BYTE byCrc[3] = {0}; byCrc[2] = GetCheckCode(byAryRecv,19); byCrc[0] = ByteToAscii((byCrc[2] >> 4) & 0x0f); byCrc[1] = ByteToAscii(byCrc[2] & 0x0f); if ( byCrc[0] != byAryRecv[19] || byCrc[1] != byAryRecv[20]) { SetEvent( m_hSemComm ); if( iRecvLen != NULL) { 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 lds2011::SetLDSVarMsg( int nAddr,char *szCmd, char *pBuffer) { int iCmd = atoi(szCmd + 4); if ( iCmd >= 1 && iCmd <=4) { #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 } } int lds2011::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 <=4) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif if ( iCmd == 1 || iCmd == 2) { char Cable[3] ={0}; memcpy(Cable, m_szMsg[nAddr - 1] + nIndex, nLen); int iSour = HexStr2Dec(Cable); //LOG4C((LOG_NOTICE,"%s,iSour = %d, nIndex = %d, nLen = %d, iSBit = %d, iEBit = %d",szCmd,iSour, nIndex, nLen, iSBit, iEBit)); ONEBYTE_BIT(szRecvMsg,iSour,iSBit,iEBit); //GetWORDBit(szRecvMsg,iSour,iSBit,iEBit); } else { char Leak[5] ={0}; memcpy(Leak, m_szMsg[nAddr - 1] + nIndex, nLen); itoa( HexStr2Dec(Leak),szRecvMsg,10 ); } #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); nRet = 0; #endif } return nRet; } void lds2011::SimulationCommData(void) { memcpy(m_szMsg,":0104061F0801030109EF..",sizeof(":010406120801030109EF..")); }