TCLCommand.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  1. #include "StdAfx.h"
  2. #include "TCLCommand.h"
  3. #define NoneOptLen 5
  4. #define MINSIZE 128
  5. #define MIDSIZE 4096
  6. #define MAXSIZE 10240
  7. byte* TCLCommand::m_pData = NULL;
  8. TCLCommand::TCLCommand(bool bSync):CBaseSerial(bSync ? 0 : FILE_FLAG_OVERLAPPED)
  9. {
  10. m_pData = new byte[MAXSIZE];
  11. }
  12. TCLCommand::~TCLCommand(void)
  13. {
  14. if (m_pData)
  15. delete []m_pData;
  16. m_pData = NULL;
  17. m_vtExternalCMDParams.clear();
  18. m_vtInternalCMDParams.clear();
  19. }
  20. int TCLCommand::pares_time_value(std::string strTime)
  21. {
  22. int nTimes = 0;
  23. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  24. if (strstr(strTime.c_str(), _T("ms")) || strstr(strTime.c_str(), _T("MS")))
  25. {
  26. nTimes = atol(strTime.c_str());
  27. }
  28. else if (strstr(strTime.c_str(), _T("s")) || strstr(strTime.c_str(), _T("S")))
  29. {
  30. nTimes = atol(strTime.c_str()) * 1000;
  31. }
  32. else if (strstr(strTime.c_str(), _T("m")) || strstr(strTime.c_str(), _T("M")))
  33. {
  34. nTimes = atol(strTime.c_str()) * 6000;
  35. }
  36. else
  37. {
  38. // 不带单位或其他的,默认ms;
  39. nTimes = atol(strTime.c_str());
  40. }
  41. #elif _MSC_VER >= 1500
  42. if (_tcsstr(strTime.c_str(), _T("ms")) || _tcsstr(strTime.c_str(), _T("MS")))
  43. {
  44. nTimes = _tstol(strTime.c_str());
  45. }
  46. else if (_tcsstr(strTime.c_str(), _T("s")) || _tcsstr(strTime.c_str(), _T("S")))
  47. {
  48. nTimes = _tstol(strTime.c_str()) * 1000;
  49. }
  50. else if (_tcsstr(strTime.c_str(), _T("m")) || _tcsstr(strTime.c_str(), _T("M")))
  51. {
  52. nTimes = _tstol(strTime.c_str()) * 6000;
  53. }
  54. else
  55. {
  56. // 不带单位或其他的,默认ms;
  57. nTimes = _tstol(strTime.c_str());
  58. }
  59. #endif
  60. return nTimes;
  61. }
  62. bool TCLCommand::parse_pair_key(std::string& RetValue, std::string strLine, TCHAR* lpszText)
  63. {
  64. TCHAR szText[MAX_PATH] = { 0 };
  65. TCHAR szValue[MAX_PATH] = { 0 };
  66. // 去除空格;
  67. #if _MSC_VER > 1900
  68. strLine.erase(std::remove_if(strLine.begin(), strLine.end(), [](unsigned char x) {return std::isspace(x); }), strLine.end()); //C++17
  69. #else
  70. for (std::string::iterator it = strLine.begin(); it != strLine.end();) {
  71. !isspace(*it) ? it++ : it = it = strLine.erase(it);
  72. }
  73. #endif
  74. #if _MSC_VER >= 1200 && _MSC_VER < 1500 // VC6.0~8.0
  75. if (2 == sscanf(strLine.c_str(), _T("%[^=]=%s"), szText, MAX_PATH, szValue, MAX_PATH))
  76. #else
  77. if (2 == _stscanf_s(strLine.c_str(), _T("%[^=]=%s"), szText, MAX_PATH, szValue, MAX_PATH))
  78. #endif
  79. {
  80. if (_tcsstr(szText, lpszText)) {
  81. RetValue = szValue;
  82. return true;
  83. }
  84. }
  85. return false;
  86. }
  87. int TCLCommand::parse_cmds_from_file(const TCHAR* file_name, std::vector<CommandParam>& vtCommandParams)
  88. {
  89. TCHAR buf[MAX_PATH] = { 0 };
  90. TCHAR name[MAX_PATH] = { 0 };
  91. TCHAR option[MAX_PATH] = { 0 };
  92. TCHAR head[MAX_PATH] = { 0 };
  93. TCHAR code[MAX_PATH] = { 0 };
  94. TCHAR param[MAX_PATH] = { 0 };
  95. TCHAR multicode[MAX_PATH] = { 0 };
  96. TCHAR cmd_wait_time[MAX_PATH] = { 0 };
  97. TCHAR read_wait_time[MAX_PATH] = { 0 };
  98. int ret = -1;
  99. FILE* fp = NULL;
  100. if (!file_name || file_name[0] == '\0')
  101. return ret;
  102. fp = fopen(file_name, "r");
  103. if (!fp)
  104. goto EXIT;
  105. while ((fgets((char*)buf, MAX_PATH, fp) != NULL)) {
  106. int tmp_len = 0;
  107. tmp_len = _tcslen(buf);
  108. if (tmp_len >= 1) {
  109. if (buf[tmp_len - 1] == '\r' || buf[tmp_len - 1] == '\n')
  110. buf[tmp_len - 1] = 0;
  111. if (tmp_len >= 2) {
  112. if (buf[tmp_len - 2] == '\r' || buf[tmp_len - 2] == '\n')
  113. buf[tmp_len - 2] = 0;
  114. }
  115. }
  116. #if _MSC_VER >= 1200 && _MSC_VER < 1500 // VC6.0~8.0
  117. if (sscanf(buf, "%[^;];%[^;];%[^;];%[^;];%[^;];%[^;];%[^;]", name, option, head, code, param, multicode, read_wait_time, cmd_wait_time) == 8)
  118. #elif _MSC_VER >= 1500
  119. //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) // 等价下面;
  120. 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)
  121. #endif
  122. {
  123. CommandParam cp;
  124. parse_pair_key(cp.name, name, _T("Name"));
  125. parse_pair_key(cp.head, head, _T("HeadCode"));
  126. parse_pair_key(cp.code, code, _T("Command"));
  127. parse_pair_key(cp.param, param, _T("CMDParam"));
  128. std::string value;
  129. parse_pair_key(value, option, _T("Option"));
  130. if (!_tcsicmp(value.c_str(), _T("None")))
  131. cp.nOption = CMDOPT_None;
  132. else if (!_tcsicmp(value.c_str(), _T("Get")))
  133. cp.nOption = CMDOPT_Get;
  134. else if (!_tcsicmp(value.c_str(), _T("Set")))
  135. cp.nOption = CMDOPT_Set;
  136. parse_pair_key(value, multicode, _T("MultiParams"));
  137. cp.bMulticode = !_tcsicmp(value.c_str(), _T("true")) ? true : false;
  138. parse_pair_key(value, read_wait_time, _T("ReadWaitTime"));
  139. cp.read_wait_time = pares_time_value(value.c_str());
  140. parse_pair_key(value, cmd_wait_time, _T("CMDWaitTime"));
  141. cp.cmd_wait_time = pares_time_value(value.c_str());
  142. cp.UpdateRtnCode();
  143. vtCommandParams.push_back(cp);
  144. }
  145. }
  146. ret = 0;
  147. EXIT:
  148. if (fp)
  149. fclose(fp);
  150. return ret;
  151. }
  152. void TCLCommand::parse_cmds_from_string(std::string str, std::vector<CommandParam>& vtCommandParams)
  153. {
  154. int nPos(0);
  155. TCHAR buf[MAX_PATH] = { 0 };
  156. TCHAR name[MAX_PATH] = { 0 };
  157. TCHAR option[MAX_PATH] = { 0 };
  158. TCHAR head[MAX_PATH] = { 0 };
  159. TCHAR code[MAX_PATH] = { 0 };
  160. TCHAR param[MAX_PATH] = { 0 };
  161. TCHAR multicode[MAX_PATH] = { 0 };
  162. TCHAR cmd_wait_time[MAX_PATH] = { 0 };
  163. TCHAR read_wait_time[MAX_PATH] = { 0 };
  164. do
  165. {
  166. memset(buf, 0, MAX_PATH);
  167. int nPos1 = str.find_first_of('\r');
  168. int nPos2 = str.find_first_of('\n');
  169. if ( std::string::npos != nPos1 && std::string::npos != nPos2 ) {
  170. nPos = nPos1 > nPos2 ? nPos1 : nPos2;
  171. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  172. sprintf(buf, _T("%s"), str.substr(0, nPos - 1).c_str());
  173. #elif _MSC_VER >= 1500
  174. _stprintf_s(buf, _T("%s"), str.substr(0, nPos - 1).c_str());
  175. #endif
  176. str = str.substr(nPos+1);
  177. }
  178. else if ( std::string::npos != nPos1 ) {
  179. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  180. sprintf(buf, _T("%s"), str.substr(0, nPos1 - 1).c_str());
  181. #elif _MSC_VER >= 1500
  182. _stprintf_s(buf, _T("%s"), str.substr(0, nPos1 - 1).c_str());
  183. #endif
  184. str = str.substr(nPos + 1);
  185. }
  186. else if ( std::string::npos != nPos2 ) {
  187. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  188. sprintf(buf, _T("%s"), str.substr(0, nPos2 - 1).c_str());
  189. #elif _MSC_VER >= 1500
  190. _stprintf_s(buf, _T("%s"), str.substr(0, nPos2 - 1).c_str());
  191. #endif
  192. str = str.substr(nPos + 1);
  193. }
  194. else {
  195. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  196. sprintf(buf, _T("%s"), str.c_str());
  197. #elif _MSC_VER >= 1500
  198. _stprintf_s(buf, _T("%s"), str.c_str());
  199. #endif
  200. str = "";
  201. }
  202. #if _MSC_VER >= 1200 && _MSC_VER < 1500 // VC6.0~8.0
  203. if (sscanf(buf, "%[^;];%[^;];%[^;];%[^;];%[^;];%[^;];%[^;]", name, option, head, code, param, multicode, read_wait_time, cmd_wait_time) == 8)
  204. #elif _MSC_VER >= 1500
  205. //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) // 等价下面;
  206. 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)
  207. #endif
  208. {
  209. CommandParam cp;
  210. parse_pair_key(cp.name, name, _T("Name"));
  211. parse_pair_key(cp.head, head, _T("HeadCode"));
  212. parse_pair_key(cp.code, code, _T("Command"));
  213. parse_pair_key(cp.param, param, _T("CMDParam"));
  214. std::string value;
  215. parse_pair_key(value, option, _T("Option"));
  216. if (!_tcsicmp(value.c_str(), _T("None")))
  217. cp.nOption = CMDOPT_None;
  218. else if (!_tcsicmp(value.c_str(), _T("Get")))
  219. cp.nOption = CMDOPT_Get;
  220. else if (!_tcsicmp(value.c_str(), _T("Set")))
  221. cp.nOption = CMDOPT_Set;
  222. parse_pair_key(value, multicode, _T("MultiParams"));
  223. cp.bMulticode = !_tcsicmp(value.c_str(), _T("true")) ? true : false;
  224. parse_pair_key(value, read_wait_time, _T("ReadWaitTime"));
  225. cp.read_wait_time = pares_time_value(value.c_str());
  226. parse_pair_key(value, cmd_wait_time, _T("CMDWaitTime"));
  227. cp.cmd_wait_time = pares_time_value(value.c_str());
  228. cp.UpdateRtnCode();
  229. vtCommandParams.push_back(cp);
  230. }
  231. } while (str.size());
  232. }
  233. bool TCLCommand::GetCommandParams(std::string name, CommandParam& cmdPara)
  234. {
  235. bool bget = false;
  236. // 外部优先;
  237. for (std::vector<CommandParam>::iterator it = m_vtExternalCMDParams.begin(); it != m_vtExternalCMDParams.end(); it++ )
  238. {
  239. if ( !_tcsicmp(name.c_str(), it->name.c_str()) ) {
  240. bget = true;
  241. cmdPara = *it;
  242. break;
  243. }
  244. }
  245. if ( !bget )
  246. {
  247. for (std::vector<CommandParam>::iterator it = m_vtInternalCMDParams.begin(); it != m_vtInternalCMDParams.end(); it++ )
  248. {
  249. if ( !_tcsicmp(name.c_str(), it->name.c_str()) ) {
  250. bget = true;
  251. cmdPara = *it;
  252. break;
  253. }
  254. }
  255. }
  256. // 清除状态;
  257. cmdPara.Clean();
  258. return bget;
  259. }
  260. bool TCLCommand::TheFirstPart(CommandParam& cmdPara, std::string data)
  261. {
  262. if (data.size() == NoneOptLen) {
  263. if ((byte)data[0] == cmdPara._rtnCode) {
  264. // 长度;
  265. int nPacketLen = (byte)data[1];
  266. if (nPacketLen != NoneOptLen) {
  267. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  268. cmdPara._rtnError = utils::_dprintf("[%s] 返回数据长度错误:%ld", (byte)data[1]);
  269. #elif _MSC_VER >= 1500
  270. cmdPara._rtnError = utils::_dprintf("[%s] 返回数据长度错误:%ld", __FUNCTION__, (byte)data[1]);
  271. #endif
  272. return false;
  273. }
  274. // 执行状态;
  275. cmdPara._rtnStatus = (byte)data[2];
  276. //utils::_dprintf(_T("[%s] rtnStatus=%02X"), __FUNCTION__, cmdPara._rtnStatus);
  277. // 校验crc;
  278. unsigned short usCRCValue = utils::CRC16Calculate((byte*)data.data(), nPacketLen - 2);
  279. if (((usCRCValue >> 8) & 0xFF) != (byte)data[nPacketLen - 2] || (usCRCValue & 0xFF) != (byte)data[nPacketLen - 1]) {
  280. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  281. cmdPara._rtnError = utils::_dprintf("CRC校验错误:计算[%02% %02X] != 接收[%02X %02X]", (usCRCValue >> 8) & 0xFF, usCRCValue & 0xFF, (byte)data[nPacketLen - 2], (byte)data[nPacketLen - 1]);
  282. #elif _MSC_VER >= 1500
  283. cmdPara._rtnError = utils::_dprintf("[%s] CRC校验错误:计算[%02% %02X] != 接收[%02X %02X]", __FUNCTION__, (usCRCValue >> 8) & 0xFF, usCRCValue & 0xFF, (byte)data[nPacketLen - 2], (byte)data[nPacketLen - 1]);
  284. #endif
  285. return false;
  286. }
  287. }
  288. else {
  289. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  290. cmdPara._rtnError = utils::_dprintf("返回码错误:%02X", (byte)data[0]);
  291. #elif _MSC_VER >= 1500
  292. cmdPara._rtnError = utils::_dprintf("[%s] 返回码错误:%02X", __FUNCTION__, (byte)data[0]);
  293. #endif
  294. return false;
  295. }
  296. }
  297. else {
  298. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  299. cmdPara._rtnError = utils::_dprintf("返回数据长度错误", (byte)data[0]);
  300. #elif _MSC_VER >= 1500
  301. cmdPara._rtnError = utils::_dprintf("[%s] 返回数据长度错误", __FUNCTION__);
  302. #endif
  303. return false;
  304. }
  305. return true;
  306. }
  307. bool TCLCommand::TheSecondPart(CommandParam& cmdPara, std::string data)
  308. {
  309. // 数据起始位;
  310. int nDataPos = 0;
  311. // 数据包长度;
  312. int nPacketLen = 0;
  313. if ((byte)data[0] == cmdPara._rtnCode) {
  314. // 获取长度;
  315. if ((byte)data[1] == 0xFE) {
  316. nDataPos = 4;
  317. nPacketLen = (byte)data[2] << 8 | (byte)data[3];
  318. if (data.size() < 255 && data[2] != 0) {
  319. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  320. cmdPara._rtnError = utils::_dprintf(_T("返回数据长度异常"));
  321. #elif _MSC_VER >= 1500
  322. cmdPara._rtnError = utils::_dprintf(_T("[%s] 返回数据长度异常"), __FUNCTION__);
  323. #endif
  324. return false;
  325. }
  326. }
  327. else
  328. {
  329. nDataPos = 2;
  330. nPacketLen = (byte)data[1];
  331. #if 0 // 如果数据包含有非协议包内的数据,会判断异常;
  332. if (data.size() > 255) {
  333. //nPackageLen = data[1] << 8 | data[2];
  334. cmdPara._rtnError = _dprintf(_T("长度异常"));
  335. return false;
  336. }
  337. #endif
  338. }
  339. #if 0
  340. // 计算出的长度,必等于包长;// 如果包含有其他非包数据,会判断异常;
  341. if (nPackageLen != data.size())
  342. return false;
  343. #endif
  344. if (_tcsicmp(cmdPara.code.c_str(), utils::ByteToChars((byte)data[nDataPos] - 1).c_str()) != 0) {
  345. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  346. cmdPara._rtnError = utils::_dprintf(_T("返回的指令错误, %s, %02X"), cmdPara.head.c_str(), (byte)data[nDataPos]);
  347. #elif _MSC_VER >= 1500
  348. cmdPara._rtnError = utils::_dprintf(_T("[%s] 返回的指令错误, %s, %02X"), __FUNCTION__, cmdPara.head.c_str(), (byte)data[nDataPos]);
  349. #endif
  350. return false;
  351. }
  352. // 返回的数据;
  353. ++nDataPos;// 返回码占一字节;
  354. if (cmdPara.bMulticode) {
  355. if (_tcsicmp(cmdPara.param.c_str(), utils::ByteToChars((byte)data[nDataPos]).c_str()) != 0) {
  356. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  357. cmdPara._rtnError = utils::_dprintf(_T("返回的指令参数错误, %s, %02X"), cmdPara.param.c_str(), (byte)data[nDataPos]);
  358. #elif _MSC_VER >= 1500
  359. cmdPara._rtnError = utils::_dprintf(_T("[%s] 返回的指令参数错误, %s, %02X"), __FUNCTION__, cmdPara.param.c_str(), (byte)data[nDataPos]);
  360. #endif
  361. return false;
  362. }
  363. ++nDataPos;// 指令参数码占一字节;
  364. }
  365. cmdPara._rtnData = data.substr(nDataPos, nPacketLen - nDataPos - 2); //2 = crc;
  366. utils::_dprintf(_T("rtnData=%s"), utils::BytesToHexString((byte*)cmdPara._rtnData.c_str(), cmdPara._rtnData.size(), ' ').c_str());
  367. // 校验crc;
  368. unsigned short usCRCValue = utils::CRC16Calculate((byte*)data.data(), nPacketLen - 2);
  369. if (((usCRCValue >> 8) & 0xFF) != (byte)data[nPacketLen - 2] || (usCRCValue & 0xFF) != (byte)data[nPacketLen - 1])
  370. {
  371. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  372. cmdPara._rtnError = utils::_dprintf("CRC校验错误:计算[%02X %02X] != 接收[%02X %02X]", (usCRCValue >> 8) & 0xFF, usCRCValue & 0xFF, (byte)data[nPacketLen - 2], (byte)data[nPacketLen - 1]);
  373. #elif _MSC_VER >= 1500
  374. cmdPara._rtnError = utils::_dprintf("[%s] CRC校验错误:计算[%02X %02X] != 接收[%02X %02X]", __FUNCTION__, (usCRCValue >> 8) & 0xFF, usCRCValue & 0xFF, (byte)data[nPacketLen - 2], (byte)data[nPacketLen - 1]);
  375. #endif
  376. return false;
  377. }
  378. if (data.size() > nPacketLen)
  379. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  380. utils::_dprintf(_T("带有脏数据:%s"), data.substr(nPacketLen));
  381. #elif _MSC_VER >= 1500
  382. utils::_dprintf("[%s] 带有脏数据:%s", __FUNCTION__, data.substr(nPacketLen));
  383. #endif
  384. }
  385. else {
  386. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  387. cmdPara._rtnError = utils::_dprintf("返回码错误:%02X", (byte)data[0]);
  388. #elif _MSC_VER >= 1500
  389. cmdPara._rtnError = utils::_dprintf("[%s] 返回码错误:%02X", __FUNCTION__, (byte)data[0]);
  390. #endif
  391. return false;
  392. }
  393. return true;
  394. }
  395. void TCLCommand::PackingCommand(CommandParam& cmdPara, std::string data, const int& dataLen)
  396. {
  397. // Tag:[命令头][全命令长度][命令码]<命令码参数><附加数据>[crc1][crc2]
  398. std::string command;
  399. // 命令头标识位;
  400. command.append(utils::HexStringToBytes(cmdPara.head, 2).c_str(), cmdPara.head.size() / 2);
  401. // 命令码;
  402. command.append(utils::HexStringToBytes(cmdPara.code, 2).c_str(), cmdPara.code.size() / 2);
  403. // 命令码参数;
  404. command.append(utils::HexStringToBytes(cmdPara.param, 2).c_str(), cmdPara.param.size() / 2);
  405. // 附加的数据;
  406. if (dataLen > 0)
  407. command.append(data.c_str(), dataLen);
  408. int len(0);
  409. // 长度:可能1字节表示,超过255用2字节表示;
  410. byte szlen[2] = { 0 };
  411. //if ((byte)command[1] == 0xFE)
  412. //if ( cmdPara.head.size() >= 4 && cmdPara.head.find("FE", 2, 2) != std::string::npos )
  413. if (_tcsicmp(_T("AAFE"), cmdPara.head.c_str()) == 0)
  414. {// 长度超过255,需要2字节表示;
  415. len = command.size() + 4; // 2位crc + 2位长度;
  416. szlen[0] = (len >> 8) & 0xFF;
  417. szlen[1] = len & 0xFF;
  418. command.insert(2, (char*)szlen, 2);
  419. }
  420. else {
  421. // 2位crc + 1位长度;
  422. len = command.size() + 3;
  423. //if ( _tcsicmp(cmdPara.code.c_str(), "99 00") == 0 )
  424. // len -= 2;
  425. if (len > 255) {// 长度超过255,多一个占位符;
  426. ++len;
  427. szlen[0] = (len >> 8) & 0xFF;
  428. szlen[1] = len & 0xFF;
  429. command.insert(1, (char*)szlen, 2);
  430. }
  431. else {
  432. szlen[0] = len & 0xFF;
  433. command.insert(1, (char*)szlen, 1);
  434. }
  435. }
  436. // crc校验;
  437. byte szcrc[2] = { 0 };
  438. WORD usCRCValue = utils::CRC16Calculate((byte*)command.c_str(), command.size()); // 如果有0断开有危险;
  439. //WORD usCRCValue = CRC16Calculate((byte *)command.c_str(), len - 2);
  440. szcrc[0] = (usCRCValue >> 8) & 0xFF;
  441. szcrc[1] = usCRCValue & 0xFF;
  442. command.append((char*)szcrc, 2);
  443. cmdPara._cmdContext = command;
  444. utils::_dprintf(_T("指令:%s = %s"), cmdPara.name.c_str(), utils::BytesToHexString((byte*)command.c_str(), command.size(), ' ').c_str());
  445. }
  446. bool TCLCommand::ParseResultString(CommandParam& cmdPara, std::string data, const int& dataLen)
  447. {
  448. // Tag:[返回头][全数据长度][返回码]<返回码子项><附加数据>[crc16]
  449. if (!TheFirstPart(cmdPara, data.substr(0, 5)))
  450. return false;
  451. if (cmdPara._rtnStatus != 0x0A) {
  452. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  453. utils::_dprintf("执行结果错误:%02X", cmdPara._rtnStatus);
  454. #elif _MSC_VER >= 1500
  455. utils::_dprintf("[%s] 执行结果错误:%02X", __FUNCTION__, cmdPara._rtnStatus);
  456. #endif
  457. return false;
  458. }
  459. switch (cmdPara.nOption)
  460. {
  461. case CMDOPT_None:
  462. break;
  463. case CMDOPT_Get:
  464. case CMDOPT_Set:
  465. return TheSecondPart(cmdPara, data.substr(5));
  466. break;
  467. default:
  468. break;
  469. }
  470. return false;
  471. }
  472. bool TCLCommand::SendCommand(CommandParam& cmdPara)
  473. {
  474. memset(m_pData, 0, MAXSIZE);
  475. if (_dwIOMode == FILE_FLAG_OVERLAPPED)
  476. {
  477. if (!Write((void*)cmdPara._cmdContext.c_str(), cmdPara._cmdContext.size()))
  478. return false;
  479. Sleep(cmdPara.read_wait_time);
  480. int nReadCount = Read(m_pData, MAXSIZE);
  481. cmdPara._rtnContext.append((char*)m_pData, nReadCount);
  482. }
  483. else
  484. {
  485. if (!WriteSync((void*)cmdPara._cmdContext.c_str(), cmdPara._cmdContext.size()))
  486. return false;
  487. Sleep(cmdPara.read_wait_time);
  488. int nReadCount = ReadSync(m_pData, MAXSIZE);
  489. cmdPara._rtnContext.append((char*)m_pData, nReadCount);
  490. }
  491. utils::_dprintf("结果:%s = %s", cmdPara.name.c_str(), utils::BytesToHexString((byte*)cmdPara._rtnContext.c_str(), cmdPara._rtnContext.size(), ' ').c_str());
  492. ParseResultString(cmdPara, cmdPara._rtnContext, cmdPara._rtnContext.size());
  493. Sleep(cmdPara.cmd_wait_time);
  494. return cmdPara._rtnStatus == 0x0A ? true : false;
  495. }
  496. void TCLCommand::SetInternalCMDParams(DWORD dwResouceID)
  497. {
  498. std::string data;
  499. if ( utils::GetResourceData(dwResouceID, _T("BIN"), data) )
  500. {
  501. parse_cmds_from_string(data, m_vtInternalCMDParams);
  502. }
  503. }
  504. void TCLCommand::SetExternalCMDParams(LPCTSTR lpFileName)
  505. {
  506. parse_cmds_from_file(lpFileName, m_vtExternalCMDParams);
  507. }
  508. bool TCLCommand::EnterFactory()
  509. {
  510. CommandParam cmdpara;
  511. GetCommandParams("EnterFactory", cmdpara);
  512. PackingCommand(cmdpara);
  513. return SendCommand(cmdpara);
  514. }
  515. bool TCLCommand::LeaveFactory()
  516. {
  517. CommandParam cmdpara;
  518. GetCommandParams("LeaveFactory", cmdpara);
  519. PackingCommand(cmdpara);
  520. return SendCommand(cmdpara);
  521. }
  522. bool TCLCommand::GetProjectId(int& pid)
  523. {
  524. CommandParam cmdpara;
  525. GetCommandParams("GetProjectId", cmdpara);
  526. PackingCommand(cmdpara);
  527. if (SendCommand(cmdpara))
  528. {
  529. if (cmdpara._rtnData.size() == 2)
  530. pid = (byte)cmdpara._rtnData[0] << 8 | (byte)cmdpara._rtnData[1];
  531. else
  532. pid = (byte)cmdpara._rtnData[0];
  533. return true;
  534. }
  535. return false;
  536. }
  537. bool TCLCommand::GetSoftVersion(std::string& strValue)
  538. {
  539. CommandParam cmdpara;
  540. GetCommandParams("GetSoftVersion", cmdpara);
  541. PackingCommand(cmdpara);
  542. if (SendCommand(cmdpara))
  543. {
  544. strValue = cmdpara._rtnData;
  545. return true;
  546. }
  547. return false;
  548. }
  549. bool TCLCommand::GetDeviceId(std::string& strValue)
  550. {
  551. CommandParam cmdpara;
  552. GetCommandParams("GetDeviceId", cmdpara);
  553. PackingCommand(cmdpara);
  554. if (SendCommand(cmdpara))
  555. {
  556. strValue = cmdpara._rtnData;
  557. return true;
  558. }
  559. return false;
  560. }
  561. bool TCLCommand::GetClientType(std::string& strValue)
  562. {
  563. CommandParam cmdpara;
  564. GetCommandParams("GetClientType", cmdpara);
  565. PackingCommand(cmdpara);
  566. if (SendCommand(cmdpara))
  567. {
  568. strValue = cmdpara._rtnData;
  569. return true;
  570. }
  571. return false;
  572. }
  573. bool TCLCommand::GetMAC(std::string& strValue)
  574. {
  575. CommandParam cmdpara;
  576. GetCommandParams("GetMAC", cmdpara);
  577. PackingCommand(cmdpara);
  578. if (SendCommand(cmdpara))
  579. {
  580. strValue = cmdpara._rtnData;
  581. return true;
  582. }
  583. return false;
  584. }
  585. bool TCLCommand::GetHDCPKey(std::string& strValue)
  586. {
  587. CommandParam cmdpara;
  588. GetCommandParams("GetHDCPKey", cmdpara);
  589. PackingCommand(cmdpara);
  590. if (SendCommand(cmdpara))
  591. {
  592. strValue = cmdpara._rtnData;
  593. return true;
  594. }
  595. return false;
  596. }
  597. bool TCLCommand::GetHDCPKey22(std::string& strValue)
  598. {
  599. CommandParam cmdpara;
  600. GetCommandParams("GetHDCPKey22", cmdpara);
  601. PackingCommand(cmdpara);
  602. if (SendCommand(cmdpara))
  603. {
  604. strValue = cmdpara._rtnData;
  605. return true;
  606. }
  607. return false;
  608. }
  609. bool TCLCommand::GetWidi(std::string& strValue)
  610. {
  611. CommandParam cmdpara;
  612. GetCommandParams("GetWidi", cmdpara);
  613. PackingCommand(cmdpara);
  614. if (SendCommand(cmdpara))
  615. {
  616. strValue = cmdpara._rtnData;
  617. return true;
  618. }
  619. return false;
  620. }
  621. bool TCLCommand::GetNetflixESN(std::string& strValue)
  622. {
  623. CommandParam cmdpara;
  624. GetCommandParams("GetNetflixESN", cmdpara);
  625. PackingCommand(cmdpara);
  626. if (SendCommand(cmdpara))
  627. {
  628. strValue = cmdpara._rtnData;
  629. return true;
  630. }
  631. return false;
  632. }
  633. bool TCLCommand::GetWidevine(std::string& strValue)
  634. {
  635. CommandParam cmdpara;
  636. GetCommandParams("GetWidevine", cmdpara);
  637. PackingCommand(cmdpara);
  638. if (SendCommand(cmdpara))
  639. {
  640. strValue = cmdpara._rtnData;
  641. return true;
  642. }
  643. return false;
  644. }
  645. bool TCLCommand::GetCiKey(std::string& strValue)
  646. {
  647. CommandParam cmdpara;
  648. GetCommandParams("GetCiKey", cmdpara);
  649. PackingCommand(cmdpara);
  650. if (SendCommand(cmdpara))
  651. {
  652. strValue = cmdpara._rtnData;
  653. return true;
  654. }
  655. return false;
  656. }
  657. bool TCLCommand::GetOSDLanguage(std::string& strValue)
  658. {
  659. CommandParam cmdpara;
  660. GetCommandParams("GetOSDLanguage", cmdpara);
  661. PackingCommand(cmdpara);
  662. if (SendCommand(cmdpara))
  663. {
  664. strValue = cmdpara._rtnData;
  665. return true;
  666. }
  667. return false;
  668. }
  669. bool TCLCommand::GetShopLanguage(std::string& strValue)
  670. {
  671. CommandParam cmdpara;
  672. GetCommandParams("GetShopLanguage", cmdpara);
  673. PackingCommand(cmdpara);
  674. if (SendCommand(cmdpara))
  675. {
  676. strValue = cmdpara._rtnData;
  677. return true;
  678. }
  679. return false;
  680. }
  681. bool TCLCommand::GetChannel(std::string& strValue)
  682. {
  683. CommandParam cmdpara;
  684. GetCommandParams("GetChannel", cmdpara);
  685. PackingCommand(cmdpara);
  686. if (SendCommand(cmdpara))
  687. {
  688. strValue = cmdpara._rtnData;
  689. return true;
  690. }
  691. return false;
  692. }
  693. bool TCLCommand::SetProjectId(const byte* pBuffer, const int& nLen)
  694. {
  695. return false;
  696. }
  697. bool TCLCommand::SetDeviceId(LPCTSTR lpDeviceId)
  698. {
  699. return false;
  700. }
  701. bool TCLCommand::SetDeviceId(const byte* pBuffer, const int& nLen)
  702. {
  703. return false;
  704. }
  705. bool TCLCommand::SetMAC(LPCTSTR lpMac)
  706. {
  707. return false;
  708. }
  709. bool TCLCommand::SetMAC(const byte* pBuffer, const int& nLen)
  710. {
  711. return false;
  712. }
  713. bool TCLCommand::SetHDCPKey(LPCTSTR lpHDCP, bool bHasSpace)
  714. {
  715. return false;
  716. }
  717. bool TCLCommand::SetHDCPKey(const byte* pBuffer, const int& nLen)
  718. {
  719. return false;
  720. }
  721. bool TCLCommand::SetHDCPKey22(LPCTSTR lpHDCP22, bool bHasSpace)
  722. {
  723. return false;
  724. }
  725. bool TCLCommand::SetHDCPKey22(const byte* pBuffer, const int& nLen)
  726. {
  727. return false;
  728. }
  729. bool TCLCommand::SetNetflixESN(LPCTSTR lpESN, bool bHasSpace)
  730. {
  731. return false;
  732. }
  733. bool TCLCommand::SetNetflixESN(const byte* pBuffer, const int& nLen)
  734. {
  735. return false;
  736. }
  737. bool TCLCommand::SetWidi(LPCTSTR lpWidi, bool bHasSpace)
  738. {
  739. return false;
  740. }
  741. bool TCLCommand::SetWidi(const byte* pBuffer, const int& nLen)
  742. {
  743. return false;
  744. }
  745. bool TCLCommand::SetWidevine(LPCTSTR lpWidevine, bool bHasSpace)
  746. {
  747. return false;
  748. }
  749. bool TCLCommand::SetWidevine(const byte* pBuffer, const int& nLen)
  750. {
  751. return false;
  752. }
  753. bool TCLCommand::SetCiKey(LPCTSTR lpCiKey, bool bHasSpace)
  754. {
  755. return false;
  756. }
  757. bool TCLCommand::SetCiKey(const byte* pBuffer, const int& nLen)
  758. {
  759. return false;
  760. }
  761. bool TCLCommand::SetOSDLanguage(LPCTSTR lan, bool bHasSpace)
  762. {
  763. return false;
  764. }
  765. bool TCLCommand::SetOSDLanguage(const byte* pBuffer, const int& nLen)
  766. {
  767. return false;
  768. }
  769. bool TCLCommand::SetShopLanguage(LPCTSTR lan, bool bHasSpace)
  770. {
  771. return false;
  772. }
  773. bool TCLCommand::SetShopLanguage(const byte* pBuffer, const int& nLen)
  774. {
  775. return false;
  776. }
  777. bool TCLCommand::SetChannel(LPCTSTR channel, bool bHasSpace)
  778. {
  779. return false;
  780. }
  781. bool TCLCommand::SetChannel(const byte* pBuffer, const int& nLen)
  782. {
  783. return false;
  784. }
  785. bool TCLCommand::SetWBNormal(LPCTSTR data)
  786. {
  787. return false;
  788. }
  789. bool TCLCommand::SetWBNormal(const byte* pBuffer, const int& nLen)
  790. {
  791. return false;
  792. }
  793. bool TCLCommand::SetWBCool(LPCTSTR data)
  794. {
  795. return false;
  796. }
  797. bool TCLCommand::SetWBCool(const byte* pBuffer, const int& nLen)
  798. {
  799. return false;
  800. }
  801. bool TCLCommand::SetWBWarm(LPCTSTR data)
  802. {
  803. return false;
  804. }
  805. bool TCLCommand::SetWBWarm(const byte* pBuffer, const int& nLen)
  806. {
  807. return false;
  808. }
  809. bool TCLCommand::CheckDeviceId()
  810. {
  811. return false;
  812. }
  813. bool TCLCommand::CheckMAC()
  814. {
  815. return false;
  816. }
  817. bool TCLCommand::CheckHDCP()
  818. {
  819. return false;
  820. }
  821. bool TCLCommand::CheckHDCP22()
  822. {
  823. return false;
  824. }
  825. bool TCLCommand::CheckNetflixESN()
  826. {
  827. return false;
  828. }
  829. bool TCLCommand::CheckWidi()
  830. {
  831. return false;
  832. }
  833. bool TCLCommand::CheckWidevine()
  834. {
  835. return false;
  836. }
  837. bool TCLCommand::CheckCikey()
  838. {
  839. return false;
  840. }
  841. bool TCLCommand::StarWarmUpMode()
  842. {
  843. return false;
  844. }
  845. bool TCLCommand::StopWarmUpMode()
  846. {
  847. return false;
  848. }
  849. bool TCLCommand::SetProjectId(int pid)
  850. {
  851. return false;
  852. }
  853. bool TCLCommand::SetProjectId(LPCTSTR lpPid)
  854. {
  855. return false;
  856. }