CommonFun.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #pragma once
  2. #include "stdafx.h"
  3. #include <string>
  4. #include <map>
  5. #include <assert.h>
  6. #include <functional>
  7. using namespace std::placeholders;
  8. namespace DIOS::Dev::Detail::SYNBOX
  9. {
  10. static const int TIMEOUTVALUE = 100;
  11. //using JSONString = std::string;
  12. //namespace DevDAP = DIOS::Dev::Detail::DAP;
  13. static const int MAX_COMMAND_LEN = 100;
  14. static const int msTimeOut_Lock = 500;
  15. static int CalChecksum(const char* cCmd, int nCmdSize)
  16. {
  17. int nSum = 0;
  18. for (int i = 0; i < nCmdSize; ++i)
  19. {
  20. nSum += cCmd[i];
  21. }
  22. return nSum % 256;
  23. }
  24. static bool FormatCmd(char* cCmd, int& nSize)
  25. {
  26. if (nSize < 1)
  27. return false;
  28. char temp[MAX_COMMAND_LEN];
  29. for (int i = 0; i < nSize; ++i)
  30. {
  31. cCmd[i] = toupper(cCmd[i]);
  32. }
  33. temp[0] = 0x3A;
  34. memcpy((void*)(temp + 1), (void*)cCmd, nSize);
  35. nSize++;
  36. int nsum = CalChecksum((char*)temp, nSize);
  37. char strSum[1024];
  38. sprintf_s(strSum, "%02x", nsum);
  39. for (size_t i = 0; i < 2; i++)
  40. {
  41. if (strSum[i] >= 'a' && strSum[i] <= 'z') //A Z 65~90 a z 97~122
  42. {
  43. strSum[i] -= 32; //转成大写
  44. }
  45. }
  46. temp[nSize] = strSum[0];
  47. nSize++;
  48. temp[nSize] = strSum[1];
  49. nSize++;
  50. temp[nSize] = char(0x0d);
  51. nSize++;
  52. temp[nSize] = char(0x0a);
  53. nSize++;
  54. //strcpy_s((char *)cCmd,nSize,(char *)temp);
  55. memcpy((void*)cCmd, (void*)temp, nSize);
  56. return true;
  57. }
  58. //把指令生产Char和hex字符串
  59. static bool CmdtoString(const char* cCmd, int nCmdSize, std::string& strCmd)
  60. {
  61. std::string strTemp;
  62. strCmd = "";
  63. for (int i = 0; i < nCmdSize; i++)
  64. {
  65. strTemp = "";
  66. if ((cCmd[i] == '\r') || (cCmd[i] == '\n') || (cCmd[i] == ':')) //
  67. strTemp = "";
  68. else
  69. strTemp = toupper(cCmd[i]);
  70. strCmd += strTemp;
  71. }
  72. return true;
  73. }
  74. static std::string GetContentFromString(std::string& strContent, char ch)
  75. {
  76. std::string strTemp;
  77. size_t nPos = strContent.find(ch);
  78. if (nPos == std::string::npos)
  79. {
  80. strTemp = strContent;
  81. strContent.clear();
  82. }
  83. else
  84. {
  85. strTemp = strContent.substr(0, nPos);
  86. strContent = strContent.substr(nPos + 1, strContent.length() - nPos - 1);
  87. }
  88. return strTemp;
  89. }
  90. static int String2Hex(std::string str)
  91. {
  92. int base = 16;
  93. char* entptr;
  94. int nRes = strtol(str.c_str(), &entptr, base);
  95. return nRes;
  96. }
  97. //Hex字符转化到Int
  98. static int ChartoInt(char uc)
  99. {
  100. int nValue;
  101. if ('0' <= uc && uc <= '9')
  102. {
  103. nValue = uc - '0';
  104. }
  105. else if ('A' <= uc && uc <= 'F')
  106. {
  107. nValue = 10 + uc - 'A';
  108. }
  109. else if ('a' <= uc && uc <= 'f')
  110. {
  111. nValue = 10 + uc - 'a';
  112. }
  113. else
  114. {
  115. nValue = -1;
  116. }
  117. return nValue;
  118. }
  119. struct tFrameMapping
  120. {
  121. static const int MaxLen = 5; // 前缀不能超超过 5 个字符 !
  122. using cbFun = std::function <void(const char*, int)>;
  123. char strHead[MaxLen]{0};
  124. int NbOfCharOfHead;
  125. cbFun fun;
  126. tFrameMapping(char* str, int len, cbFun f)
  127. {
  128. assert(len < MaxLen);
  129. for (int i = 0; i < len; i++)
  130. strHead[i] = str[i];
  131. NbOfCharOfHead = len;
  132. fun = f;
  133. }
  134. };
  135. static std::list <tFrameMapping> arFrame;
  136. //-----------------------------------------------------------------------------
  137. // DecodeFrame
  138. //-----------------------------------------------------------------------------
  139. static bool DecodeFrame(const char* strFrame, int length)
  140. {
  141. auto pr = [strFrame, length](const tFrameMapping& Item)
  142. {
  143. for (int i = 0; i < Item.NbOfCharOfHead; i++)
  144. {
  145. if (strFrame[i+1] != Item.strHead[i])
  146. {
  147. return false;
  148. }
  149. }
  150. return true;
  151. };
  152. auto found = std::find_if(arFrame.begin(), arFrame.end(), pr);
  153. if (found == arFrame.end())
  154. {
  155. return false;
  156. }
  157. const auto& Item = *found;
  158. auto pc = strFrame;
  159. pc += Item.NbOfCharOfHead+1;
  160. Item.fun(pc, length - Item.NbOfCharOfHead);
  161. return true;
  162. }
  163. }