stdafx.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. // stdafx.cpp : 只包括标准包含文件的源文件
  2. // Demo.pch 将作为预编译头
  3. // stdafx.obj 将包含预编译类型信息
  4. #include "stdafx.h"
  5. // TODO: 在 STDAFX.H 中
  6. // 引用任何所需的附加头文件,而不是在此文件中引用
  7. bool parse_key(std::string &RetValue, std::string strLine, TCHAR *lpszText)
  8. {
  9. TCHAR szText[MAX_PATH] = {0};
  10. TCHAR szValue[MAX_PATH] = {0};
  11. // 去除空格;
  12. #if _MSC_VER > 1900
  13. strLine.erase(std::remove_if(strLine.begin(), strLine.end(), [](unsigned char x) {return std::isspace(x); }), strLine.end()); //C++17
  14. #else
  15. for (std::string::iterator it = strLine.begin(); it != strLine.end();) {
  16. !isspace(*it) ? it++ : it = it = strLine.erase(it);
  17. }
  18. #endif
  19. #if _MSC_VER >= 1200 && _MSC_VER < 1500 // VC6.0~8.0
  20. if ( 2 == sscanf(strLine.c_str(), _T("%[^=]=%s"), szText, MAX_PATH, szValue, MAX_PATH) )
  21. #else
  22. if ( 2 == _stscanf_s(strLine.c_str(), _T("%[^=]=%s"), szText, MAX_PATH, szValue, MAX_PATH) )
  23. #endif
  24. {
  25. if ( _tcsstr(szText, lpszText) )
  26. {
  27. RetValue = szValue;
  28. return true;
  29. }
  30. }
  31. return false;
  32. }
  33. // 计算时间;
  34. // 如: 100ms 100m 100s
  35. int pares_time_value(std::string strTime)
  36. {
  37. int nTimes = 0;
  38. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  39. if ( strstr(strTime.c_str(), _T("ms")) || strstr(strTime.c_str(), _T("MS")))
  40. {
  41. nTimes = atol(strTime.c_str());
  42. }
  43. else if (strstr(strTime.c_str(), _T("s")) || strstr(strTime.c_str(), _T("S")))
  44. {
  45. nTimes = atol(strTime.c_str()) * 1000;
  46. }
  47. else if (strstr(strTime.c_str(), _T("m")) || strstr(strTime.c_str(), _T("M")))
  48. {
  49. nTimes = atol(strTime.c_str()) * 6000;
  50. }
  51. else
  52. {
  53. // 不带单位或其他的,默认ms;
  54. nTimes = atol(strTime.c_str());
  55. }
  56. #endif
  57. #if _MSC_VER >= 1500
  58. if ( _tcsstr(strTime.c_str(), _T("ms")) || _tcsstr(strTime.c_str(), _T("MS")))
  59. {
  60. nTimes = _tstol(strTime.c_str());
  61. }
  62. else if (_tcsstr(strTime.c_str(), _T("s")) || _tcsstr(strTime.c_str(), _T("S")))
  63. {
  64. nTimes = _tstol(strTime.c_str()) * 1000;
  65. }
  66. else if (_tcsstr(strTime.c_str(), _T("m")) || _tcsstr(strTime.c_str(), _T("M")))
  67. {
  68. nTimes = _tstol(strTime.c_str()) * 6000;
  69. }
  70. else
  71. {
  72. // 不带单位或其他的,默认ms;
  73. nTimes = _tstol(strTime.c_str());
  74. }
  75. #endif
  76. return nTimes;
  77. }
  78. int parse_cmd_param_from_file(TCHAR *file_name, std::vector<CommandParam> &vtCommandParams)
  79. {
  80. TCHAR buf[MAX_PATH] = {0};
  81. TCHAR name[MAX_PATH] = {0};
  82. TCHAR head[MAX_PATH] = {0};
  83. TCHAR code[MAX_PATH] = {0};
  84. TCHAR param[MAX_PATH] = {0};
  85. TCHAR multicode[MAX_PATH] = {0};
  86. TCHAR cmd_wait_time[MAX_PATH] = {0};
  87. TCHAR read_wait_time[MAX_PATH] = {0};
  88. int ret = -1;
  89. FILE *fp = NULL;
  90. if(!file_name || file_name[0] == '\0' )
  91. return ret;
  92. fp = fopen(file_name, "r");
  93. if(!fp)
  94. goto EXIT;
  95. while((fgets((char*)buf, MAX_PATH, fp) != NULL)) {
  96. int tmp_len = 0;
  97. unsigned short keycode = 0;
  98. unsigned int protocol = 0;
  99. unsigned int scancode = 0;
  100. tmp_len = _tcslen(buf);
  101. if(tmp_len >= 1) {
  102. if (buf[tmp_len - 1] == '\r' || buf[tmp_len - 1] == '\n')
  103. buf[tmp_len - 1] = 0;
  104. if(tmp_len >= 2) {
  105. if(buf[tmp_len - 2] == '\r' || buf[tmp_len - 2] == '\n')
  106. buf[tmp_len - 2] = 0;
  107. }
  108. }
  109. #if _MSC_VER >= 1500
  110. //if ( _stscanf_s(buf, "%[^;];%[^;];%[^;];%[^;];%[^;];%[^;];%[^;]", name, MAX_PATH, head, MAX_PATH, code, MAX_PATH, param, MAX_PATH, multicode, MAX_PATH, read_wait_time, MAX_PATH, cmd_wait_time, MAX_PATH) == 7) // 等价下面;
  111. if ( _stscanf_s(buf, "%[^;];%[^;];%[^;];%[^;];%[^;];%[^;];%s", name, MAX_PATH, head, MAX_PATH, code, MAX_PATH, param, MAX_PATH, multicode, MAX_PATH, read_wait_time, MAX_PATH, cmd_wait_time, MAX_PATH) == 7)
  112. #endif
  113. #if _MSC_VER >= 1200 && _MSC_VER < 1500 // VC6.0~8.0
  114. if ( sscanf(buf, "%[^;];%[^;];%[^;];%[^;];%[^;];%[^;];%[^;]", name, head, code, param, multicode, read_wait_time, cmd_wait_time) == 7)
  115. #endif
  116. {
  117. CommandParam cp;
  118. parse_key(cp.name, name, _T("Name"));
  119. parse_key(cp.head, head, _T("HeadCode"));
  120. parse_key(cp.code, code, _T("Command"));
  121. parse_key(cp.param, param, _T("CMDParam"));
  122. std::string value;
  123. parse_key(value, multicode, _T("MultiParams"));
  124. cp.bMulticode = !_tcsicmp(value.c_str(), _T("true")) ? true : false;
  125. parse_key(value, read_wait_time, _T("ReadWaitTime"));
  126. cp.read_wait_time = pares_time_value(value.c_str());
  127. parse_key(value, cmd_wait_time, _T("CMDWaitTime"));
  128. cp.cmd_wait_time = pares_time_value(value.c_str());
  129. vtCommandParams.push_back(cp);
  130. }
  131. }
  132. ret = 0;
  133. EXIT:
  134. if(fp)
  135. fclose(fp);
  136. return ret;
  137. }
  138. unsigned char TwoHexCharToInteger(char high, char low)
  139. {
  140. std::string str = "0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
  141. if ( str.find_first_of(high) == std::string::npos || str.find_first_of(low) == std::string::npos )
  142. {
  143. return 0;
  144. }
  145. char Numb1 = high >= 'A' ? (toupper(high) - '0' - 7) * 16 : (high - '0') * 16;
  146. char Numb2 = low >= 'A' ? (toupper(low) - '0' - 7) : (low - '0');
  147. return (Numb1 + Numb2);
  148. }
  149. const unsigned short CRC16_TABLE[16] = {
  150. 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
  151. 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF
  152. };
  153. unsigned short CRC16Calculate(byte *pBuffer, unsigned int wordLength)
  154. {
  155. byte byteTemp(0);
  156. unsigned short wordCRC(0);
  157. wordCRC = 0xFFFF;
  158. while (wordLength--) {
  159. byteTemp = (byte)(wordCRC >> 0x0C);
  160. wordCRC <<= 4;
  161. wordCRC ^= CRC16_TABLE[byteTemp ^ ((*pBuffer) >> 0x04)];
  162. byteTemp = (byte)(wordCRC >> 0x0C);
  163. wordCRC <<= 4;
  164. wordCRC ^= CRC16_TABLE[byteTemp ^ ((*pBuffer) & 0x0F)];
  165. pBuffer++;
  166. }
  167. return wordCRC;
  168. }
  169. std::string HexStringToBytes(std::string strHex, const int &len /* = 3 */)
  170. {
  171. byte value = 0;
  172. std::string strBytes;
  173. int nSize = strHex.size();
  174. for (int i = 0; i < nSize; i += len)
  175. {
  176. #if _MSC_VER >=1500
  177. strBytes.push_back((char)TwoHexCharToInteger(strHex[i], strHex[i + 1]));
  178. #endif
  179. #if _MSC_VER >=1200 && _MSC_VER < 1500
  180. strBytes.append((char)TwoHexCharToInteger(strHex[i], strHex[i + 1]),1);
  181. #endif
  182. }
  183. return strBytes;
  184. }
  185. std::string BytesToHexString(const unsigned char *pbuffer, int nLen, char chSpace)
  186. {
  187. std::string hex;
  188. char szhex[5] = {0};
  189. for (int i = 0; i < nLen; i++)
  190. {
  191. memset(szhex, 0, 5);
  192. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  193. sprintf(szhex, "%02X%c", pbuffer[i], chSpace);
  194. #endif
  195. #if _MSC_VER >= 1500
  196. _stprintf_s(szhex, "%02X%c", pbuffer[i], chSpace);
  197. #endif
  198. hex.append(szhex);
  199. }
  200. return hex.substr(0, hex.size() - 1);
  201. }
  202. std::string BytesToHexString(const unsigned char *pbuffer, int nLen)
  203. {
  204. std::string hex;
  205. char szhex[5] = {0};
  206. for (int i = 0; i < nLen; i++)
  207. {
  208. memset(szhex, 0, 5);
  209. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  210. sprintf(szhex, "%02X", pbuffer[i]);
  211. #endif
  212. #if _MSC_VER >= 1500
  213. _stprintf_s(szhex, "%02X", pbuffer[i]);
  214. #endif
  215. hex.append(szhex);
  216. }
  217. return hex;
  218. }
  219. BOOL IsValidString(LPCTSTR lpszString)
  220. {
  221. if (lpszString == NULL)
  222. return FALSE;
  223. do
  224. {
  225. // ASCII可显示的字符;
  226. if (*lpszString < 32 || *lpszString > 126)
  227. {
  228. return FALSE;
  229. }
  230. } while (*++lpszString);
  231. return TRUE;
  232. }
  233. // 去除空格;
  234. std::string &trim(std::string &str)
  235. {
  236. #if 0
  237. int nIndex = 0;
  238. while ((nIndex = str.find_first_of(' ')) != std::string::npos)
  239. str.erase(nIndex, 1);
  240. return str;
  241. #endif
  242. #if _MSC_VER > 1900
  243. str.erase(std::remove_if(str.begin(), str.end(), [](unsigned char x) {return std::isspace(x); }), str.end()); //C++17
  244. #endif
  245. #if _MSC_VER >= 1200 && _MSC_VER < 1600
  246. for (std::string::iterator it = str.begin(); it != str.end();) {
  247. !isspace(*it) ? it++ : it = it = str.erase(it);
  248. }
  249. #endif
  250. return str;
  251. }
  252. std::string PackingCommand(CommandParam &cmdPara, std::string data, const int &dataLen)
  253. {
  254. std::string command;
  255. // 命令头标识位;
  256. command.append(HexStringToBytes(cmdPara.head, 2).c_str(), cmdPara.head.size() / 2);
  257. // 命令码;
  258. command.append(HexStringToBytes(cmdPara.code, 2).c_str(), cmdPara.code.size() / 2);
  259. // 命令码参数;
  260. command.append(HexStringToBytes(cmdPara.param, 2).c_str(), cmdPara.param.size() / 2);
  261. // 附加的数据;
  262. if ( dataLen > 0)
  263. command.append(data.c_str(), dataLen);
  264. int len(0);
  265. // 长度:可能1字节表示,超过255用2字节表示;
  266. byte szlen[2] = {0};
  267. //if ((byte)command[1] == 0xFE)
  268. //if ( cmdPara.head.size() >= 4 && cmdPara.head.find("FE", 2, 2) != std::string::npos )
  269. if ( _tcsicmp(_T("AAFE"), cmdPara.head.c_str()) == 0 )
  270. {// 长度超过255,需要2字节表示;
  271. len = command.size() + 4; // 2位crc + 2位长度;
  272. szlen[0] = (len >> 8) & 0xFF;
  273. szlen[1] = len & 0xFF;
  274. command.insert(2, (char *)szlen, 2);
  275. } else {
  276. // 2位crc + 1位长度;
  277. len = command.size() + 3;
  278. //if ( _tcsicmp(cmdPara.code.c_str(), "99 00") == 0 )
  279. // len -= 2;
  280. if ( len > 255 ) {// 长度超过255,多一个占位符;
  281. ++len;
  282. szlen[0] = (len >> 8) & 0xFF;
  283. szlen[1] = len & 0xFF;
  284. command.insert(1, (char *)szlen, 2);
  285. } else {
  286. szlen[0] = len & 0xFF;
  287. command.insert(1, (char *)szlen, 1);
  288. }
  289. }
  290. // crc校验;
  291. byte szcrc[2] = {0};
  292. //WORD usCRCValue = CRC16Calculate((byte *)command.c_str(), command.size()); // 如果有0断开有危险;
  293. WORD usCRCValue = CRC16Calculate((byte *)command.c_str(), len - 2);
  294. szcrc[0] = (usCRCValue >> 8) & 0xFF;
  295. szcrc[1] = usCRCValue & 0xFF;
  296. command.append((char *)szcrc, 2);
  297. return command;
  298. }