stdafx.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. #elif _MSC_VER >= 1500
  57. if ( _tcsstr(strTime.c_str(), _T("ms")) || _tcsstr(strTime.c_str(), _T("MS")))
  58. {
  59. nTimes = _tstol(strTime.c_str());
  60. }
  61. else if (_tcsstr(strTime.c_str(), _T("s")) || _tcsstr(strTime.c_str(), _T("S")))
  62. {
  63. nTimes = _tstol(strTime.c_str()) * 1000;
  64. }
  65. else if (_tcsstr(strTime.c_str(), _T("m")) || _tcsstr(strTime.c_str(), _T("M")))
  66. {
  67. nTimes = _tstol(strTime.c_str()) * 6000;
  68. }
  69. else
  70. {
  71. // 不带单位或其他的,默认ms;
  72. nTimes = _tstol(strTime.c_str());
  73. }
  74. #endif
  75. return nTimes;
  76. }
  77. int parse_cmd_param_from_file(TCHAR *file_name, std::vector<CommandParam> &vtCommandParams)
  78. {
  79. TCHAR buf[MAX_PATH] = {0};
  80. TCHAR name[MAX_PATH] = {0};
  81. TCHAR option[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 >= 1200 && _MSC_VER < 1500 // VC6.0~8.0
  110. if ( sscanf(buf, "%[^;];%[^;];%[^;];%[^;];%[^;];%[^;];%[^;]", name, option, head, code, param, multicode, read_wait_time, cmd_wait_time) == 8)
  111. #elif _MSC_VER >= 1500
  112. //if ( _stscanf_s(buf, "%[^;];%[^;];%[^;];%[^;];%[^;];%[^;];%[^;];%[^;]", name, MAX_PATH, option, MAX_PATH, head, MAX_PATH, code, MAX_PATH, param, MAX_PATH, multicode, MAX_PATH, read_wait_time, MAX_PATH, cmd_wait_time, MAX_PATH) == 8) // 等价下面;
  113. if ( _stscanf_s(buf, "%[^;];%[^;];%[^;];%[^;];%[^;];%[^;];%[^;];%s", name, MAX_PATH, option, MAX_PATH, head, MAX_PATH, code, MAX_PATH, param, MAX_PATH, multicode, MAX_PATH, read_wait_time, MAX_PATH, cmd_wait_time, MAX_PATH) == 8)
  114. #endif
  115. {
  116. CommandParam cp;
  117. parse_key(cp.name, name, _T("Name"));
  118. parse_key(cp.head, head, _T("HeadCode"));
  119. parse_key(cp.code, code, _T("Command"));
  120. parse_key(cp.param, param, _T("CMDParam"));
  121. std::string value;
  122. parse_key(value, option, _T("Option"));
  123. if ( !_tcsicmp(value.c_str(), _T("None")) )
  124. cp.nOption = 0;
  125. else if (!_tcsicmp(value.c_str(), _T("Get")))
  126. cp.nOption = 1;
  127. else if (!_tcsicmp(value.c_str(), _T("Set")))
  128. cp.nOption = 2;
  129. parse_key(value, multicode, _T("MultiParams"));
  130. cp.bMulticode = !_tcsicmp(value.c_str(), _T("true")) ? true : false;
  131. parse_key(value, read_wait_time, _T("ReadWaitTime"));
  132. cp.read_wait_time = pares_time_value(value.c_str());
  133. parse_key(value, cmd_wait_time, _T("CMDWaitTime"));
  134. cp.cmd_wait_time = pares_time_value(value.c_str());
  135. vtCommandParams.push_back(cp);
  136. }
  137. }
  138. ret = 0;
  139. EXIT:
  140. if(fp)
  141. fclose(fp);
  142. return ret;
  143. }
  144. unsigned char TwoHexCharToInteger(char high, char low)
  145. {
  146. std::string str = "0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
  147. if ( str.find_first_of(high) == std::string::npos || str.find_first_of(low) == std::string::npos )
  148. {
  149. return 0;
  150. }
  151. char Numb1 = high >= 'A' ? (toupper(high) - '0' - 7) * 16 : (high - '0') * 16;
  152. char Numb2 = low >= 'A' ? (toupper(low) - '0' - 7) : (low - '0');
  153. return (Numb1 + Numb2);
  154. }
  155. const unsigned short CRC16_TABLE[16] = {
  156. 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
  157. 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF
  158. };
  159. unsigned short CRC16Calculate(byte *pBuffer, unsigned int wordLength)
  160. {
  161. byte byteTemp(0);
  162. unsigned short wordCRC(0);
  163. wordCRC = 0xFFFF;
  164. while (wordLength--) {
  165. byteTemp = (byte)(wordCRC >> 0x0C);
  166. wordCRC <<= 4;
  167. wordCRC ^= CRC16_TABLE[byteTemp ^ ((*pBuffer) >> 0x04)];
  168. byteTemp = (byte)(wordCRC >> 0x0C);
  169. wordCRC <<= 4;
  170. wordCRC ^= CRC16_TABLE[byteTemp ^ ((*pBuffer) & 0x0F)];
  171. pBuffer++;
  172. }
  173. return wordCRC;
  174. }
  175. std::string HexStringToBytes(std::string strHex, const int &len /* = 3 */)
  176. {
  177. byte value = 0;
  178. std::string strBytes;
  179. int nSize = strHex.size();
  180. for (int i = 0; i < nSize; i += len)
  181. {
  182. #if _MSC_VER >=1200 && _MSC_VER < 1500
  183. strBytes.append((char)TwoHexCharToInteger(strHex[i], strHex[i + 1]),1);
  184. #elif _MSC_VER >=1500
  185. strBytes.push_back((char)TwoHexCharToInteger(strHex[i], strHex[i + 1]));
  186. #endif
  187. }
  188. return strBytes;
  189. }
  190. std::string BytesToHexString(const unsigned char *pbuffer, int nLen, char chSpace)
  191. {
  192. std::string hex;
  193. char szhex[5] = {0};
  194. for (int i = 0; i < nLen; i++)
  195. {
  196. memset(szhex, 0, 5);
  197. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  198. sprintf(szhex, "%02X%c", pbuffer[i], chSpace);
  199. #elif _MSC_VER >= 1500
  200. _stprintf_s(szhex, "%02X%c", pbuffer[i], chSpace);
  201. #endif
  202. hex.append(szhex);
  203. }
  204. return hex.substr(0, hex.size() - 1);
  205. }
  206. std::string BytesToHexString(const unsigned char *pbuffer, int nLen)
  207. {
  208. std::string hex;
  209. char szhex[5] = {0};
  210. for (int i = 0; i < nLen; i++)
  211. {
  212. memset(szhex, 0, 5);
  213. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  214. sprintf(szhex, "%02X", pbuffer[i]);
  215. #elif _MSC_VER >= 1500
  216. _stprintf_s(szhex, "%02X", pbuffer[i]);
  217. #endif
  218. hex.append(szhex);
  219. }
  220. return hex;
  221. }
  222. BOOL IsValidString(LPCTSTR lpszString)
  223. {
  224. if (lpszString == NULL)
  225. return FALSE;
  226. do {
  227. // ASCII可显示的字符;
  228. if (*lpszString < 32 || *lpszString > 126) {
  229. return FALSE;
  230. }
  231. } while (*++lpszString);
  232. return TRUE;
  233. }
  234. // 去除空格;
  235. std::string &trim(std::string &str)
  236. {
  237. #if 0
  238. int nIndex = 0;
  239. while ((nIndex = str.find_first_of(' ')) != std::string::npos)
  240. str.erase(nIndex, 1);
  241. return str;
  242. #endif
  243. #if _MSC_VER >= 1200 && _MSC_VER < 1600
  244. for (std::string::iterator it = str.begin(); it != str.end();) {
  245. !isspace(*it) ? it++ : it = it = str.erase(it);
  246. }
  247. #elif _MSC_VER >= 1600
  248. str.erase(std::remove_if(str.begin(), str.end(), [](unsigned char x) {return std::isspace(x); }), str.end()); //C++17
  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. }