#include "StdAfx.h" #include ".\canatal.h" canatal::canatal(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)); for (int i = 0; i < MAX_ADDR ; i++) { memset(m_sz00Msg[i], 0, sizeof(m_sz00Msg[i])); // memset(m_sz03Msg[i], 0, sizeof(m_sz03Msg[i])); m_dwOnlineTick[i] = 0; m_devOnline[i] = TRUE; } } canatal::~canatal(void) { #if IS_USE_READMSG_CS DeleteCriticalSection(& m_csReadMsg); #endif MTVERIFY(CloseHandle(m_hSemComm)); CloseComm(); } BOOL canatal::OpenCRACComm(int nCommPort, int nAddr, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval) { BOOL bResult = FALSE; //LOG4C((LOG_NOTICE,"->OpenUPSComm")); bResult = OpenComm( nCommPort, nAddr, nRate, nDataBit, nStopBit, nParity, nInterval ); return bResult; } int canatal::GetIniInfo(char *szPath,char *szIniName,char *szCmd,char *IniSendCMD, int &IniSendlen, char *nType,int &nIndex,int &nLen, int &nBit) { 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结束的; GetPrivateProfileString(szCmd, "type", "", nType, 10, szFile); nType[strlen(nType)] = '\0'; nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile); nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile); nBit = GetPrivateProfileInt(szCmd,"Bit",-1,szFile); return 0; } int canatal::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 ) { //LOG4C((LOG_NOTICE,"->SendReadRequest")); CCommProcess *pComm = FindComm(nCommPort); if( pComm == NULL ) return -1; int nRet(-1); int nIndex(0), nLen(0), IniSendlen(0),nBit(-1); char IniSendCMD[MAX_CMD] = {0}, szDataType[CMD_TYPE] = {0}; GetIniInfo(szPath,szIniName,szCmd,IniSendCMD,IniSendlen,szDataType,nIndex,nLen,nBit); if ( (strlen(m_sz00Msg[nAddr -1]) == 0 && ( strcmp(szCmd, "cmd-1") ==0 || strcmp(szCmd, "cmd-2") ==0 || strcmp(szCmd, "cmd-3") ==0 || strcmp(szCmd, "cmd-4") ==0 || strcmp(szCmd, "cmd-5") ==0 || strcmp(szCmd, "cmd-6") ==0 || strcmp(szCmd, "cmd-7") ==0 || strcmp(szCmd, "cmd-8") ==0 || strcmp(szCmd, "cmd-9") ==0 || strcmp(szCmd, "cmd-10") ==0 || strcmp(szCmd, "cmd-11") ==0 || strcmp(szCmd, "cmd-12") ==0 || strcmp(szCmd, "cmd-13") ==0 || strcmp(szCmd, "cmd-14") ==0 || strcmp(szCmd, "cmd-15") ==0 || strcmp(szCmd, "cmd-16") ==0 || strcmp(szCmd, "cmd-17") ==0 || strcmp(szCmd, "cmd-18") ==0 || strcmp(szCmd, "cmd-19") ==0 || strcmp(szCmd, "cmd-20") ==0 || strcmp(szCmd, "cmd-21") ==0 || strcmp(szCmd, "cmd-22") ==0 || strcmp(szCmd, "cmd-23") ==0 || strcmp(szCmd, "cmd-24") ==0 || strcmp(szCmd, "cmd-25") ==0 || strcmp(szCmd, "cmd-26") ==0 ) )|| strcmp(szCmd, "cmd-1") == 0 ) { nRet = GetDeviceParam(nAddr ,pComm, IniSendCMD,IniSendlen, szCmd ); 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 = Get_00Msg(nAddr, szCmd, szMsg, nIndex, nLen, nBit,szDataType); //nRet = Get_03Msg(nAddr, szCmd, szMsg, nIndex, nLen, szDataType); LOG4C((LOG_NOTICE,"nRet = %d,cmd = %s,值 = %s",nRet,szCmd,szMsg)); return nRet; } // 发送写设置; int canatal::SendSetReuest( 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),nBit(-1); char IniSendCMD[MAX_CMD] = {0}, szDataType[CMD_TYPE] = {0}; GetIniInfo(szPath,szIniName,szCmd,IniSendCMD,IniSendlen,szDataType,nIndex,nLen,nBit); if ( strcmp(szCmd, "cmd-27") ==0 ) { BYTE szSendMSG[32] = {0}; // 对数组进行赋值; szSendMSG[0] = 0x55; szSendMSG[1] = 0x67; szSendMSG[2] = 0x89; szSendMSG[3] = 0xAB; szSendMSG[4] = 0xCD; szSendMSG[5] = 0xEF; szSendMSG[6] = 0x12; szSendMSG[7] = 0x34; szSendMSG[8] = nAddr; //szSendMSG[9] = atoi(IniSendCMD); szSendMSG[9] = 0x03; int iCrc = RtnCheck( szSendMSG, 10 ); szSendMSG[10] = iCrc>>8&0xff; // 高字节; szSendMSG[11] = iCrc&0xff; // 低字节; int iLen = 12; if (WaitForSingleObject(m_hSemComm,0) == WAIT_OBJECT_0 ) { ResetEvent(m_hSemComm); int nResult = pComm->Write(szSendMSG, iLen); if (nResult != iLen) { SetEvent(m_hSemComm); return EER_CODE_COM_REGNUM; } } else { return ERR_CODE_COM_BUSY; } int nProcessLen(0),nReadLen(0); char *pBuff = new char[MAX_RECE_MSG]; memset(pBuff, 0 , MAX_RECE_MSG); nReadLen = pComm->Read((BYTE *)pBuff,MAX_RECE_MSG); SetEvent(m_hSemComm); if( pBuff != NULL) { delete[] pBuff; pBuff = NULL; } } return nRet; } int canatal::GetDeviceParam(int naddr, CCommProcess *pComm,char *IniSendCMD,const int &IniSendlen ,char *szCmd) { int nRet(-1); nRet = Send_ReadDeviceData(naddr, pComm, IniSendCMD, IniSendlen); if( nRet != 0 ) { return nRet; } nRet = Recv_ReadDeviceData(naddr, pComm, szCmd); return nRet; } int canatal::Send_ReadDeviceData(int nAddr,CCommProcess *pComm,const char *IniSendCMD,const int &IniSendlen) { #if DEBUG_CANATAL13 BYTE szSendMSG[32] = {0}; // 对数组进行赋值; szSendMSG[0] = 0x55; szSendMSG[1] = 0x67; szSendMSG[2] = 0x89; szSendMSG[3] = 0xAB; szSendMSG[4] = 0xCD; szSendMSG[5] = 0xEF; szSendMSG[6] = 0x12; szSendMSG[7] = 0x34; szSendMSG[8] = nAddr; szSendMSG[9] = atoi(IniSendCMD); int iCrc = RtnCheck( szSendMSG, 10 ); szSendMSG[10] = iCrc>>8&0xff; // 高字节; szSendMSG[11] = iCrc&0xff; // 低字节; int iLen = 12; if (WaitForSingleObject(m_hSemComm,0) == WAIT_OBJECT_0 ) { ResetEvent(m_hSemComm); int nResult = pComm->Write(szSendMSG, iLen); if (nResult != iLen){ SetEvent(m_hSemComm); return EER_CODE_COM_REGNUM; } } else{ return ERR_CODE_COM_BUSY; } #endif return 0; } int canatal::Recv_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd) { #if DEBUG_CANATAL13 int nProcessLen(0),nReadLen(0); char *pBuff = new char[MAX_RECE_MSG]; memset(pBuff, 0 , MAX_RECE_MSG); nReadLen = pComm->Read((BYTE *)pBuff,MAX_RECE_MSG); // 对返回的数据进行CRC校验;(不作校验) //int iCrc = RtnCheck((BYTE *)pBuff, nReadLen-2); //BYTE Hby = iCrc>>8&0xff; // 高字节; //BYTE Lby = iCrc&0xff; // 低字节; //if (iCrc != 0) //{ // SetEvent( m_hSemComm ); // if( pBuff != NULL) // { // delete[] pBuff; // pBuff = NULL; // } // LOG4C((LOG_NOTICE,"->RTN校验出错:%d",nRet)); // return -1; //RTN校验出错 //} //if (nReadLen != 23){ if (nReadLen < 0){ SetEvent(m_hSemComm); if (pBuff != NULL) { delete [] pBuff; pBuff = NULL; } LOG4C((LOG_NOTICE,"->串口没有读到数据")); return ERR_CODE_COM_READ_NO_DATA; } //长度校验出错 ; Copy_00_Msg(nAddr, szCmd, pBuff); m_dwOnlineTick[nAddr -1] = GetTickCount(); SetEvent( m_hSemComm ); if( pBuff != NULL) { delete[] pBuff; pBuff = NULL; } #else SimulationCommData(nAddr); #endif return 0; } void canatal::Copy_00_Msg( int nAddr, char *szCmd, char *pBuffer) { if ( strcmp(szCmd, "cmd-1") ==0 || strcmp(szCmd, "cmd-2") ==0 || strcmp(szCmd, "cmd-3") ==0 || strcmp(szCmd, "cmd-4") ==0 || strcmp(szCmd, "cmd-5") ==0 || strcmp(szCmd, "cmd-6") ==0 || strcmp(szCmd, "cmd-7") ==0 || strcmp(szCmd, "cmd-8") ==0 || strcmp(szCmd, "cmd-9") ==0 || strcmp(szCmd, "cmd-10") ==0 || strcmp(szCmd, "cmd-11") ==0 || strcmp(szCmd, "cmd-12") ==0 || strcmp(szCmd, "cmd-13") ==0 || strcmp(szCmd, "cmd-14") ==0 || strcmp(szCmd, "cmd-15") ==0 || strcmp(szCmd, "cmd-16") ==0 || strcmp(szCmd, "cmd-17") ==0 || strcmp(szCmd, "cmd-18") ==0 || strcmp(szCmd, "cmd-19") ==0 || strcmp(szCmd, "cmd-20") ==0 || strcmp(szCmd, "cmd-21") ==0 || strcmp(szCmd, "cmd-22") ==0 || strcmp(szCmd, "cmd-23") ==0 || strcmp(szCmd, "cmd-24") ==0 || strcmp(szCmd, "cmd-25") ==0 || strcmp(szCmd, "cmd-26") ==0 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif memcpy(m_sz00Msg[nAddr -1], pBuffer, sizeof(m_sz00Msg[nAddr -1])); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } //void canatal::Copy_03_Msg( int nAddr, char *szCmd, char *pBuffer) //{ // if ( // strcmp(szCmd, "cmd-16") ==0 || strcmp(szCmd, "cmd-17") ==0 || strcmp(szCmd, "cmd-18") ==0 || // strcmp(szCmd, "cmd-19") ==0 || strcmp(szCmd, "cmd-20") ==0 || strcmp(szCmd, "cmd-21") ==0 || // strcmp(szCmd, "cmd-22") ==0 || strcmp(szCmd, "cmd-23") ==0 || strcmp(szCmd, "cmd-24") ==0 || // strcmp(szCmd, "cmd-25") ==0 || strcmp(szCmd, "cmd-26") ==0 || strcmp(szCmd, "cmd-27") ==0 || // strcmp(szCmd, "cmd-28") ==0 || strcmp(szCmd, "cmd-29") ==0 || strcmp(szCmd, "cmd-30") ==0 || // strcmp(szCmd, "cmd-31") ==0 // ) // { //#if IS_USE_READMSG_CS // EnterCriticalSection(&m_csReadMsg); //#endif // memcpy(m_sz03Msg[nAddr -1], pBuffer, sizeof(m_sz03Msg[nAddr -1])); //#if IS_USE_READMSG_CS // LeaveCriticalSection(&m_csReadMsg); //#endif // } //} int canatal::Get_00Msg(int nAddr, char *szCmd, char *szMsg, int &nIndex, int &nLen, int &nBit,char *szType ) { int nRet(0); if (strcmp(szCmd, "cmd-1") ==0 || strcmp(szCmd, "cmd-2") ==0 || strcmp(szCmd, "cmd-3") ==0 || strcmp(szCmd, "cmd-4") ==0 || strcmp(szCmd, "cmd-5") ==0 || strcmp(szCmd, "cmd-6") ==0 || strcmp(szCmd, "cmd-7") ==0 || strcmp(szCmd, "cmd-8") ==0 || strcmp(szCmd, "cmd-9") ==0 || strcmp(szCmd, "cmd-10") ==0 || strcmp(szCmd, "cmd-11") ==0 || strcmp(szCmd, "cmd-12") ==0 || strcmp(szCmd, "cmd-13") ==0 || strcmp(szCmd, "cmd-14") ==0 || strcmp(szCmd, "cmd-15") ==0 || strcmp(szCmd, "cmd-16") ==0 || strcmp(szCmd, "cmd-17") ==0 || strcmp(szCmd, "cmd-18") ==0 || strcmp(szCmd, "cmd-19") ==0 || strcmp(szCmd, "cmd-20") ==0 || strcmp(szCmd, "cmd-21") ==0 || strcmp(szCmd, "cmd-22") ==0 || strcmp(szCmd, "cmd-23") ==0 || strcmp(szCmd, "cmd-24") ==0 || strcmp(szCmd, "cmd-25") ==0 || strcmp(szCmd, "cmd-26") ==0 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif if ( nBit == -1) DataConversion(szType, m_sz00Msg[nAddr -1] + nIndex, szMsg, nLen); else BitDataProcess(m_sz00Msg[nAddr -1] + nIndex, szMsg, nLen, nBit); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } //int canatal::Get_03Msg(int nAddr, char *szCmd, char *szMsg, int &nIndex, int &nLen, int &nBit,char *szType ) //{ // int nRet(0); // if (strcmp(szCmd, "cmd-16") ==0 || strcmp(szCmd, "cmd-17") ==0 || strcmp(szCmd, "cmd-18") ==0 || // strcmp(szCmd, "cmd-19") ==0 || strcmp(szCmd, "cmd-20") ==0 || strcmp(szCmd, "cmd-21") ==0 || // strcmp(szCmd, "cmd-22") ==0 || strcmp(szCmd, "cmd-23") ==0 || strcmp(szCmd, "cmd-24") ==0 || // strcmp(szCmd, "cmd-25") ==0 || strcmp(szCmd, "cmd-26") ==0 || strcmp(szCmd, "cmd-27") ==0 || // strcmp(szCmd, "cmd-28") ==0 || strcmp(szCmd, "cmd-29") ==0 || strcmp(szCmd, "cmd-30") ==0 || // strcmp(szCmd, "cmd-31") ==0 ) // { //#if IS_USE_READMSG_CS // EnterCriticalSection(&m_csReadMsg); //#endif // DataConversion(szType, m_sz03Msg[nAddr -1] + nIndex, szMsg, nLen); //#if IS_USE_READMSG_CS // LeaveCriticalSection(&m_csReadMsg); //#endif // nRet = 0; // } // return nRet; //} UINT canatal::RtnCheck(BYTE *pBuf,int len) { unsigned int Genpoly=0xA001; unsigned int CRC=0xFFFF; unsigned int index; while(len--) { CRC=CRC^(unsigned int)*pBuf++; for(index=0;index<8;index++) { if((CRC & 0x0001)==1) CRC=(CRC>>1)^Genpoly; else (CRC=CRC>>1); } } return(CRC); } void canatal::BitDataProcess(const char *szSour, char *szRecv, int &iSourLen,int &StaBit) { CString strBit = ""; // 存放位串字符; int ByteBit = 0; char *tmp_Sour = new char[iSourLen+1]; memcpy(tmp_Sour,szSour,iSourLen); if ( iSourLen == 1) { int sum = tmp_Sour[0]; switch(StaBit) // switch(7 - StaBit); { case 7: ByteBit = sum & 0x01; break; case 6: ByteBit = (sum & 0x02) / 0x02 ; break; case 5: ByteBit = (sum & 0x04) / 0x04 ; break; case 4: ByteBit = (sum & 0x08) / 0x08 ; break; case 3: ByteBit = (sum & 0x10) / 0x10 ; break; case 2: ByteBit = (sum & 0x20) / 0x20 ; break; case 1: ByteBit = (sum & 0x40) / 0x40 ; break; case 0: ByteBit = (sum & 0x80) / 0x80; break; } itoa(ByteBit,szRecv,10); } else { for (int i = 0; i < iSourLen; i+=2) { char szDest[9] = {0}; // 目标数据; int sum = (AsciiToBYTE(tmp_Sour[i]))*16 + AsciiToBYTE(tmp_Sour[i+1]); for(int j = 0; j < 8; j ++) { switch(j) { case 7: ByteBit = sum & 0x01; break; case 6: ByteBit = (sum & 0x02) / 0x02 ; break; case 5: ByteBit = (sum & 0x04) / 0x04 ; break; case 4: ByteBit = (sum & 0x08) / 0x08 ; break; case 3: ByteBit = (sum & 0x10) / 0x10 ; break; case 2: ByteBit = (sum & 0x20) / 0x20 ; break; case 1: ByteBit = (sum & 0x40) / 0x40 ; break; case 0: ByteBit = (sum & 0x80) / 0x80; break; } szDest[j] = ByteBit + 48; } strBit += szDest; } strBit = strBit.MakeReverse(); szRecv[0] = strBit.GetAt(StaBit); } if (tmp_Sour != NULL) { delete[] tmp_Sour; tmp_Sour = NULL; } } void canatal::SimulationCommData(int nAddr) { //~10012A0020680033335F439A1963439A99644366665C4333335A4333335D433333B33F0000C03FCDCC8C3F33B3DA43CDCC474200000033B3DA43E71D. memcpy(m_sz00Msg[nAddr -1], "~10012A0020680033335F439A1963439A99644366665C4333335A4333335D433333B33F0000C03FCDCC8C3F33B3DA43CDCC474200000033B3DA43E71D.", sizeof("~10012A0020680033335F439A1963439A99644366665C4333335A4333335D433333B33F0000C03FCDCC8C3F33B3DA43CDCC474200000033B3DA43E71D.")); //~10012A00307600000E66A6C2439A19C5439A59C3436666663FCDCC4C3F0000803FC3F547423333333FA4703D3F1F852B3F9A995F43CD4C614333B35E4348E14742E3B1. // memcpy(m_sz03Msg[nAddr -1], "~10012A00307600000E66A6C2439A19C5439A59C3436666663FCDCC4C3F0000803FC3F547423333333FA4703D3F1F852B3F9A995F43CD4C614333B35E4348E14742E3B1.", sizeof("~10012A00307600000E66A6C2439A19C5439A59C3436666663FCDCC4C3F0000803FC3F547423333333FA4703D3F1F852B3F9A995F43CD4C614333B35E4348E14742E3B1.")); } /////////