Global.cpp 20 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  1. //////////////////////////////////////////////////////////////////////////////
  2. ////// //////
  3. ////// 文 件: Global.cpp //////
  4. ////// 作 者: jesse //////
  5. ////// 创建时间: 2010-04-15 //////
  6. ////// 说 明: 定义全局变量,全局函数 //////
  7. ////// //////
  8. ////// 修改时间: //////
  9. ////// 修改说明: //////
  10. ////// //////
  11. //////////////////////////////////////////////////////////////////////////////
  12. #pragma once
  13. #include "stdafx.h"
  14. #include "Global.h"
  15. #include <cmath>
  16. #include <bitset>
  17. INT g_nPrecision = 4;
  18. /*字符大小写互换*/
  19. void UpperLowerSwap(char *szMsg)
  20. {
  21. for (int i=0; i< (int)strlen(szMsg); i++)
  22. {
  23. if ((szMsg[i]>='A') && (szMsg[i]<='Z'))
  24. {
  25. szMsg[i] = szMsg[i] + 32;
  26. }
  27. else if ((szMsg[i]>='a') && (szMsg[i]<='z'))
  28. {
  29. szMsg[i] = szMsg[i] - 32;
  30. }
  31. else
  32. {
  33. szMsg[i] = szMsg[i];
  34. }
  35. }
  36. }
  37. /*字符全转为大写*/
  38. void ByteToUpper(char *szMsg)
  39. {
  40. for (int i=0; i < static_cast<int>(strlen(szMsg)); i++)
  41. {
  42. if ((szMsg[i]>='a') && (szMsg[i]<='z'))
  43. {
  44. szMsg[i] = szMsg[i] - 32;
  45. }
  46. else
  47. {
  48. szMsg[i] = szMsg[i];
  49. }
  50. }
  51. }
  52. /*字符全转为小写*/
  53. void ByteToLower(char *szMsg)
  54. {
  55. for (int i=0; i < static_cast<int>(strlen(szMsg)); i++)
  56. {
  57. if ((szMsg[i]>='A') && (szMsg[i]<='Z'))
  58. {
  59. szMsg[i] = szMsg[i] + 32;
  60. }
  61. else
  62. {
  63. szMsg[i] = szMsg[i];
  64. }
  65. }
  66. }
  67. /*一串字符的每个字节转换为两个字节*/
  68. //szMsg, 要转换的消息
  69. //szConvMsg 转换后的消息
  70. void ByteToTwoByte( char *szMsg,char *szConvMsg)
  71. {
  72. char ch[3];
  73. for (int i=0; i < static_cast<int>(strlen(szMsg)); i++)
  74. {
  75. memset(ch, 0, 3);
  76. //itoa(int(szMsg[i]), ch,16);
  77. itoa(static_cast<int>(szMsg[i]), ch, 16);
  78. szConvMsg[i*2] = ch[0];
  79. szConvMsg[i*2 + 1] = ch[1];
  80. }
  81. }
  82. /*一串字符的每两个字节转换为一个字节*/
  83. void TwoByteToByte(
  84. char *szMsg, /*要转换的消息*/
  85. char *szConvMsg /*转换后的消息*/
  86. )
  87. {
  88. for (int i=0; i< static_cast<int>(strlen(szMsg)/2); i++)
  89. {
  90. szConvMsg[i] = szMsg[i*2] << 4 | szMsg[i*2 + 1];
  91. }
  92. }
  93. /*十六进制Ascii字符转整数*/
  94. INT AsciiToBYTE(char szMsg)
  95. {
  96. int iConvMsg = static_cast<int>(szMsg);
  97. //iConvMsg = (int)szMsg;
  98. if ((szMsg >= 'A')&&(szMsg <= 'F'))
  99. {
  100. iConvMsg = iConvMsg - 'A' + 10;
  101. }
  102. else if ((szMsg >= 'a')&&(szMsg <= 'f'))
  103. {
  104. iConvMsg = iConvMsg - 'a' + 10;
  105. }
  106. else if ((szMsg >= '0')&&(szMsg <= '9'))
  107. {
  108. iConvMsg -= '0';
  109. }
  110. return iConvMsg;
  111. }
  112. /*获得chksum*/
  113. void GetChkSum(
  114. char *szMsg, /*要转换的消息*/
  115. char *szConvMsg, /*转换后的消息*/
  116. int len
  117. )
  118. {
  119. unsigned short iMsg;
  120. iMsg = 0;
  121. for (int i=1;i<=(len-6);i++)
  122. {
  123. iMsg += szMsg[i]; //ASSIC码值相加
  124. }
  125. iMsg = iMsg%65536;
  126. iMsg = ~iMsg + 1;
  127. itoa(iMsg,szConvMsg,16);
  128. ByteToUpper(szConvMsg);
  129. }
  130. /*电总协议chksum校验*/
  131. bool ChkSumCheck(
  132. char *szMsg, /*要转换的消息*/
  133. int len
  134. )
  135. {
  136. char szConvMsg[5]; /*转换后的消息*/
  137. char ChkSum[5];
  138. int j = 0;
  139. memset(szConvMsg, 0, 5);
  140. memset(ChkSum, 0, 5);
  141. for (int i=(len-5); i<(len-1); i++)
  142. {
  143. ChkSum[j] = szMsg[i];
  144. j++;
  145. }
  146. GetChkSum(szMsg, szConvMsg, len);
  147. ByteToUpper(szConvMsg);
  148. ByteToUpper(ChkSum);
  149. if (memcmp(szConvMsg, ChkSum, 4) == 0)
  150. {
  151. return true;
  152. }
  153. else
  154. {
  155. return false;
  156. }
  157. }
  158. /*电总协议Length校验*/
  159. bool CheckLength( char *szMsg )
  160. {
  161. //char *szConvMsg;
  162. WORD len ,index , lenID;
  163. len = 4;
  164. index = 9;
  165. char ch[4];
  166. for (int i=0;i<len;i++)
  167. {
  168. ch[i] = szMsg[index+i];
  169. }
  170. lenID = AsciiToBYTE(ch[1]) + AsciiToBYTE(ch[2]) + AsciiToBYTE(ch[3]);
  171. lenID = lenID%16;
  172. lenID = ~lenID + 1;
  173. lenID = lenID & 15;
  174. if (AsciiToBYTE(ch[0]) == lenID)
  175. {
  176. return true;
  177. }
  178. else
  179. {
  180. return false;
  181. }
  182. }
  183. /*字符转ASCII*/
  184. char ByteToAscii(BYTE btSrc)
  185. {
  186. char chDest;
  187. if( btSrc < 10 )
  188. {
  189. chDest = static_cast<char>(btSrc % 10 + '0');
  190. chDest = lowercase2uppercase(chDest);
  191. return chDest;
  192. }
  193. else
  194. {
  195. chDest = ByteToAscii( btSrc / 10 ) + static_cast<char>( btSrc % 10 + '0' );
  196. chDest = lowercase2uppercase(chDest);
  197. return chDest;
  198. }
  199. }
  200. char lowercase2uppercase(BYTE btSrc)
  201. {
  202. if( btSrc >= 'a' && btSrc <= 'z' )
  203. {
  204. return btSrc - 'a' + 'A';
  205. }
  206. return btSrc;
  207. }
  208. INT DataConversion(char *szType, char *szMsg, char *szConvMsg, int len, int StaBit, int EndBit)
  209. {
  210. if ( strcmp(szType, "FLOAT") == 0)
  211. {
  212. TypeIsFLOAT(szMsg, szConvMsg);
  213. }
  214. else if( strcmp(szType, "WORD") == 0)
  215. {
  216. TypeIsWORD(szMsg, szConvMsg);
  217. }
  218. else if( strcmp(szType, "SHORT") == 0)
  219. {
  220. TypeIsSHORT(szMsg, szConvMsg);
  221. }
  222. else if ( strcmp(szType, "BYTE") == 0 )
  223. {
  224. TypeIsBYTE(reinterpret_cast<BYTE *>(szMsg), reinterpret_cast<BYTE *>(szConvMsg), len);
  225. }
  226. else if ( strcmp(szType, "CHAR") == 0 )
  227. {
  228. TypeIsCHAR(szMsg, szConvMsg, len);
  229. }
  230. else if( strcmp(szType, "BIT") == 0)
  231. {
  232. TypeIsBIT(szMsg, szConvMsg, len, StaBit, EndBit);
  233. }
  234. else if (strcmp(szType, "UINT") == 0)
  235. {
  236. TypeIsUINT(szMsg, szConvMsg, len);
  237. }
  238. else if (strcmp(szType, "ONEBYTE") == 0)
  239. {
  240. TypeIsONEBYTE(szMsg, szConvMsg, len);
  241. }
  242. else if ( strcmp(szType,"DL") == 0)
  243. {
  244. _BYTE_1_Decimal(szConvMsg,(BYTE*)szMsg);
  245. }
  246. else if ( strcmp(szType,"WBIT") == 0)
  247. {
  248. int nSour = szMsg[0]; //ASCII_to_Byte(szMsg[0]) *16 + ASCII_to_Byte(szMsg[1]);
  249. GetWORDBit(szConvMsg,nSour,StaBit,EndBit);
  250. }
  251. return 0;
  252. }
  253. //将32 33 37 2E 30 转换为237.0
  254. INT TypeIsUINT(char *szMsg, char *szConvMsg, int len)
  255. {
  256. char strBuf[10] = {0};
  257. //memcpy(szConvMsg, szMsg, len);
  258. sprintf(szConvMsg,"%lf",atof(szMsg));
  259. return 0;
  260. }
  261. //一个字节 0x31转换为1
  262. INT TypeIsONEBYTE(char *szMsg, char *szConvMsg, int len)
  263. {
  264. char strBuf[5] = {0};
  265. szConvMsg[0] = szMsg[0];
  266. return 0;
  267. }
  268. INT TypeIsFLOAT(char *szMsg, char *szConvMsg)
  269. {
  270. union
  271. {
  272. float fNum ;
  273. BYTE ch[4];
  274. }uBtoF;
  275. BYTE ch[8] = {0};
  276. memcpy(ch, szMsg, 8);
  277. //LOG4C((LOG_NOTICE,"szMsg = %s ; szConvMsg = %s", szMsg, szConvMsg));
  278. uBtoF.ch[0] = (AsciiToBYTE(szMsg[0])<<4) | AsciiToBYTE(szMsg[1]);
  279. uBtoF.ch[1] = (AsciiToBYTE(szMsg[2])<<4) | AsciiToBYTE(szMsg[3]);
  280. uBtoF.ch[2] = (AsciiToBYTE(szMsg[4])<<4) | AsciiToBYTE(szMsg[5]);
  281. uBtoF.ch[3] = (AsciiToBYTE(szMsg[6])<<4) | AsciiToBYTE(szMsg[7]);
  282. sprintf(szConvMsg,"%f",uBtoF.fNum);
  283. //LOG4C((LOG_NOTICE,"ch = %s ; szConvMsg = %s", ch, szConvMsg));
  284. return 0;
  285. }
  286. INT TypeIsSHORT(char *szMsg, char *szConvMsg)
  287. {
  288. WORD hW;
  289. BYTE a[2] = {0};
  290. BYTE ch[4] = {0};
  291. memcpy(ch, szMsg, 4);
  292. a[0] = AsciiToBYTE(ch[0]) << 4 | AsciiToBYTE(ch[1]);
  293. a[1] = AsciiToBYTE(ch[2]) << 4 | AsciiToBYTE(ch[3]);
  294. hW = (a[0] & 0xFFFF) << 8 | ( a[1] & 0xFFFF );
  295. //hW = atoi(a);
  296. sprintf(szConvMsg,"%d",hW);
  297. return 0;
  298. }
  299. INT TypeIsWORD(char *szMsg, char *szConvMsg)
  300. {
  301. WORD hW;
  302. BYTE a[2] = {0};
  303. int i;
  304. BYTE ch[4] = {0};
  305. for (i=0;i<4;i++)
  306. {
  307. ch[i] = szMsg[i];
  308. }
  309. a[0] = AsciiToBYTE(ch[0])*16 + AsciiToBYTE(ch[1]);
  310. a[1] = AsciiToBYTE(ch[2])*16 + AsciiToBYTE(ch[3]);
  311. hW = (a[0] & 0xFFFF) << 8 | ( a[1] & 0xFFFF );
  312. //hW = atoi(a);
  313. sprintf(szConvMsg,"%d",hW);
  314. //LOG4C((LOG_NOTICE,"szMsg = %s ; szConvMsg = %s", szMsg, szConvMsg))
  315. return 0;
  316. }
  317. // 市委未用到,回去再改
  318. INT TypeIsDWORD(char *szMsg, char *szConvMsg)
  319. {
  320. char nbuf[VAR_MSG] = {0};
  321. char nchar;
  322. char nchar1;
  323. char nchar2;
  324. char nchar3;
  325. for (int i = 0; i<(int)strlen(szMsg); i++)
  326. {
  327. nbuf[i] = static_cast<char>(AsciiToBYTE(szMsg[i]));
  328. }
  329. nchar = nbuf[0]<<4 | nbuf[1];
  330. nchar1 = nbuf[2]<<4 | nbuf[3];
  331. nchar2 = nbuf[4]<<4 | nbuf[5];
  332. nchar3 = nbuf[6]<<4 | nbuf[7];
  333. if ((nchar == 0) && (nchar1 == 0) && (nchar2 == 0) && (nchar3 == 0))
  334. {
  335. sprintf(szConvMsg, "0");
  336. }
  337. else
  338. {
  339. sprintf(szConvMsg, "%c%c%c%c",nchar, nchar1, nchar2, nchar3);
  340. }
  341. return 0;
  342. }
  343. INT TypeIsBYTE(BYTE *szMsg, BYTE *szConvMsg, int len)
  344. {
  345. //LOG4C((LOG_NOTICE, "TypeIsBYTE"));
  346. //LOG4C_HEX_DUMP((LOG_INFO, (char *)szMsg, 5));
  347. BYTE btChar;
  348. char ch[3] = {0};
  349. for (int i=0; i<len/2; i++)
  350. {
  351. for (int j=0; j<2; j++)
  352. {
  353. ch[j] = szMsg[j + i*2];
  354. }
  355. }
  356. btChar = AsciiToBYTE(ch[0]) << 4 | AsciiToBYTE(ch[1]);
  357. sprintf(reinterpret_cast<char*>(szConvMsg),"%d",btChar);
  358. //szConvMsg[i] = AsciiToBYTE(ch[0]) << 4 | AsciiToBYTE(ch[1]);
  359. return 0;
  360. }
  361. INT TypeIsCHAR(char *szMsg, char *szConvMsg, int len)
  362. {
  363. BYTE ch[3];
  364. for (int i=0; i<len/2; i++)
  365. {
  366. memset(ch, 0, 2);
  367. for (int j=0; j<2; j++)
  368. {
  369. ch[j] = szMsg[j + i*2];
  370. }
  371. szConvMsg[i] += AsciiToBYTE(ch[0]) << 4 | AsciiToBYTE(ch[1]);
  372. }
  373. return 0;
  374. }
  375. INT TypeIsBIT(char *szMsg, char *szConvMsg, int len, int StaBit, int EndBit)
  376. {
  377. char bytValue[2];
  378. int ByteBit = 0;
  379. memset(bytValue, 0, 2);
  380. TypeIsCHAR(szMsg, bytValue, len);
  381. bytValue[0] = AsciiToBYTE(bytValue[0]);
  382. //int Len = sizeof(bytValue);
  383. if (StaBit == EndBit)
  384. {
  385. switch(StaBit)
  386. {
  387. case 0:
  388. ByteBit = bytValue[0] & 0x01;
  389. break;
  390. case 1:
  391. ByteBit = (bytValue[0] & 0x02) / 0x02 ;
  392. break;
  393. case 2:
  394. ByteBit = (bytValue[0] & 0x04) / 0x04 ;
  395. break;
  396. case 3:
  397. ByteBit = (bytValue[0] & 0x08) / 0x08 ;
  398. break;
  399. case 4:
  400. ByteBit = (bytValue[0] & 0x10) / 0x10 ;
  401. break;
  402. case 5:
  403. ByteBit = (bytValue[0] & 0x20) / 0x20 ;
  404. break;
  405. case 6:
  406. ByteBit = (bytValue[0] & 0x40) / 0x40 ;
  407. break;
  408. case 7:
  409. ByteBit = (bytValue[0] & 0x80) / 0x80;
  410. break;
  411. }
  412. _snprintf(szConvMsg,sizeof(szConvMsg),"%d",ByteBit);
  413. }
  414. else
  415. {
  416. int tempbit = StaBit;
  417. int TiLen = EndBit - StaBit;
  418. for (int i = 0; i < TiLen;i++)
  419. {
  420. tempbit = tempbit + i;
  421. switch(tempbit)
  422. {
  423. case 0:
  424. ByteBit = bytValue[0] & 0x01;
  425. break;
  426. case 1:
  427. ByteBit = (bytValue[0] & 0x02) / 0x02 ;
  428. break;
  429. case 2:
  430. ByteBit = (bytValue[0] & 0x04) / 0x04 ;
  431. break;
  432. case 3:
  433. ByteBit = (bytValue[0] & 0x08) / 0x08 ;
  434. break;
  435. case 4:
  436. ByteBit = (bytValue[0] & 0x10) / 0x10 ;
  437. break;
  438. case 5:
  439. ByteBit = (bytValue[0] & 0x20) / 0x20 ;
  440. break;
  441. case 6:
  442. ByteBit = (bytValue[0] & 0x40) / 0x40 ;
  443. break;
  444. case 7:
  445. ByteBit = (bytValue[0] & 0x80) / 0x80;
  446. break;
  447. }
  448. _snprintf(szConvMsg + i,sizeof(szConvMsg),"%f",ByteBit);
  449. }
  450. }
  451. return 0;
  452. }
  453. int DigitToBinary(WORD wdSource, char* pDes, int iBit)
  454. {
  455. char pTmpBuf[16] = {0};
  456. char chBuffer[16] = {0};
  457. //wdSource =htonl(wdSource);
  458. itoa(wdSource, pTmpBuf, 2);
  459. int iLen = static_cast<int>(strlen(pTmpBuf)) - 1;
  460. char chValue[16] = {0};
  461. strcpy(chValue, pTmpBuf);
  462. for (int i =0; i<=iLen; i++)
  463. {
  464. pTmpBuf[i] = chValue[iLen - i];
  465. }
  466. for (int k = 0; k<iBit; k++)
  467. {
  468. if ( 0x00 == pTmpBuf[iBit - k - 1])
  469. chBuffer[k] = 0x30;
  470. else
  471. chBuffer[k] = pTmpBuf[iBit - k - 1];
  472. }
  473. memcpy(pDes, chBuffer, iBit);
  474. return 0;
  475. }
  476. void strReverse( char *str )
  477. {
  478. int l = (int)strlen(str);
  479. for( int i = 0; i < l; i++ )
  480. {
  481. for(int i = 0; i < l; i++)
  482. {
  483. if( str[i] >= 'A' && str[i] <= 'Z' )
  484. {
  485. str[i] += 32;
  486. }
  487. else if(str[i] >= 'a' && str[i] <= 'z')
  488. {
  489. str[i] -= 32;
  490. }
  491. }
  492. }
  493. }
  494. //由数据精度获得浮点数的字符串值
  495. CString GetDoubleText(double d)
  496. {
  497. CString strRtn;
  498. CString strFmt;
  499. if(g_nPrecision == 0)
  500. strFmt = "%g";
  501. else
  502. {
  503. long lV = static_cast<long>(fabs(d));
  504. if(lV >= 1)
  505. {
  506. int nIntBit = 0;
  507. while(lV >= 1)
  508. {
  509. lV = lV/10;
  510. nIntBit++;
  511. }
  512. if(nIntBit < g_nPrecision)
  513. strFmt.Format("%c%d.%df", '%', nIntBit, (g_nPrecision-nIntBit));
  514. else
  515. strFmt.Format("%c%d.0f", '%', nIntBit);
  516. }
  517. else
  518. {
  519. double dTemp = fabs(d);
  520. if(dTemp > 0.00000000001)
  521. {
  522. int nDot0Bit = 0;
  523. while(dTemp < 0.1)
  524. {
  525. dTemp = dTemp*10;
  526. nDot0Bit++;
  527. }
  528. strFmt.Format("%c1.%df", '%', nDot0Bit + g_nPrecision);
  529. }
  530. else
  531. {
  532. strFmt = "%g";
  533. }
  534. }
  535. }
  536. strRtn.Format(strFmt, d);
  537. return strRtn;
  538. }
  539. char Hex16(char WillChangeNum[]) //该函数把四位二进制转换成十六进制数
  540. {
  541. int i;
  542. i = (WillChangeNum[3]) + (WillChangeNum[2] * 10) +
  543. (WillChangeNum[1] * 100) + (WillChangeNum[0] * 1000);
  544. switch(i)
  545. {
  546. case 0:
  547. return 0;
  548. case 1:
  549. return 1;
  550. case 10:
  551. return 2;
  552. case 11:
  553. return 3;
  554. case 100:
  555. return 4;
  556. case 101:
  557. return 5;
  558. case 110:
  559. return 6;
  560. case 111:
  561. return 7;
  562. case 1000:
  563. return 8;
  564. case 1001:
  565. return 9;
  566. case 1010:
  567. return 0x0A;
  568. case 1011:
  569. return 0x0B;
  570. case 1100:
  571. return 0x0C;
  572. case 1101:
  573. return 0x0D;
  574. case 1110:
  575. return 0x0E;
  576. case 1111:
  577. return 0x0F;
  578. }
  579. return -1;
  580. }
  581. #define CPH 0
  582. #define CPL 1
  583. static unsigned char tbcrch[] = {
  584. 0,193,129,64,1,192,128,65,1,192,128,65,0,193,129,64,
  585. 1,192,128,65,0,193,129,64,0,193,129,64,1,192,128,65,
  586. 1,192,128,65,0,193,129,64,0,193,129,64,1,192,128,65,
  587. 0,193,129,64,1,192,128,65,1,192,128,65,0,193,129,64,
  588. 1,192,128,65,0,193,129,64,0,193,129,64,1,192,128,65,
  589. 0,193,129,64,1,192,128,65,1,192,128,65,0,193,129,64,
  590. 0,193,129,64,1,192,128,65,1,192,128,65,0,193,129,64,
  591. 1,192,128,65,0,193,129,64,0,193,129,64,1,192,128,65,
  592. 1,192,128,65,0,193,129,64,0,193,129,64,1,192,128,65,
  593. 0,193,129,64,1,192,128,65,1,192,128,65,0,193,129,64,
  594. 0,193,129,64,1,192,128,65,1,192,128,65,0,193,129,64,
  595. 1,192,128,65,0,193,129,64,0,193,129,64,1,192,128,65,
  596. 0,193,129,64,1,192,128,65,1,192,128,65,0,193,129,64,
  597. 1,192,128,65,0,193,129,64,0,193,129,64,1,192,128,65,
  598. 1,192,128,65,0,193,129,64,0,193,129,64,1,192,128,65,
  599. 0,193,129,64,1,192,128,65,1,192,128,65,0,193,129,64,};
  600. static unsigned char tbcrcl[] = {
  601. 0,192,193,1,195,3,2,194,198,6,7,199,5,197,196,4,
  602. 204,12,13,205,15,207,206,14,10,202,203,11,201,9,8,200,
  603. 216,24,25,217,27,219,218,26,30,222,223,31,221,29,28,220,
  604. 20,212,213,21,215,23,22,214,210,18,19,211,17,209,208,16,
  605. 240,48,49,241,51,243,242,50,54,246,247,55,245,53,52,244,
  606. 60,252,253,61,255,63,62,254,250,58,59,251,57,249,248,56,
  607. 40,232,233,41,235,43,42,234,238,46,47,239,45,237,236,44,
  608. 228,36,37,229,39,231,230,38,34,226,227,35,225,33,32,224,
  609. 160,96,97,161,99,163,162,98,102,166,167,103,165,101,100,164,
  610. 108,172,173,109,175,111,110,174,170,106,107,171,105,169,168,104,
  611. 120,184,185,121,187,123,122,186,190,126,127,191,125,189,188,124,
  612. 180,116,117,181,119,183,182,118,114,178,179,115,177,113,112,176,
  613. 80,144,145,81,147,83,82,146,150,86,87,151,85,149,148,84,
  614. 156,92,93,157,95,159,158,94,90,154,155,91,153,89,88,152,
  615. 136,72,73,137,75,139,138,74,78,142,143,79,141,77,76,140,
  616. 68,132,133,69,135,71,70,134,130,66,67,131,65,129,128,64,};
  617. /********************************************************************
  618. * NAME : TwoHexCharToChar
  619. * FUNCTION :
  620. * PROCESS :
  621. * INPUT :
  622. * OUTPUT :
  623. * UPDATE :
  624. * RETURN :
  625. * :
  626. * PROGRAMMED :
  627. * DATE(ORG) :
  628. * CALL :
  629. * SYSTEM :
  630. ********************************************************************/
  631. char TwoHexCharToChar(char ch1,char ch2)
  632. {
  633. char Numb1;
  634. char Numb2;
  635. if (ch1 >= 'A')
  636. Numb1 = (toupper(ch1)-'0'-7)*16;
  637. else
  638. Numb1 = (ch1 - '0')*16;
  639. if (ch2 >= 'A')
  640. Numb2 = (toupper(ch2) - '0' - 7);
  641. else
  642. Numb2 = (ch2 - '0');
  643. return (Numb1 + Numb2);
  644. }
  645. /********************************************************************
  646. * NAME : Str2HexStr
  647. * FUNCTION :
  648. * PROCESS :
  649. * INPUT :
  650. * OUTPUT :
  651. * UPDATE :
  652. * RETURN :
  653. * :
  654. * PROGRAMMED :
  655. * DATE(ORG) :
  656. * CALL :
  657. * SYSTEM :
  658. ********************************************************************/
  659. void Str2HexStr(char *szHexString,char *szString,int *iHexStringLen)
  660. {
  661. int iLoop;
  662. int iCount;
  663. iLoop = 0;
  664. iCount = 0;
  665. szHexString[0] = '\0';
  666. while(1)
  667. {
  668. if (szString[iLoop] == '\0' || szString[iLoop + 1] == '\0')
  669. {
  670. break;
  671. }
  672. szHexString[iCount] = static_cast<char>(TwoHexCharToChar(szString[iLoop],szString[iLoop + 1]));
  673. iLoop ++;
  674. iLoop ++;
  675. iCount ++;
  676. }
  677. szHexString[iCount] = '\0';
  678. *iHexStringLen = iCount;
  679. }
  680. /*
  681. Convert a binary data buffer to a hex string
  682. str: output (a string like “20ef9a“)
  683. bin: input (a data buffer)
  684. binlen: input (the length of the data buffer)
  685. */
  686. void bin2str(char *str, const unsigned char *bin, int binlen)
  687. {
  688. int i;
  689. for(i=0; i<binlen; i++)
  690. {
  691. str[2*i] = ((bin[i] & 0xF0) >> 4);
  692. str[2*i] = (str[2*i] > 9) ? (str[2*i] + 'A' - 10) : (str[2*i] + '0');
  693. str[2*i+1] = (bin[i] & 0x0F);
  694. str[2*i+1] = (str[2*i+1] > 9) ? (str[2*i+1] + 'A' - 10) : (str[2*i+1] + '0');
  695. }
  696. str[2*i] = '\0';
  697. }
  698. // 将10进制转换为2进制字符串;
  699. char *dectobin(int dec,int len)
  700. {
  701. int i=0;
  702. static char buf[256];
  703. char szbuf1[4] = {0};
  704. char szbuf2[4] = {0};
  705. char szbuf3[4] = {0};
  706. #if 0
  707. for (i = 0; i< 4;i++)
  708. {
  709. szbuf1[i] = dec%2+48;
  710. dec = dec/2;
  711. }
  712. for (i = 0; i< 4;i++)
  713. {
  714. szbuf2[i] = dec%2+48;
  715. dec = dec/2;
  716. }
  717. for (i = 0; i< 4;i++)
  718. {
  719. szbuf3[i] = dec%2+48;
  720. dec = dec/2;
  721. }
  722. #endif
  723. #if 1
  724. memset(buf,0,256);
  725. memset(buf,0,len);
  726. while(dec != 0)
  727. {
  728. buf[i++]=dec%2+48;
  729. dec=dec/2;
  730. }
  731. #endif
  732. return strrev(buf);
  733. }
  734. // 算法:calc length by lenid;
  735. void ReadDZLegthbyLenid(BYTE LenID[2],BYTE Length[4])
  736. {
  737. int ndec = atoi((char*)LenID);
  738. Length = reinterpret_cast<BYTE*>(dectobin(ndec,12));
  739. return;
  740. }
  741. // 将16进制字符串按顺序转为10进制字符串;
  742. // 高在前,低在后;
  743. int HexStr2Dec(char *Source)
  744. {
  745. int iLen,temp = 0,n;
  746. iLen = (int)strlen(Source);//十六进制是按字符串传进来的,所以要获得他的长度;
  747. for( int i = 0; i < iLen; i++)
  748. {
  749. if(Source[i] >= 'A' && Source[i] <= 'F') //十六进制还要判断他是不是在A-F或者a-f之间a=10;
  750. n = Source[i] - 'A' + 10;
  751. else if(Source[i] >= 'a' && Source[i] <= 'f')
  752. n = Source[i] - 'a' + 10;
  753. else n = Source[i] - '0';
  754. temp = temp*16 + n;
  755. }
  756. return temp;
  757. }
  758. // 取字节的位;
  759. void ONEBYTE_BIT(char *szValue, const int &iSour, const int &iSBit, const int &iEBit)
  760. {
  761. if ( iSBit > iEBit)
  762. return;
  763. bitset<8> bitSor(iSour);
  764. char szBinary[9] = {0};
  765. for ( int i = 0; i < 8; i++)
  766. szBinary[i] = bitSor[i];
  767. if ( iSBit == iEBit)
  768. sprintf(szValue,"%d",(int)bitSor[iSBit]);
  769. else
  770. {
  771. // biset是从位0开始的;
  772. for (int i = 0; i < 8; i++)
  773. {
  774. if ( i < iSBit || i > iEBit)
  775. bitSor.reset(i);
  776. }
  777. itoa(bitSor.to_ulong(),szValue,10);
  778. }
  779. }
  780. void GetBYTEBit(char *szValue, const int &iSour, const int &iSBit, const int &iEBit)
  781. {
  782. if ( iSBit > iEBit)
  783. return;
  784. bitset<8> bitSor(iSour);
  785. char szBinary[9] = {0};
  786. for ( int i = 0; i < 8; i++)
  787. szBinary[i] = bitSor[i];
  788. if ( iSBit == iEBit)
  789. sprintf(szValue,"%d",(int)bitSor[iSBit]);
  790. else
  791. {
  792. // 以下无效;
  793. for (int i = 0; i < 8; i++)
  794. {
  795. if ( i < iSBit || i > iEBit)
  796. bitSor.reset(i);
  797. }
  798. itoa(bitSor.to_ulong(),szValue,10);
  799. }
  800. }
  801. // 取字节的位;
  802. void GetWORDBit(char *szValue, const int &iSour, const int &iSBit, const int &iEBit)
  803. {
  804. if ( iSBit > iEBit)
  805. return;
  806. bitset<16> bitSor(iSour);
  807. char szBinary[17] = {0};
  808. for ( int i = 0; i < 16; i++)
  809. szBinary[i] = bitSor[i];
  810. if ( iSBit == iEBit)
  811. sprintf(szValue,"%d",(int)bitSor[iSBit]);
  812. }
  813. // 取字节的位;
  814. void ONEBYTE_BIT2(char *szValue, const int &iSour, const int &iSBit, const int &iEBit)
  815. {
  816. if ( iSBit > iEBit)
  817. return;
  818. //int iSour = szCopy[0];
  819. // 位顺序: 高->低 (7~0);
  820. bitset<8> bitSor(iSour);
  821. char szBinary[9] = {0};
  822. for ( int i = 0,j = 7; i < 8; i++,j--)
  823. szBinary[j] = bitSor[i] + 48;
  824. char szCut[3] = {0};
  825. for ( int i = iSBit,j = 0; i <= iEBit; i++,j++)
  826. szCut[j] = szBinary[i];
  827. if ( strcmp(szCut,"00") == 0)
  828. szValue[0] = '0';
  829. else if ( strcmp(szCut,"01") == 0)
  830. szValue[0] = '1';
  831. else if ( strcmp(szCut,"10") == 0)
  832. szValue[0] = '2';
  833. else if ( strcmp(szCut,"11") == 0)
  834. szValue[0] = '3';
  835. }
  836. int GetIniInfo(char *szPath,char *szIniName,char *szCmd,char *IniSendCMD,int &IniSendlen,char *szDataType,int &nIndex,int &nLen, int &iSBit, int &iEBit)
  837. {
  838. CHAR szFile[MAX_PATH + 1] = "";
  839. wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
  840. IniSendlen = GetPrivateProfileString(szCmd, "SendCmd", "", IniSendCMD, 10, szFile); // 返回的字符串是以\0结束的;
  841. GetPrivateProfileString(szCmd, "type", "", szDataType, 10, szFile);
  842. szDataType[strlen(szDataType)] = '\0';
  843. nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile);
  844. nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile);
  845. iSBit = GetPrivateProfileInt(szCmd, "iSBit", 0, szFile);//从配置文件中取值
  846. iEBit = GetPrivateProfileInt(szCmd, "iEBit", 0, szFile);
  847. return 0;
  848. }
  849. UINT GetModbus16CRC(BYTE *pBuf, int len)
  850. {
  851. unsigned int Genpoly=0xA001;
  852. unsigned int CRC=0xFFFF;
  853. unsigned int index;
  854. while(len--)
  855. {
  856. CRC=CRC^(unsigned int)*pBuf++;
  857. for(index=0;index<8;index++)
  858. {
  859. if((CRC & 0x0001)==1) CRC=(CRC>>1)^Genpoly;
  860. else (CRC=CRC>>1);
  861. }
  862. }
  863. return(CRC);
  864. }
  865. INT ASCII_to_Byte(char c)
  866. {
  867. int iConvt = static_cast<int>(c);
  868. if ((c >= 'A')&&(c <= 'F'))
  869. iConvt = iConvt - 'A' + 10;
  870. else if ((c >= 'a')&&(c <= 'f'))
  871. iConvt = iConvt - 'a' + 10;
  872. else if ((c >= '0')&&(c <= '9'))
  873. iConvt -= '0';
  874. return iConvt;
  875. }
  876. int digit_to_hex(BYTE by)
  877. {
  878. if ( by >= 0 && by <= 9 )
  879. return by;
  880. char szRet[3] = {0};
  881. itoa(by,szRet,10);
  882. return TwoHexCharToChar(szRet[0],szRet[1]);
  883. }
  884. WORD ReturnASCIIWORD(BYTE *bySource)
  885. {
  886. WORD wValue = 0;
  887. wValue = bySource[0] << 8 | bySource[1];
  888. return wValue;
  889. }
  890. WORD ReturnNASCIIWORD(BYTE *bySource)
  891. {
  892. WORD wValue = 0;
  893. wValue = bySource[1] << 8 | bySource[0];
  894. return wValue;
  895. }
  896. void _BYTE_2_Decimal(char *szValue, BYTE *szSource)
  897. {
  898. // 高在前,低在后;
  899. WORD wValue = ReturnASCIIWORD(szSource);
  900. sprintf(szValue,"%d",wValue);
  901. }
  902. void _BYTE_1_Decimal(char *szValue, BYTE *szSource)
  903. {
  904. // 高在前,低在后;
  905. sprintf(szValue,"%d",szSource[0]);
  906. }