| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409 |
- #include "StdAfx.h"
- #include ".\checkprocess.h"
- #include "CharacterProcess.h"
- CCheckProcess::CCheckProcess(void)
- {
- }
- CCheckProcess::~CCheckProcess(void)
- {
- }
- BOOL CCheckProcess::GetCheck(const char *szInput,char *szOutput, const int &nInputLen, int &nOutputLen, const char *szCheckType)
- {
- return TRUE;
- }
- BOOL CCheckProcess::GetCheck(const char *szInput,char *szOutput, const int &nInputLen, int &nOutputLen, const DWORD &nCheckType)
- {
- switch ( nCheckType )
- {
- case 1:
- //GetCheck_Rtu(szInput,szOutput,nInputLen,nOutputLen);
- break;
- case 2:
- //GetCheck_Emerson(szInput,szOutput,nInputLen,nOutputLen);
- break;
- }
- return TRUE;
- }
- #if XMLCHECK
- void CCheckProcess::GetXmlCheck_Rtu(const char *szInput,char *szOutput, int nInputLen/*, int &nOutputLen*/)
- {
- char szCommand[255] = {0};
- nInputLen = CCharacterProcess::ASCIIStr_to_HexStr(szInput,szCommand,nInputLen);
- unsigned int Genpoly=0xA001;
- unsigned int CRC=0xFFFF;
- unsigned int index;
- int i = 0;
- while(nInputLen--)
- {
- CRC=CRC^(unsigned int)szCommand[i++];
- for(index=0;index<8;index++)
- {
- if((CRC & 0x0001)==1) CRC=(CRC>>1)^Genpoly;
- else (CRC=CRC>>1);
- }
- }
- BYTE high = CRC>>8&0xff;
- BYTE low = CRC&0xff;
- memset(szOutput, 0, strlen(szOutput));
- char ch[3] = {0};
- memset(ch, 0, 3);
- itoa(static_cast<int>(low), ch, 16);
- if ( low < 10)
- {
- szOutput[0] = '0';
- szOutput[1] = ch[0];
- }
- else
- {
- szOutput[0] = ch[0];
- szOutput[1] = ch[1];
- }
- szOutput[2] = 32;
- memset(ch, 0, 3);
- itoa(static_cast<int>(high), ch, 16);
- if ( high < 10)
- {
- szOutput[3] = '0';
- szOutput[4] = ch[0];
- }
- else
- {
- szOutput[3] = ch[0];
- szOutput[4] = ch[1];
- }
- for (int i(0); i < 5 ; i++)
- CCharacterProcess::lowercase2uppercase_s(szOutput[i]);
- }
- void CCheckProcess::GetXmlCheck_Emerson(const char *szInput,char *szOutput, int nInputLen/*, int &nOutputLen*/)
- {
- char szCommand[255] = {0};
- nInputLen = CCharacterProcess::ASCIIStr_to_HexStr(szInput,szCommand,nInputLen);
- WORD dwSum(0);
- for (int i(0); i < nInputLen; i++)
- dwSum += szCommand[i+1] ;
- WORD iCompliment = dwSum;
- iCompliment = ~iCompliment;//取反
- iCompliment++;
- char szDest[5] = {0};
- itoa(iCompliment, szDest, 16);
- for (int i(0); i < 4 ; i++)
- CCharacterProcess::lowercase2uppercase_s(szDest[i]);
- memset(szOutput, 0, strlen(szOutput));
- char ch[3] = {0};
- for (int i = 0, j = 0; i < 4; i++, j=j+3)
- {
- memset(ch, 0, 3);
- itoa(static_cast<int>(szDest[i]), ch, 16);
- szOutput[j] = ch[0];
- szOutput[j + 1] = ch[1];
- if ( i != 3)
- szOutput[j + 2] = 32;
- }
- }
- void CCheckProcess::GetXmlCheck_fad(const char *szInput,char *szOutput, int nInputLen)
- {
- char szCommand[255] = {0};
- nInputLen = CCharacterProcess::ASCIIStr_to_HexStr(szInput,szCommand,nInputLen);
- // 任何数与0异或,等于本身;
- BYTE bySum(0);
- for (int i(1); i < nInputLen; i++)
- bySum ^= szCommand[i];
- bySum = bySum & 0xfe;
- memset(szOutput, 0, strlen(szOutput));
- CCharacterProcess::Byte_to_HexASCII( bySum,szOutput );
- //szOutput[0] = bySum;
- }
- void CCheckProcess::GetXmlCheck_newave( const char *szInput,char *szOutput, int nInputLen )
- {
- char szCommand[255] = {0};
- nInputLen = CCharacterProcess::ASCIIStr_to_HexStr(szInput,szCommand,nInputLen);
- WORD dwSum(0);
- for (int i(0); i < nInputLen; i++)
- dwSum += szCommand[i] ;
- CString strSum;
- strSum.Format("%00004X",dwSum);
- memset(szOutput, 0, strlen(szOutput));
- int nstrlen = strSum.GetLength();
- char ch[3] = {0};
- for (int i = 0, j = 0; i < 4; i++, j=j+3)
- {
- memset(ch, 0, 3);
- itoa(static_cast<int>(strSum.GetAt(i)), ch, 16);
- szOutput[j] = ch[0];
- szOutput[j + 1] = ch[1];
- if ( i != 3)
- szOutput[j + 2] = 32;
- }
- }
- void CCheckProcess::GetXmlCheck_stulz6000( const char *szInput,char *szOutput, int nInputLen )
- {
- char szCommand[255] = {0};
- nInputLen = CCharacterProcess::ASCIIStr_to_HexStr(szInput,szCommand,nInputLen);
- WORD dwSum(0);
- for (int i(0); i < nInputLen; i++)
- dwSum += szCommand[i] ;
- WORD iCompliment = dwSum;
- iCompliment = ~iCompliment;//取反
- iCompliment++;
- memset(szOutput, 0, strlen(szOutput));
- char ch[5] = {0};
- itoa(iCompliment, ch, 16);
- for (int i(0); i < 4 ; i++)
- CCharacterProcess::lowercase2uppercase_s(ch[i]);
- szOutput[0] = ch[2];
- szOutput[1] = ch[3];
- szOutput[2] = 32;
- szOutput[3] = 'F';
- szOutput[4] = 'F';
-
- }
- void CCheckProcess::GetXmlCheck_CM( const char *szInput,char *szOutput, int nInputLen )
- {
- char szCommand[255] = {0};
- nInputLen = CCharacterProcess::ASCIIStr_to_HexStr(szInput,szCommand,nInputLen);
- WORD dwSum(0);
- for (int i(0); i < nInputLen; i++)
- dwSum += szCommand[i] ;
- dwSum &= 0xFF;
- memset(szOutput, 0, strlen(szOutput));
- szOutput[0] = CCharacterProcess::Byte_to_ASCII((dwSum >> 4) & 0x0F);
- szOutput[1] = CCharacterProcess::Byte_to_ASCII( dwSum & 0x0F);
- }
- #endif
- #if RETURNCHECK
- nErrorCode CCheckProcess::GetReturnCheck_Rtu(const BYTE *szInput, const int &nInputLen)
- {
- if( nInputLen == 0)
- return nEr_Nodata;
- // 第2字节;
- if( szInput[1] < 0x80 && szInput[1] - 0x80 > 0 )
- return nEr_InvalidFunc;
- unsigned int Genpoly=0xA001;
- unsigned int CRC=0xFFFF;
- unsigned int index;
- int i = 0;
- int len = nInputLen - 2;
- while(len--)
- {
- CRC=CRC^(unsigned int)szInput[i++];
- for(index=0;index<8;index++)
- {
- if((CRC & 0x0001)==1) CRC=(CRC>>1)^Genpoly;
- else (CRC=CRC>>1);
- }
- }
- BYTE high = CRC>>8&0xff;
- BYTE low = CRC&0xff;
- if ( low != szInput[nInputLen-2] || high != szInput[nInputLen-1] )
- return nEr_InvalidData;
- return nEr_Normal;
- }
- nErrorCode CCheckProcess::GetReturnCheck_Emerson(const BYTE *szInput,const int &nInputLen)
- {
- // BYTE szInput[1024];
- // int nInputLen = strlen("~11012A00E02000010CE1E0E1E0E1E1E0E1E1E0E0E000F67D.") - 1;
- // memcpy(szInput,"~11012A00E02000010CE1E0E1E0E1E1E0E1E1E0E0E000F67D.",nInputLen);
- LOG4C((LOG_NOTICE,"长度%d,%s",nInputLen,szInput));
- if( nInputLen == 0)
- return nEr_Nodata;
- int len = nInputLen - 6;
- //int len = nInputLen - 4;
- WORD dwSum(0);
- for (int i(0); i < len; i++)
- dwSum += szInput[i+1] ;
- WORD iCompliment = dwSum;
- iCompliment = ~iCompliment;//取反
- iCompliment++;
- char szDest[5] = {0};
- itoa(iCompliment, szDest, 16);
- LOG4C((LOG_NOTICE,"校验1:%s",szDest));
- for (int i(0); i < 4 ; i++)
- CCharacterProcess::lowercase2uppercase_s(szDest[i]);
- LOG4C((LOG_NOTICE,"校验:%s",szDest));
- char szCheck[5] = {0};
- memcpy(szCheck,szInput+nInputLen-5,4);
- if( strcmp(szCheck,szDest) != 0)
- {
- LOG4C((LOG_NOTICE,"校验不等%s,%s",szDest,szCheck));
- return nEr_InvalidData;
- }
- return nEr_Normal;
- }
- /************************************************************************/
- /*
- 函数:富安达气体设备校验
- 校验方式:所有字节(就4字节)相异或后,再将第8bit位 [置0] 后的值,范围为0-0X7F
- */
- /************************************************************************/
- nErrorCode CCheckProcess::GetReturnCheck_fad(const BYTE *szInput1,const int &nInputLen1)
- {
- BYTE szInput[20] = {0xFF, 0x01, 0x33, 0x00, 0x00, 0x32};
- int nInputLen = 6;
- if( nInputLen == 0)
- return nEr_Nodata;
- // 任何数与0异或,等于本身;
- BYTE bySum(0);
- int len = nInputLen -1;
- for (int i(1); i < len; i++)
- bySum ^= szInput[i];
- bySum = bySum & 0xfe;
- if ( bySum != szInput[nInputLen-1])
- return nEr_InvalidData;
- return nEr_Normal;
- }
- /************************************************************************/
- /*
- 函数:威能UPS校验;
- 校验方式:从头开始的所有字节和(校验码本身不包括),以4字节形式表示;
- */
- /************************************************************************/
- nErrorCode CCheckProcess::GetReturnCheck_newave(const BYTE *szInput,const int &nInputLen)
- {
- //char szInput[256] = {0x3E, 0x30, 0x31, 0x47, 0x30, 0x30, 0x38, 0x33, 0x30, 0x32, 0x34, 0x30, 0x32, 0x34, 0x37};
- //int nInputLen = strlen(szInput) ;//strlen有时会不对的,sizeof也是一样;
- if( nInputLen == 0)
- return nEr_Nodata;
- WORD dwSum(0);
- int len = nInputLen - 4;
- for (int i(0); i < len; i++)
- dwSum += szInput[i] ;
- CString strSum;
- strSum.Format("%00004X",dwSum);
- char szCheck[5]={0};
- memcpy(szCheck,szInput+nInputLen-4,4);
- if ( strSum.Compare(szCheck) != 0)
- return nEr_InvalidData;
- return nEr_Normal;
- }
- /************************************************************************/
- /*
- 函数:stulz6000空调校验;
- 校验方式:所有字节求和,以2字节的 [高字节|低字节] 形式表示;
- */
- /************************************************************************/
- nErrorCode CCheckProcess::GetReturnCheck_stulz6000(const BYTE *szInput,const int &nInputLen)
- {
- //BYTE szInput[20] = {0x03, 0x01, 0x02, 0xFA, 0xFF};
- //int nInputLen = strlen((char*)szInput) ;
- if( nInputLen == 0)
- return nEr_Nodata;
- WORD dwSum(0);
- int len = nInputLen - 2;
- for (int i(0); i < len; i++)
- dwSum += szInput[i] ;
- WORD iCompliment = dwSum;
- iCompliment = ~iCompliment;//取反
- iCompliment++;
- BYTE high = iCompliment>>8 & 0xff;
- BYTE low = iCompliment & 0xff;
- if ( szInput[nInputLen-2] != low || szInput[nInputLen-1] != high)
- return nEr_InvalidData;
- return nEr_Normal;
- }
- /************************************************************************/
- /*
- 函数:stulz6000空调校验;
- 校验方式:所有字节求和,以2字节的 [高字节|低字节] 形式表示;
- */
- /************************************************************************/
- nErrorCode CCheckProcess::GetReturnCheck_CM(const BYTE *szInput,const int &nInputLen)
- {
- //BYTE szInput[20] = {0x03, 0x01, 0x02, 0xFA, 0xFF};
- //int nInputLen = strlen((char*)szInput) ;
- if( nInputLen == 0)
- return nEr_Nodata;
- WORD dwSum(0);
- int len = nInputLen - 1;
- for (int i(0); i < len; i++)
- dwSum += szInput[i] ;
- dwSum &= 0xFF;
- if ( szInput[nInputLen-1] != dwSum )
- return nEr_InvalidData;
- return nEr_Normal;
- }
- #endif
|