CommonFun.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. memcpy((void*)(temp), (void*)cCmd, nSize);
  34. temp[nSize] = char(0x0d);
  35. nSize++;
  36. temp[nSize] = char(0x0a);
  37. nSize++;
  38. //strcpy_s((char *)cCmd,nSize,(char *)temp);
  39. memcpy((void*)cCmd, (void*)temp, nSize);
  40. return true;
  41. }
  42. //把指令生产Char和hex字符串
  43. static bool CmdtoString(const char* cCmd, int nCmdSize, std::string& strCmd)
  44. {
  45. std::string strTemp;
  46. strCmd = "";
  47. for (int i = 0; i < nCmdSize; i++)
  48. {
  49. strTemp = "";
  50. if ((cCmd[i] == '\r') || (cCmd[i] == '\n') || (cCmd[i] == ':')) //
  51. strTemp = "";
  52. else
  53. strTemp = toupper(cCmd[i]);
  54. strCmd += strTemp;
  55. }
  56. return true;
  57. }
  58. static std::string GetContentFromString(std::string& strContent, char ch)
  59. {
  60. std::string strTemp;
  61. size_t nPos = strContent.find(ch);
  62. if (nPos == std::string::npos)
  63. {
  64. strTemp = strContent;
  65. strContent.clear();
  66. }
  67. else
  68. {
  69. strTemp = strContent.substr(0, nPos);
  70. strContent = strContent.substr(nPos + 1, strContent.length() - nPos - 1);
  71. }
  72. return strTemp;
  73. }
  74. static int String2Hex(std::string str)
  75. {
  76. int base = 16;
  77. char* entptr;
  78. int nRes = strtol(str.c_str(), &entptr, base);
  79. return nRes;
  80. }
  81. //Hex字符转化到Int
  82. static int ChartoInt(char uc)
  83. {
  84. int nValue;
  85. if ('0' <= uc && uc <= '9')
  86. {
  87. nValue = uc - '0';
  88. }
  89. else if ('A' <= uc && uc <= 'F')
  90. {
  91. nValue = 10 + uc - 'A';
  92. }
  93. else if ('a' <= uc && uc <= 'f')
  94. {
  95. nValue = 10 + uc - 'a';
  96. }
  97. else
  98. {
  99. nValue = -1;
  100. }
  101. return nValue;
  102. }
  103. struct tFrameMapping
  104. {
  105. static const int MaxLen = 5; // 前缀不能超超过 5 个字符 !
  106. using cbFun = std::function <void(const char*, int)>;
  107. char strHead[MaxLen]{0};
  108. int NbOfCharOfHead;
  109. cbFun fun;
  110. tFrameMapping(char* str, int len, cbFun f)
  111. {
  112. assert(len < MaxLen);
  113. for (int i = 0; i < len; i++)
  114. strHead[i] = str[i];
  115. NbOfCharOfHead = len;
  116. fun = f;
  117. }
  118. };
  119. static std::list <tFrameMapping> arFrame;
  120. //-----------------------------------------------------------------------------
  121. // DecodeFrame
  122. //-----------------------------------------------------------------------------
  123. static bool DecodeFrame(const char* strFrame, int length)
  124. {
  125. auto pr = [strFrame, length](const tFrameMapping& Item)
  126. {
  127. for (int i = 0; i < Item.NbOfCharOfHead; i++)
  128. {
  129. if (strFrame[i] != Item.strHead[i])
  130. {
  131. return false;
  132. }
  133. }
  134. return true;
  135. };
  136. auto found = std::find_if(arFrame.begin(), arFrame.end(), pr);
  137. if (found == arFrame.end())
  138. {
  139. return false;
  140. }
  141. const auto& Item = *found;
  142. auto pc = strFrame;
  143. //pc += Item.NbOfCharOfHead;
  144. Item.fun(pc, length/* - Item.NbOfCharOfHead*/);
  145. return true;
  146. }
  147. }