CheckProcess.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. #include "StdAfx.h"
  2. #include ".\checkprocess.h"
  3. #include "CharacterProcess.h"
  4. CCheckProcess::CCheckProcess(void)
  5. {
  6. }
  7. CCheckProcess::~CCheckProcess(void)
  8. {
  9. }
  10. BOOL CCheckProcess::GetCheck(const char *szInput,char *szOutput, const int &nInputLen, int &nOutputLen, const char *szCheckType)
  11. {
  12. return TRUE;
  13. }
  14. BOOL CCheckProcess::GetCheck(const char *szInput,char *szOutput, const int &nInputLen, int &nOutputLen, const DWORD &nCheckType)
  15. {
  16. switch ( nCheckType )
  17. {
  18. case 1:
  19. //GetCheck_Rtu(szInput,szOutput,nInputLen,nOutputLen);
  20. break;
  21. case 2:
  22. //GetCheck_Emerson(szInput,szOutput,nInputLen,nOutputLen);
  23. break;
  24. }
  25. return TRUE;
  26. }
  27. #if XMLCHECK
  28. void CCheckProcess::GetXmlCheck_Rtu(const char *szInput,char *szOutput, int nInputLen/*, int &nOutputLen*/)
  29. {
  30. char szCommand[255] = {0};
  31. nInputLen = CCharacterProcess::ASCIIStr_to_HexStr(szInput,szCommand,nInputLen);
  32. unsigned int Genpoly=0xA001;
  33. unsigned int CRC=0xFFFF;
  34. unsigned int index;
  35. int i = 0;
  36. while(nInputLen--)
  37. {
  38. CRC=CRC^(unsigned int)szCommand[i++];
  39. for(index=0;index<8;index++)
  40. {
  41. if((CRC & 0x0001)==1) CRC=(CRC>>1)^Genpoly;
  42. else (CRC=CRC>>1);
  43. }
  44. }
  45. BYTE high = CRC>>8&0xff;
  46. BYTE low = CRC&0xff;
  47. memset(szOutput, 0, strlen(szOutput));
  48. char ch[3] = {0};
  49. memset(ch, 0, 3);
  50. itoa(static_cast<int>(low), ch, 16);
  51. if ( low < 10)
  52. {
  53. szOutput[0] = '0';
  54. szOutput[1] = ch[0];
  55. }
  56. else
  57. {
  58. szOutput[0] = ch[0];
  59. szOutput[1] = ch[1];
  60. }
  61. szOutput[2] = 32;
  62. memset(ch, 0, 3);
  63. itoa(static_cast<int>(high), ch, 16);
  64. if ( high < 10)
  65. {
  66. szOutput[3] = '0';
  67. szOutput[4] = ch[0];
  68. }
  69. else
  70. {
  71. szOutput[3] = ch[0];
  72. szOutput[4] = ch[1];
  73. }
  74. for (int i(0); i < 5 ; i++)
  75. CCharacterProcess::lowercase2uppercase_s(szOutput[i]);
  76. }
  77. void CCheckProcess::GetXmlCheck_Emerson(const char *szInput,char *szOutput, int nInputLen/*, int &nOutputLen*/)
  78. {
  79. char szCommand[255] = {0};
  80. nInputLen = CCharacterProcess::ASCIIStr_to_HexStr(szInput,szCommand,nInputLen);
  81. WORD dwSum(0);
  82. for (int i(0); i < nInputLen; i++)
  83. dwSum += szCommand[i+1] ;
  84. WORD iCompliment = dwSum;
  85. iCompliment = ~iCompliment;//取反
  86. iCompliment++;
  87. char szDest[5] = {0};
  88. itoa(iCompliment, szDest, 16);
  89. for (int i(0); i < 4 ; i++)
  90. CCharacterProcess::lowercase2uppercase_s(szDest[i]);
  91. memset(szOutput, 0, strlen(szOutput));
  92. char ch[3] = {0};
  93. for (int i = 0, j = 0; i < 4; i++, j=j+3)
  94. {
  95. memset(ch, 0, 3);
  96. itoa(static_cast<int>(szDest[i]), ch, 16);
  97. szOutput[j] = ch[0];
  98. szOutput[j + 1] = ch[1];
  99. if ( i != 3)
  100. szOutput[j + 2] = 32;
  101. }
  102. }
  103. void CCheckProcess::GetXmlCheck_fad(const char *szInput,char *szOutput, int nInputLen)
  104. {
  105. char szCommand[255] = {0};
  106. nInputLen = CCharacterProcess::ASCIIStr_to_HexStr(szInput,szCommand,nInputLen);
  107. // 任何数与0异或,等于本身;
  108. BYTE bySum(0);
  109. for (int i(1); i < nInputLen; i++)
  110. bySum ^= szCommand[i];
  111. bySum = bySum & 0xfe;
  112. memset(szOutput, 0, strlen(szOutput));
  113. CCharacterProcess::Byte_to_HexASCII( bySum,szOutput );
  114. //szOutput[0] = bySum;
  115. }
  116. void CCheckProcess::GetXmlCheck_newave( const char *szInput,char *szOutput, int nInputLen )
  117. {
  118. char szCommand[255] = {0};
  119. nInputLen = CCharacterProcess::ASCIIStr_to_HexStr(szInput,szCommand,nInputLen);
  120. WORD dwSum(0);
  121. for (int i(0); i < nInputLen; i++)
  122. dwSum += szCommand[i] ;
  123. CString strSum;
  124. strSum.Format("%00004X",dwSum);
  125. memset(szOutput, 0, strlen(szOutput));
  126. int nstrlen = strSum.GetLength();
  127. char ch[3] = {0};
  128. for (int i = 0, j = 0; i < 4; i++, j=j+3)
  129. {
  130. memset(ch, 0, 3);
  131. itoa(static_cast<int>(strSum.GetAt(i)), ch, 16);
  132. szOutput[j] = ch[0];
  133. szOutput[j + 1] = ch[1];
  134. if ( i != 3)
  135. szOutput[j + 2] = 32;
  136. }
  137. }
  138. void CCheckProcess::GetXmlCheck_stulz6000( const char *szInput,char *szOutput, int nInputLen )
  139. {
  140. char szCommand[255] = {0};
  141. nInputLen = CCharacterProcess::ASCIIStr_to_HexStr(szInput,szCommand,nInputLen);
  142. WORD dwSum(0);
  143. for (int i(0); i < nInputLen; i++)
  144. dwSum += szCommand[i] ;
  145. WORD iCompliment = dwSum;
  146. iCompliment = ~iCompliment;//取反
  147. iCompliment++;
  148. memset(szOutput, 0, strlen(szOutput));
  149. char ch[5] = {0};
  150. itoa(iCompliment, ch, 16);
  151. for (int i(0); i < 4 ; i++)
  152. CCharacterProcess::lowercase2uppercase_s(ch[i]);
  153. szOutput[0] = ch[2];
  154. szOutput[1] = ch[3];
  155. szOutput[2] = 32;
  156. szOutput[3] = 'F';
  157. szOutput[4] = 'F';
  158. }
  159. void CCheckProcess::GetXmlCheck_CM( const char *szInput,char *szOutput, int nInputLen )
  160. {
  161. char szCommand[255] = {0};
  162. nInputLen = CCharacterProcess::ASCIIStr_to_HexStr(szInput,szCommand,nInputLen);
  163. WORD dwSum(0);
  164. for (int i(0); i < nInputLen; i++)
  165. dwSum += szCommand[i] ;
  166. dwSum &= 0xFF;
  167. memset(szOutput, 0, strlen(szOutput));
  168. szOutput[0] = CCharacterProcess::Byte_to_ASCII((dwSum >> 4) & 0x0F);
  169. szOutput[1] = CCharacterProcess::Byte_to_ASCII( dwSum & 0x0F);
  170. }
  171. #endif
  172. #if RETURNCHECK
  173. nErrorCode CCheckProcess::GetReturnCheck_Rtu(const BYTE *szInput, const int &nInputLen)
  174. {
  175. if( nInputLen == 0)
  176. return nEr_Nodata;
  177. // 第2字节;
  178. if( szInput[1] < 0x80 && szInput[1] - 0x80 > 0 )
  179. return nEr_InvalidFunc;
  180. unsigned int Genpoly=0xA001;
  181. unsigned int CRC=0xFFFF;
  182. unsigned int index;
  183. int i = 0;
  184. int len = nInputLen - 2;
  185. while(len--)
  186. {
  187. CRC=CRC^(unsigned int)szInput[i++];
  188. for(index=0;index<8;index++)
  189. {
  190. if((CRC & 0x0001)==1) CRC=(CRC>>1)^Genpoly;
  191. else (CRC=CRC>>1);
  192. }
  193. }
  194. BYTE high = CRC>>8&0xff;
  195. BYTE low = CRC&0xff;
  196. if ( low != szInput[nInputLen-2] || high != szInput[nInputLen-1] )
  197. return nEr_InvalidData;
  198. return nEr_Normal;
  199. }
  200. nErrorCode CCheckProcess::GetReturnCheck_Emerson(const BYTE *szInput,const int &nInputLen)
  201. {
  202. // BYTE szInput[1024];
  203. // int nInputLen = strlen("~11012A00E02000010CE1E0E1E0E1E1E0E1E1E0E0E000F67D.") - 1;
  204. // memcpy(szInput,"~11012A00E02000010CE1E0E1E0E1E1E0E1E1E0E0E000F67D.",nInputLen);
  205. LOG4C((LOG_NOTICE,"长度%d,%s",nInputLen,szInput));
  206. if( nInputLen == 0)
  207. return nEr_Nodata;
  208. int len = nInputLen - 6;
  209. //int len = nInputLen - 4;
  210. WORD dwSum(0);
  211. for (int i(0); i < len; i++)
  212. dwSum += szInput[i+1] ;
  213. WORD iCompliment = dwSum;
  214. iCompliment = ~iCompliment;//取反
  215. iCompliment++;
  216. char szDest[5] = {0};
  217. itoa(iCompliment, szDest, 16);
  218. LOG4C((LOG_NOTICE,"校验1:%s",szDest));
  219. for (int i(0); i < 4 ; i++)
  220. CCharacterProcess::lowercase2uppercase_s(szDest[i]);
  221. LOG4C((LOG_NOTICE,"校验:%s",szDest));
  222. char szCheck[5] = {0};
  223. memcpy(szCheck,szInput+nInputLen-5,4);
  224. if( strcmp(szCheck,szDest) != 0)
  225. {
  226. LOG4C((LOG_NOTICE,"校验不等%s,%s",szDest,szCheck));
  227. return nEr_InvalidData;
  228. }
  229. return nEr_Normal;
  230. }
  231. /************************************************************************/
  232. /*
  233. 函数:富安达气体设备校验
  234. 校验方式:所有字节(就4字节)相异或后,再将第8bit位 [置0] 后的值,范围为0-0X7F
  235. */
  236. /************************************************************************/
  237. nErrorCode CCheckProcess::GetReturnCheck_fad(const BYTE *szInput1,const int &nInputLen1)
  238. {
  239. BYTE szInput[20] = {0xFF, 0x01, 0x33, 0x00, 0x00, 0x32};
  240. int nInputLen = 6;
  241. if( nInputLen == 0)
  242. return nEr_Nodata;
  243. // 任何数与0异或,等于本身;
  244. BYTE bySum(0);
  245. int len = nInputLen -1;
  246. for (int i(1); i < len; i++)
  247. bySum ^= szInput[i];
  248. bySum = bySum & 0xfe;
  249. if ( bySum != szInput[nInputLen-1])
  250. return nEr_InvalidData;
  251. return nEr_Normal;
  252. }
  253. /************************************************************************/
  254. /*
  255. 函数:威能UPS校验;
  256. 校验方式:从头开始的所有字节和(校验码本身不包括),以4字节形式表示;
  257. */
  258. /************************************************************************/
  259. nErrorCode CCheckProcess::GetReturnCheck_newave(const BYTE *szInput,const int &nInputLen)
  260. {
  261. //char szInput[256] = {0x3E, 0x30, 0x31, 0x47, 0x30, 0x30, 0x38, 0x33, 0x30, 0x32, 0x34, 0x30, 0x32, 0x34, 0x37};
  262. //int nInputLen = strlen(szInput) ;//strlen有时会不对的,sizeof也是一样;
  263. if( nInputLen == 0)
  264. return nEr_Nodata;
  265. WORD dwSum(0);
  266. int len = nInputLen - 4;
  267. for (int i(0); i < len; i++)
  268. dwSum += szInput[i] ;
  269. CString strSum;
  270. strSum.Format("%00004X",dwSum);
  271. char szCheck[5]={0};
  272. memcpy(szCheck,szInput+nInputLen-4,4);
  273. if ( strSum.Compare(szCheck) != 0)
  274. return nEr_InvalidData;
  275. return nEr_Normal;
  276. }
  277. /************************************************************************/
  278. /*
  279. 函数:stulz6000空调校验;
  280. 校验方式:所有字节求和,以2字节的 [高字节|低字节] 形式表示;
  281. */
  282. /************************************************************************/
  283. nErrorCode CCheckProcess::GetReturnCheck_stulz6000(const BYTE *szInput,const int &nInputLen)
  284. {
  285. //BYTE szInput[20] = {0x03, 0x01, 0x02, 0xFA, 0xFF};
  286. //int nInputLen = strlen((char*)szInput) ;
  287. if( nInputLen == 0)
  288. return nEr_Nodata;
  289. WORD dwSum(0);
  290. int len = nInputLen - 2;
  291. for (int i(0); i < len; i++)
  292. dwSum += szInput[i] ;
  293. WORD iCompliment = dwSum;
  294. iCompliment = ~iCompliment;//取反
  295. iCompliment++;
  296. BYTE high = iCompliment>>8 & 0xff;
  297. BYTE low = iCompliment & 0xff;
  298. if ( szInput[nInputLen-2] != low || szInput[nInputLen-1] != high)
  299. return nEr_InvalidData;
  300. return nEr_Normal;
  301. }
  302. /************************************************************************/
  303. /*
  304. 函数:stulz6000空调校验;
  305. 校验方式:所有字节求和,以2字节的 [高字节|低字节] 形式表示;
  306. */
  307. /************************************************************************/
  308. nErrorCode CCheckProcess::GetReturnCheck_CM(const BYTE *szInput,const int &nInputLen)
  309. {
  310. //BYTE szInput[20] = {0x03, 0x01, 0x02, 0xFA, 0xFF};
  311. //int nInputLen = strlen((char*)szInput) ;
  312. if( nInputLen == 0)
  313. return nEr_Nodata;
  314. WORD dwSum(0);
  315. int len = nInputLen - 1;
  316. for (int i(0); i < len; i++)
  317. dwSum += szInput[i] ;
  318. dwSum &= 0xFF;
  319. if ( szInput[nInputLen-1] != dwSum )
  320. return nEr_InvalidData;
  321. return nEr_Normal;
  322. }
  323. #endif