stdafx.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. // stdafx.cpp : 只包括标准包含文件的源文件
  2. // Demo.pch 将作为预编译头
  3. // stdafx.obj 将包含预编译类型信息
  4. #include "stdafx.h"
  5. // TODO: 在 STDAFX.H 中
  6. // 引用任何所需的附加头文件,而不是在此文件中引用
  7. VOID _dprintf(CHAR* pszStr, ...)
  8. {
  9. const int LOGLEN = 3072;
  10. char szData[LOGLEN] = { 0 };
  11. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  12. sprintf(szData, _T("[%s %s]\n\t"), _T("Db"), CTime::GetCurrentTime().Format(_T("%H:%M:%S")));
  13. #elif _MSC_VER >= 1500
  14. _stprintf_s(szData, _T("[%s %s]\n\t"), _T("Db"), CTime::GetCurrentTime().Format(_T("%H:%M:%S")).GetString());
  15. #endif
  16. int len = strlen(szData);
  17. va_list args;
  18. va_start(args, pszStr);
  19. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  20. _vsnprintf(szData + len, LOGLEN - len, pszStr, args);
  21. #elif _MSC_VER >= 1500
  22. _vsntprintf_s(szData + len, LOGLEN - len, _TRUNCATE, pszStr, args);
  23. #endif
  24. va_end(args);
  25. if (szData[strlen(szData) - 1] != '\n')
  26. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  27. strcat(szData, "\n");
  28. #elif _MSC_VER >= 1500
  29. strcat_s(szData, "\n");
  30. #endif
  31. OutputDebugStringA(szData);
  32. }
  33. bool parse_key(std::string &RetValue, std::string strLine, TCHAR *lpszText)
  34. {
  35. TCHAR szText[MAX_PATH] = {0};
  36. TCHAR szValue[MAX_PATH] = {0};
  37. // 去除空格;
  38. #if _MSC_VER > 1900
  39. strLine.erase(std::remove_if(strLine.begin(), strLine.end(), [](unsigned char x) {return std::isspace(x); }), strLine.end()); //C++17
  40. #else
  41. for (std::string::iterator it = strLine.begin(); it != strLine.end();) {
  42. !isspace(*it) ? it++ : it = it = strLine.erase(it);
  43. }
  44. #endif
  45. #if _MSC_VER >= 1200 && _MSC_VER < 1500 // VC6.0~8.0
  46. if ( 2 == sscanf(strLine.c_str(), _T("%[^=]=%s"), szText, MAX_PATH, szValue, MAX_PATH) )
  47. #else
  48. if ( 2 == _stscanf_s(strLine.c_str(), _T("%[^=]=%s"), szText, MAX_PATH, szValue, MAX_PATH) )
  49. #endif
  50. {
  51. if ( _tcsstr(szText, lpszText) )
  52. {
  53. RetValue = szValue;
  54. return true;
  55. }
  56. }
  57. return false;
  58. }
  59. // 计算时间;
  60. // 如: 100ms 100m 100s
  61. int pares_time_value(std::string strTime)
  62. {
  63. int nTimes = 0;
  64. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  65. if ( strstr(strTime.c_str(), _T("ms")) || strstr(strTime.c_str(), _T("MS")))
  66. {
  67. nTimes = atol(strTime.c_str());
  68. }
  69. else if (strstr(strTime.c_str(), _T("s")) || strstr(strTime.c_str(), _T("S")))
  70. {
  71. nTimes = atol(strTime.c_str()) * 1000;
  72. }
  73. else if (strstr(strTime.c_str(), _T("m")) || strstr(strTime.c_str(), _T("M")))
  74. {
  75. nTimes = atol(strTime.c_str()) * 6000;
  76. }
  77. else
  78. {
  79. // 不带单位或其他的,默认ms;
  80. nTimes = atol(strTime.c_str());
  81. }
  82. #elif _MSC_VER >= 1500
  83. if ( _tcsstr(strTime.c_str(), _T("ms")) || _tcsstr(strTime.c_str(), _T("MS")))
  84. {
  85. nTimes = _tstol(strTime.c_str());
  86. }
  87. else if (_tcsstr(strTime.c_str(), _T("s")) || _tcsstr(strTime.c_str(), _T("S")))
  88. {
  89. nTimes = _tstol(strTime.c_str()) * 1000;
  90. }
  91. else if (_tcsstr(strTime.c_str(), _T("m")) || _tcsstr(strTime.c_str(), _T("M")))
  92. {
  93. nTimes = _tstol(strTime.c_str()) * 6000;
  94. }
  95. else
  96. {
  97. // 不带单位或其他的,默认ms;
  98. nTimes = _tstol(strTime.c_str());
  99. }
  100. #endif
  101. return nTimes;
  102. }
  103. int parse_cmd_param_from_file(TCHAR *file_name, std::vector<CommandParam> &vtCommandParams)
  104. {
  105. TCHAR buf[MAX_PATH] = {0};
  106. TCHAR name[MAX_PATH] = {0};
  107. TCHAR option[MAX_PATH] = {0};
  108. TCHAR head[MAX_PATH] = {0};
  109. TCHAR code[MAX_PATH] = {0};
  110. TCHAR param[MAX_PATH] = {0};
  111. TCHAR multicode[MAX_PATH] = {0};
  112. TCHAR cmd_wait_time[MAX_PATH] = {0};
  113. TCHAR read_wait_time[MAX_PATH] = {0};
  114. int ret = -1;
  115. FILE *fp = NULL;
  116. if(!file_name || file_name[0] == '\0' )
  117. return ret;
  118. fp = fopen(file_name, "r");
  119. if(!fp)
  120. goto EXIT;
  121. while((fgets((char*)buf, MAX_PATH, fp) != NULL)) {
  122. int tmp_len = 0;
  123. unsigned short keycode = 0;
  124. unsigned int protocol = 0;
  125. unsigned int scancode = 0;
  126. tmp_len = _tcslen(buf);
  127. if(tmp_len >= 1) {
  128. if (buf[tmp_len - 1] == '\r' || buf[tmp_len - 1] == '\n')
  129. buf[tmp_len - 1] = 0;
  130. if(tmp_len >= 2) {
  131. if(buf[tmp_len - 2] == '\r' || buf[tmp_len - 2] == '\n')
  132. buf[tmp_len - 2] = 0;
  133. }
  134. }
  135. #if _MSC_VER >= 1200 && _MSC_VER < 1500 // VC6.0~8.0
  136. if ( sscanf(buf, "%[^;];%[^;];%[^;];%[^;];%[^;];%[^;];%[^;]", name, option, head, code, param, multicode, read_wait_time, cmd_wait_time) == 8)
  137. #elif _MSC_VER >= 1500
  138. //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) // 等价下面;
  139. 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)
  140. #endif
  141. {
  142. CommandParam cp;
  143. parse_key(cp.name, name, _T("Name"));
  144. parse_key(cp.head, head, _T("HeadCode"));
  145. parse_key(cp.code, code, _T("Command"));
  146. parse_key(cp.param, param, _T("CMDParam"));
  147. std::string value;
  148. parse_key(value, option, _T("Option"));
  149. if ( !_tcsicmp(value.c_str(), _T("None")) )
  150. cp.nOption = CMDOPT_None;
  151. else if (!_tcsicmp(value.c_str(), _T("Get")))
  152. cp.nOption = CMDOPT_Get;
  153. else if (!_tcsicmp(value.c_str(), _T("Set")))
  154. cp.nOption = CMDOPT_Set;
  155. parse_key(value, multicode, _T("MultiParams"));
  156. cp.bMulticode = !_tcsicmp(value.c_str(), _T("true")) ? true : false;
  157. parse_key(value, read_wait_time, _T("ReadWaitTime"));
  158. cp.read_wait_time = pares_time_value(value.c_str());
  159. parse_key(value, cmd_wait_time, _T("CMDWaitTime"));
  160. cp.cmd_wait_time = pares_time_value(value.c_str());
  161. cp.UpdateRtnCode();
  162. vtCommandParams.push_back(cp);
  163. }
  164. }
  165. ret = 0;
  166. EXIT:
  167. if(fp)
  168. fclose(fp);
  169. return ret;
  170. }
  171. unsigned char TwoHexCharToInteger(char high, char low)
  172. {
  173. std::string str = "0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
  174. if ( str.find_first_of(high) == std::string::npos || str.find_first_of(low) == std::string::npos )
  175. {
  176. return 0;
  177. }
  178. char Numb1 = high >= 'A' ? (toupper(high) - '0' - 7) * 16 : (high - '0') * 16;
  179. char Numb2 = low >= 'A' ? (toupper(low) - '0' - 7) : (low - '0');
  180. return (Numb1 + Numb2);
  181. }
  182. const unsigned short CRC16_TABLE[16] = {
  183. 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
  184. 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF
  185. };
  186. unsigned short CRC16Calculate(byte *pBuffer, unsigned int wordLength)
  187. {
  188. byte byteTemp(0);
  189. unsigned short wordCRC(0);
  190. wordCRC = 0xFFFF;
  191. while (wordLength--) {
  192. byteTemp = (byte)(wordCRC >> 0x0C);
  193. wordCRC <<= 4;
  194. wordCRC ^= CRC16_TABLE[byteTemp ^ ((*pBuffer) >> 0x04)];
  195. byteTemp = (byte)(wordCRC >> 0x0C);
  196. wordCRC <<= 4;
  197. wordCRC ^= CRC16_TABLE[byteTemp ^ ((*pBuffer) & 0x0F)];
  198. pBuffer++;
  199. }
  200. return wordCRC;
  201. }
  202. std::string HexStringToBytes(std::string strHex, const int &len /* = 3 */)
  203. {
  204. byte value = 0;
  205. std::string strBytes;
  206. int nSize = strHex.size();
  207. for (int i = 0; i < nSize; i += len)
  208. {
  209. #if _MSC_VER >=1200 && _MSC_VER < 1500
  210. strBytes.append((char)TwoHexCharToInteger(strHex[i], strHex[i + 1]),1);
  211. #elif _MSC_VER >=1500
  212. strBytes.push_back((char)TwoHexCharToInteger(strHex[i], strHex[i + 1]));
  213. #endif
  214. }
  215. return strBytes;
  216. }
  217. std::string BytesToHexString(const unsigned char *pbuffer, int nLen, char chSpace)
  218. {
  219. std::string hex;
  220. char szhex[5] = {0};
  221. for (int i = 0; i < nLen; i++)
  222. {
  223. memset(szhex, 0, 5);
  224. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  225. sprintf(szhex, "%02X%c", pbuffer[i], chSpace);
  226. #elif _MSC_VER >= 1500
  227. _stprintf_s(szhex, "%02X%c", pbuffer[i], chSpace);
  228. #endif
  229. hex.append(szhex);
  230. }
  231. return hex.substr(0, hex.size() - 1);
  232. }
  233. std::string BytesToHexString(const unsigned char *pbuffer, int nLen)
  234. {
  235. std::string hex;
  236. char szhex[5] = {0};
  237. for (int i = 0; i < nLen; i++)
  238. {
  239. memset(szhex, 0, 5);
  240. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  241. sprintf(szhex, "%02X", pbuffer[i]);
  242. #elif _MSC_VER >= 1500
  243. _stprintf_s(szhex, "%02X", pbuffer[i]);
  244. #endif
  245. hex.append(szhex);
  246. }
  247. return hex;
  248. }
  249. std::string ByteToChars(byte b)
  250. {
  251. char szhex[3] = { 0 };
  252. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  253. sprintf(szhex, "%02X", b);
  254. #elif _MSC_VER >= 1500
  255. _stprintf_s(szhex, "%02X", b);
  256. #endif
  257. return std::string(szhex);
  258. }
  259. BOOL IsValidString(LPCTSTR lpszString)
  260. {
  261. if (lpszString == NULL)
  262. return FALSE;
  263. do {
  264. // ASCII可显示的字符;
  265. if (*lpszString < 32 || *lpszString > 126) {
  266. return FALSE;
  267. }
  268. } while (*++lpszString);
  269. return TRUE;
  270. }
  271. // 去除空格;
  272. std::string &trim(std::string &str)
  273. {
  274. #if 0
  275. int nIndex = 0;
  276. while ((nIndex = str.find_first_of(' ')) != std::string::npos)
  277. str.erase(nIndex, 1);
  278. return str;
  279. #endif
  280. #if _MSC_VER >= 1200 && _MSC_VER < 1600
  281. for (std::string::iterator it = str.begin(); it != str.end();) {
  282. !isspace(*it) ? it++ : it = it = str.erase(it);
  283. }
  284. #elif _MSC_VER >= 1600
  285. str.erase(std::remove_if(str.begin(), str.end(), [](unsigned char x) {return std::isspace(x); }), str.end()); //C++17
  286. #endif
  287. return str;
  288. }
  289. std::string PackingCommand(CommandParam &cmdPara, std::string data, const int &dataLen)
  290. {
  291. // Tag:[命令头][全命令长度][命令码]<命令码参数><附加数据>[crc1][crc2]
  292. std::string command;
  293. // 命令头标识位;
  294. command.append(HexStringToBytes(cmdPara.head, 2).c_str(), cmdPara.head.size() / 2);
  295. // 命令码;
  296. command.append(HexStringToBytes(cmdPara.code, 2).c_str(), cmdPara.code.size() / 2);
  297. // 命令码参数;
  298. command.append(HexStringToBytes(cmdPara.param, 2).c_str(), cmdPara.param.size() / 2);
  299. // 附加的数据;
  300. if ( dataLen > 0)
  301. command.append(data.c_str(), dataLen);
  302. int len(0);
  303. // 长度:可能1字节表示,超过255用2字节表示;
  304. byte szlen[2] = {0};
  305. //if ((byte)command[1] == 0xFE)
  306. //if ( cmdPara.head.size() >= 4 && cmdPara.head.find("FE", 2, 2) != std::string::npos )
  307. if ( _tcsicmp(_T("AAFE"), cmdPara.head.c_str()) == 0 )
  308. {// 长度超过255,需要2字节表示;
  309. len = command.size() + 4; // 2位crc + 2位长度;
  310. szlen[0] = (len >> 8) & 0xFF;
  311. szlen[1] = len & 0xFF;
  312. command.insert(2, (char *)szlen, 2);
  313. } else {
  314. // 2位crc + 1位长度;
  315. len = command.size() + 3;
  316. //if ( _tcsicmp(cmdPara.code.c_str(), "99 00") == 0 )
  317. // len -= 2;
  318. if ( len > 255 ) {// 长度超过255,多一个占位符;
  319. ++len;
  320. szlen[0] = (len >> 8) & 0xFF;
  321. szlen[1] = len & 0xFF;
  322. command.insert(1, (char *)szlen, 2);
  323. } else {
  324. szlen[0] = len & 0xFF;
  325. command.insert(1, (char *)szlen, 1);
  326. }
  327. }
  328. // crc校验;
  329. byte szcrc[2] = {0};
  330. WORD usCRCValue = CRC16Calculate((byte *)command.c_str(), command.size()); // 如果有0断开有危险;
  331. //WORD usCRCValue = CRC16Calculate((byte *)command.c_str(), len - 2);
  332. szcrc[0] = (usCRCValue >> 8) & 0xFF;
  333. szcrc[1] = usCRCValue & 0xFF;
  334. command.append((char *)szcrc, 2);
  335. return command;
  336. }
  337. #define NoneOptLen 5
  338. void TheFirstPart(CommandParam& cmdPara, std::string data, const int& dataLen)
  339. {
  340. if (data.size() == NoneOptLen) {
  341. if (data[0] == cmdPara._rtnCode) {
  342. // 长度;
  343. int nPackageLen = data[1];
  344. if (nPackageLen != NoneOptLen) {
  345. _dprintf("None长度错误:%ld", data[1]);
  346. return;
  347. }
  348. // 执行状态;
  349. cmdPara._rtnStatus = data[2];
  350. _dprintf(_T("rtnStatus=%02X"), cmdPara._rtnStatus);
  351. // 校验crc;
  352. unsigned short usCRCValue = CRC16Calculate((unsigned char*)data.data(), nPackageLen - 2);
  353. if (((usCRCValue >> 8) & 0xFF) != (unsigned char)data[nPackageLen - 2] || (usCRCValue & 0xFF) != (unsigned char)data[nPackageLen - 1])
  354. {
  355. _dprintf("CRC校验错误:计算[%02% %02X] != 接收[%02X %02X]", (usCRCValue >> 8) & 0xFF, usCRCValue & 0xFF, data[nPackageLen - 2], data[nPackageLen - 1]);
  356. return;
  357. }
  358. }
  359. else {
  360. _dprintf("返回码错误:%02X", data[0]);
  361. }
  362. }
  363. else {
  364. _dprintf("长度对不上:Option.None");
  365. }
  366. }
  367. void TheSecondPart(CommandParam& cmdPara, std::string data, const int& dataLen)
  368. {
  369. }
  370. void ParseResultString(CommandParam& cmdPara, std::string data, const int& dataLen)
  371. {
  372. // Tag:[返回头][全数据长度][返回码]<返回码子项><附加数据>[crc1][crc2]
  373. if ( data.size() < 5) {
  374. return;
  375. }
  376. byte rtnCode = 0xAB;
  377. std::string head = cmdPara.head.substr(0, 2);
  378. if ( !_tcsicmp(_T("AA"), head.c_str()) ) { // 调试用命令代码引导码;
  379. rtnCode = 0xAB;
  380. }
  381. else if (!_tcsicmp(_T("AC"), head.c_str())) { // 软件配屏参数调整命令代码引导码;
  382. rtnCode = 0xAD;
  383. }
  384. else if (!_tcsicmp(_T("AE"), head.c_str())) { // 保留命令发送类型引导码;
  385. rtnCode = 0xAF;
  386. }
  387. int nTakenLen = 0; // 已取出的长度;
  388. unsigned short usCRCValue; // CRC验证值;
  389. // 一个完整的包段;
  390. int nPackageLen = 0;
  391. std::string packaged;
  392. // None可以看作是第一段数据包;
  393. if ( cmdPara.nOption == CMDOPT::CMDOPT_None ) {
  394. if ( data.size() == NoneOptLen) {
  395. if (data[0] == rtnCode) {
  396. if (data[1] == 0xFE) { // None中不应该出现,配置出错;
  397. _dprintf("类型配置错误:%02X %02X", data[0], data[1]);
  398. return;
  399. }
  400. // 长度;
  401. nPackageLen = data[1];
  402. if (nPackageLen != NoneOptLen) {
  403. _dprintf("None长度错误:%ld", data[1]);
  404. return;
  405. }
  406. // 执行状态;
  407. // 校验crc;
  408. usCRCValue = CRC16Calculate((unsigned char*)data.data(), nPackageLen - 2);
  409. if (((usCRCValue >> 8) & 0xFF) != (unsigned char)data[nPackageLen - 2] || (usCRCValue & 0xFF) != (unsigned char)data[nPackageLen - 1])
  410. {
  411. _dprintf("CRC校验错误:计算[%02% %02X] != 接收[%02X %02X]", (usCRCValue >> 8) & 0xFF, usCRCValue & 0xFF, data[nPackageLen - 2], data[nPackageLen - 1]);
  412. return;
  413. }
  414. }
  415. else {
  416. _dprintf("返回码错误:%02X", data[0]);
  417. }
  418. } else {
  419. _dprintf("长度对不上:Option.None");
  420. }
  421. }
  422. else if ( cmdPara.nOption == CMDOPT::CMDOPT_Get )
  423. {
  424. }
  425. else if ( cmdPara.nOption == CMDOPT::CMDOPT_Set )
  426. {
  427. }
  428. }