CommonFun.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. int 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. static int ChartoInt(char uc)
  82. {
  83. int nValue;
  84. if ('0' <= uc && uc <= '9')
  85. {
  86. nValue = uc - '0';
  87. }
  88. else if ('A' <= uc && uc <= 'F')
  89. {
  90. nValue = 10 + uc - 'A';
  91. }
  92. else if ('a' <= uc && uc <= 'f')
  93. {
  94. nValue = 10 + uc - 'a';
  95. }
  96. else
  97. {
  98. nValue = -1;
  99. }
  100. return nValue;
  101. }
  102. //串口指令映射表
  103. using cmdFun = std::function <void(const char*, int)>;
  104. struct tFrameMapping
  105. {
  106. enum CMDOverType
  107. {
  108. OverType_directly = 1,
  109. OverType_WaitTime = 2,
  110. OverType_WaitACK = 3
  111. };
  112. CMDOverType m_eOverType; //指令结束方式:1.直接退;2.等待一些时间;3.等待ACK
  113. bool m_bLogFlag; //设置日志打印
  114. cmdFun m_fFun; //处理函数
  115. int m_nWaitTime; //设置等待时间
  116. HANDLE m_hAckEvent; //设置ACK事件
  117. tFrameMapping()
  118. {
  119. m_eOverType = OverType_directly;
  120. m_bLogFlag = false;
  121. m_fFun = NULL;
  122. m_nWaitTime = 0;
  123. m_hAckEvent = NULL;
  124. }
  125. tFrameMapping(cmdFun fun, CMDOverType type,bool logFlag,int waitTime = 100)
  126. {
  127. m_bLogFlag = logFlag;
  128. switch (type)
  129. {
  130. case OverType_directly:
  131. {
  132. m_eOverType = OverType_directly;
  133. m_nWaitTime = 0;
  134. m_hAckEvent = NULL;
  135. }
  136. break;
  137. case OverType_WaitTime:
  138. {
  139. m_eOverType = OverType_WaitTime;
  140. m_nWaitTime = waitTime;
  141. m_hAckEvent = NULL;
  142. }
  143. break;
  144. case OverType_WaitACK:
  145. {
  146. m_eOverType = OverType_WaitACK;
  147. m_nWaitTime = 0;
  148. m_hAckEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  149. }
  150. break;
  151. default:
  152. break;
  153. }
  154. m_fFun = fun;
  155. }
  156. ~tFrameMapping()
  157. {
  158. CloseHandle(m_hAckEvent);
  159. m_fFun = NULL;
  160. }
  161. tFrameMapping& operator =(const tFrameMapping& value)
  162. {
  163. m_eOverType = value.m_eOverType;
  164. m_bLogFlag = value.m_bLogFlag;
  165. m_fFun = value.m_fFun;
  166. m_nWaitTime = value.m_nWaitTime;
  167. m_hAckEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  168. return *this;
  169. }
  170. void tSetWaitState(const char * CMD = NULL)
  171. {
  172. switch (m_eOverType)
  173. {
  174. case OverType_directly:
  175. {
  176. if (m_bLogFlag)
  177. {
  178. if (CMD != NULL)
  179. mLog::Debug("tSetWaitState:[{$}]Exit directly", CMD);
  180. else
  181. mLog::Debug("tSetWaitState:Exit directly");
  182. }
  183. }
  184. break;
  185. case OverType_WaitTime:
  186. {
  187. if (m_bLogFlag)
  188. {
  189. if (CMD != NULL)
  190. mLog::Debug("tSetWaitState:[{$}]wait[{$}]ms", CMD, m_nWaitTime);
  191. else
  192. mLog::Debug("tSetWaitState:wait[{$}]ms", m_nWaitTime);
  193. }
  194. Sleep(m_nWaitTime);
  195. }
  196. break;
  197. case OverType_WaitACK:
  198. {
  199. if (m_bLogFlag)
  200. {
  201. if (CMD != NULL)
  202. mLog::Debug("tSetWaitState:[{$}]wait AckEvent 500ms", CMD);
  203. else
  204. mLog::Debug("tSetWaitState:wait AckEvent 500ms");
  205. }
  206. DWORD reasult = WaitForSingleObject(m_hAckEvent, 500);
  207. if (WAIT_OBJECT_0 == reasult)
  208. {
  209. if (m_bLogFlag)
  210. {
  211. if (CMD != NULL)
  212. mLog::Debug("tSetWaitState:[{$}]wait AckEvent successful",CMD);
  213. else
  214. mLog::Debug("tSetWaitState:wait AckEvent successful");
  215. }
  216. //ResetEvent(m_hAckEvent);
  217. }
  218. else if (WAIT_TIMEOUT == reasult)
  219. {
  220. if (m_bLogFlag)
  221. {
  222. if (CMD != NULL)
  223. mLog::Warn("tSetWaitState:[{$}]wait AckEvent timeout", CMD);
  224. else
  225. mLog::Warn("tSetWaitState:wait AckEvent timeout");
  226. }
  227. }
  228. }
  229. break;
  230. default:
  231. break;
  232. }
  233. }
  234. void tCheckWaitState(const char* CMD = NULL)
  235. {
  236. switch (m_eOverType)
  237. {
  238. case OverType_directly:
  239. {
  240. if (m_bLogFlag)
  241. {
  242. if (CMD != NULL)
  243. mLog::Debug("tCheckWaitState:[{$}]Exit directly", CMD);
  244. else
  245. mLog::Debug("tCheckWaitState:Exit directly");
  246. }
  247. }
  248. break;
  249. case OverType_WaitTime:
  250. {
  251. if (m_bLogFlag)
  252. {
  253. if (CMD != NULL)
  254. mLog::Debug("tCheckWaitState:[{$}]wait ms", CMD);
  255. else
  256. mLog::Debug("tCheckWaitState:wait ms");
  257. }
  258. }
  259. break;
  260. case OverType_WaitACK:
  261. {
  262. if (m_bLogFlag)
  263. {
  264. if (CMD != NULL)
  265. mLog::Debug("tCheckWaitState:[{$}]activate AckEvent", CMD);
  266. else
  267. mLog::Debug("tCheckWaitState:activate AckEvent");
  268. }
  269. //SetEvent(m_hAckEvent);
  270. PulseEvent(m_hAckEvent);
  271. }
  272. break;
  273. default:
  274. break;
  275. }
  276. }
  277. };
  278. static std::map <std::string,tFrameMapping> arFrame;
  279. //-----------------------------------------------------------------------------
  280. // DecodeFrame
  281. //-----------------------------------------------------------------------------
  282. #define CMD_OVER_FLAG 1
  283. static bool DecodeFrame(const char* strFrame, int length)
  284. {
  285. char data[3] = { 0 };
  286. strncpy_s(data, strFrame, 2);
  287. auto found = arFrame.find(data);
  288. if (found == arFrame.end())
  289. {
  290. return false;
  291. }
  292. //found->second.tCheckWaitState(strFrame);
  293. found->second.m_fFun(strFrame, length);//第二个参数 不重要
  294. return true;
  295. }
  296. static bool SetWaitAction(const char* strFrame)
  297. {
  298. char data[3] = { 0 };
  299. strncpy_s(data, strFrame, 2);
  300. auto found = arFrame.find(data);
  301. if (found == arFrame.end())
  302. {
  303. return false;
  304. }
  305. found->second.tSetWaitState(strFrame);
  306. }
  307. }