TCLCommand.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  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. void TCLCommand::ParseResultString(CommandParam& cmdPara, std::string data, const int& dataLen)
  447. {
  448. // Tag:[返回头][全数据长度][返回码]<返回码子项><附加数据>[crc16]
  449. if (!TheFirstPart(cmdPara, data.substr(0, 5)))
  450. return;
  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;
  458. }
  459. switch (cmdPara.nOption)
  460. {
  461. case CMDOPT_None:
  462. break;
  463. case CMDOPT_Get:
  464. case CMDOPT_Set:
  465. TheSecondPart(cmdPara, data.substr(5));
  466. break;
  467. default:
  468. break;
  469. }
  470. }
  471. bool TCLCommand::SendCommand(CommandParam& cmdPara)
  472. {
  473. memset(m_pData, 0, MAXSIZE);
  474. if (_dwIOMode == FILE_FLAG_OVERLAPPED)
  475. {
  476. if (!Write((void*)cmdPara._cmdContext.c_str(), cmdPara._cmdContext.size()))
  477. return false;
  478. Sleep(cmdPara.read_wait_time);
  479. int nReadCount = Read(m_pData, MAXSIZE);
  480. cmdPara._rtnContext.append((char*)m_pData, nReadCount);
  481. }
  482. else
  483. {
  484. if (!WriteSync((void*)cmdPara._cmdContext.c_str(), cmdPara._cmdContext.size()))
  485. return false;
  486. Sleep(cmdPara.read_wait_time);
  487. int nReadCount = ReadSync(m_pData, MAXSIZE);
  488. cmdPara._rtnContext.append((char*)m_pData, nReadCount);
  489. }
  490. utils::_dprintf("结果:%s = %s", cmdPara.name.c_str(), utils::BytesToHexString((byte*)cmdPara._rtnContext.c_str(), cmdPara._rtnContext.size(), ' ').c_str());
  491. ParseResultString(cmdPara, cmdPara._rtnContext, cmdPara._rtnContext.size());
  492. Sleep(cmdPara.cmd_wait_time);
  493. return cmdPara._rtnStatus == 0x0A ? true : false;
  494. }
  495. void TCLCommand::SetInternalCMDParams(DWORD dwResouceID)
  496. {
  497. std::string data;
  498. if ( utils::GetResourceData(dwResouceID, _T("BIN"), data) )
  499. {
  500. parse_cmds_from_string(data, m_vtInternalCMDParams);
  501. }
  502. }
  503. void TCLCommand::SetExternalCMDParams(LPCTSTR lpFileName)
  504. {
  505. parse_cmds_from_file(lpFileName, m_vtExternalCMDParams);
  506. }
  507. bool TCLCommand::EnterFactory()
  508. {
  509. CommandParam cmdpara;
  510. GetCommandParams("EnterFactory", cmdpara);
  511. PackingCommand(cmdpara);
  512. return SendCommand(cmdpara);
  513. }
  514. bool TCLCommand::LeaveFactory()
  515. {
  516. CommandParam cmdpara;
  517. GetCommandParams("LeaveFactory", cmdpara);
  518. PackingCommand(cmdpara);
  519. return SendCommand(cmdpara);
  520. }
  521. bool TCLCommand::GetProjectId(int& pid)
  522. {
  523. CommandParam cmdpara;
  524. GetCommandParams("GetProjectId", cmdpara);
  525. PackingCommand(cmdpara);
  526. if (SendCommand(cmdpara))
  527. {
  528. if (cmdpara._rtnData.size() == 2)
  529. pid = (byte)cmdpara._rtnData[0] << 8 | (byte)cmdpara._rtnData[1];
  530. else
  531. pid = (byte)cmdpara._rtnData[0];
  532. return true;
  533. }
  534. return false;
  535. }
  536. bool TCLCommand::GetSoftVersion(std::string& strValue)
  537. {
  538. CommandParam cmdpara;
  539. GetCommandParams("GetSoftVersion", cmdpara);
  540. PackingCommand(cmdpara);
  541. if (SendCommand(cmdpara))
  542. {
  543. strValue = cmdpara._rtnData;
  544. return true;
  545. }
  546. return false;
  547. }
  548. bool TCLCommand::GetDeviceId(std::string& strValue)
  549. {
  550. CommandParam cmdpara;
  551. GetCommandParams("GetDeviceId", cmdpara);
  552. PackingCommand(cmdpara);
  553. if (SendCommand(cmdpara))
  554. {
  555. strValue = cmdpara._rtnData;
  556. return true;
  557. }
  558. return false;
  559. }
  560. bool TCLCommand::GetClientType(std::string& strValue)
  561. {
  562. CommandParam cmdpara;
  563. GetCommandParams("GetClientType", cmdpara);
  564. PackingCommand(cmdpara);
  565. if (SendCommand(cmdpara))
  566. {
  567. strValue = cmdpara._rtnData;
  568. return true;
  569. }
  570. return false;
  571. }
  572. bool TCLCommand::GetMAC(std::string& strValue)
  573. {
  574. CommandParam cmdpara;
  575. GetCommandParams("GetMAC", cmdpara);
  576. PackingCommand(cmdpara);
  577. if (SendCommand(cmdpara))
  578. {
  579. strValue = cmdpara._rtnData;
  580. return true;
  581. }
  582. return false;
  583. }
  584. bool TCLCommand::GetHDCPKey(std::string& strValue)
  585. {
  586. CommandParam cmdpara;
  587. GetCommandParams("GetHDCPKey", cmdpara);
  588. PackingCommand(cmdpara);
  589. if (SendCommand(cmdpara))
  590. {
  591. strValue = cmdpara._rtnData;
  592. return true;
  593. }
  594. return false;
  595. }
  596. bool TCLCommand::GetHDCPKey22(std::string& strValue)
  597. {
  598. CommandParam cmdpara;
  599. GetCommandParams("GetHDCPKey22", cmdpara);
  600. PackingCommand(cmdpara);
  601. if (SendCommand(cmdpara))
  602. {
  603. strValue = cmdpara._rtnData;
  604. return true;
  605. }
  606. return false;
  607. }
  608. bool TCLCommand::GetWidi(std::string& strValue)
  609. {
  610. CommandParam cmdpara;
  611. GetCommandParams("GetWidi", cmdpara);
  612. PackingCommand(cmdpara);
  613. if (SendCommand(cmdpara))
  614. {
  615. strValue = cmdpara._rtnData;
  616. return true;
  617. }
  618. return false;
  619. }
  620. bool TCLCommand::GetNetflixESN(std::string& strValue)
  621. {
  622. CommandParam cmdpara;
  623. GetCommandParams("GetNetflixESN", cmdpara);
  624. PackingCommand(cmdpara);
  625. if (SendCommand(cmdpara))
  626. {
  627. strValue = cmdpara._rtnData;
  628. return true;
  629. }
  630. return false;
  631. }
  632. bool TCLCommand::GetWidevine(std::string& strValue)
  633. {
  634. CommandParam cmdpara;
  635. GetCommandParams("GetWidevine", cmdpara);
  636. PackingCommand(cmdpara);
  637. if (SendCommand(cmdpara))
  638. {
  639. strValue = cmdpara._rtnData;
  640. return true;
  641. }
  642. return false;
  643. }
  644. bool TCLCommand::GetCiKey(std::string& strValue)
  645. {
  646. CommandParam cmdpara;
  647. GetCommandParams("GetCiKey", cmdpara);
  648. PackingCommand(cmdpara);
  649. if (SendCommand(cmdpara))
  650. {
  651. strValue = cmdpara._rtnData;
  652. return true;
  653. }
  654. return false;
  655. }
  656. bool TCLCommand::GetOSDLanguage(std::string& strValue)
  657. {
  658. CommandParam cmdpara;
  659. GetCommandParams("GetOSDLanguage", cmdpara);
  660. PackingCommand(cmdpara);
  661. if (SendCommand(cmdpara))
  662. {
  663. strValue = cmdpara._rtnData;
  664. return true;
  665. }
  666. return false;
  667. }
  668. bool TCLCommand::GetShopLanguage(std::string& strValue)
  669. {
  670. CommandParam cmdpara;
  671. GetCommandParams("GetShopLanguage", cmdpara);
  672. PackingCommand(cmdpara);
  673. if (SendCommand(cmdpara))
  674. {
  675. strValue = cmdpara._rtnData;
  676. return true;
  677. }
  678. return false;
  679. }
  680. bool TCLCommand::GetChannel(std::string& strValue)
  681. {
  682. CommandParam cmdpara;
  683. GetCommandParams("GetChannel", cmdpara);
  684. PackingCommand(cmdpara);
  685. if (SendCommand(cmdpara))
  686. {
  687. strValue = cmdpara._rtnData;
  688. return true;
  689. }
  690. return false;
  691. }
  692. bool TCLCommand::SetProjectId(const byte* pBuffer, const int& nLen)
  693. {
  694. return false;
  695. }
  696. bool TCLCommand::SetDeviceId(LPCTSTR lpDeviceId)
  697. {
  698. return false;
  699. }
  700. bool TCLCommand::SetDeviceId(const byte* pBuffer, const int& nLen)
  701. {
  702. return false;
  703. }
  704. bool TCLCommand::SetMAC(LPCTSTR lpMac)
  705. {
  706. return false;
  707. }
  708. bool TCLCommand::SetMAC(const byte* pBuffer, const int& nLen)
  709. {
  710. return false;
  711. }
  712. bool TCLCommand::SetHDCPKey(LPCTSTR lpHDCP, bool bHasSpace)
  713. {
  714. return false;
  715. }
  716. bool TCLCommand::SetHDCPKey(const byte* pBuffer, const int& nLen)
  717. {
  718. return false;
  719. }
  720. bool TCLCommand::SetHDCPKey22(LPCTSTR lpHDCP22, bool bHasSpace)
  721. {
  722. return false;
  723. }
  724. bool TCLCommand::SetHDCPKey22(const byte* pBuffer, const int& nLen)
  725. {
  726. return false;
  727. }
  728. bool TCLCommand::SetNetflixESN(LPCTSTR lpESN, bool bHasSpace)
  729. {
  730. return false;
  731. }
  732. bool TCLCommand::SetNetflixESN(const byte* pBuffer, const int& nLen)
  733. {
  734. return false;
  735. }
  736. bool TCLCommand::SetWidi(LPCTSTR lpWidi, bool bHasSpace)
  737. {
  738. return false;
  739. }
  740. bool TCLCommand::SetWidi(const byte* pBuffer, const int& nLen)
  741. {
  742. return false;
  743. }
  744. bool TCLCommand::SetWidevine(LPCTSTR lpWidevine, bool bHasSpace)
  745. {
  746. return false;
  747. }
  748. bool TCLCommand::SetWidevine(const byte* pBuffer, const int& nLen)
  749. {
  750. return false;
  751. }
  752. bool TCLCommand::SetCiKey(LPCTSTR lpCiKey, bool bHasSpace)
  753. {
  754. return false;
  755. }
  756. bool TCLCommand::SetCiKey(const byte* pBuffer, const int& nLen)
  757. {
  758. return false;
  759. }
  760. bool TCLCommand::SetOSDLanguage(LPCTSTR lan, bool bHasSpace)
  761. {
  762. return false;
  763. }
  764. bool TCLCommand::SetOSDLanguage(const byte* pBuffer, const int& nLen)
  765. {
  766. return false;
  767. }
  768. bool TCLCommand::SetShopLanguage(LPCTSTR lan, bool bHasSpace)
  769. {
  770. return false;
  771. }
  772. bool TCLCommand::SetShopLanguage(const byte* pBuffer, const int& nLen)
  773. {
  774. return false;
  775. }
  776. bool TCLCommand::SetChannel(LPCTSTR channel, bool bHasSpace)
  777. {
  778. return false;
  779. }
  780. bool TCLCommand::SetChannel(const byte* pBuffer, const int& nLen)
  781. {
  782. return false;
  783. }
  784. bool TCLCommand::SetWBNormal(LPCTSTR data)
  785. {
  786. return false;
  787. }
  788. bool TCLCommand::SetWBNormal(const byte* pBuffer, const int& nLen)
  789. {
  790. return false;
  791. }
  792. bool TCLCommand::SetWBCool(LPCTSTR data)
  793. {
  794. return false;
  795. }
  796. bool TCLCommand::SetWBCool(const byte* pBuffer, const int& nLen)
  797. {
  798. return false;
  799. }
  800. bool TCLCommand::SetWBWarm(LPCTSTR data)
  801. {
  802. return false;
  803. }
  804. bool TCLCommand::SetWBWarm(const byte* pBuffer, const int& nLen)
  805. {
  806. return false;
  807. }
  808. bool TCLCommand::CheckDeviceId()
  809. {
  810. return false;
  811. }
  812. bool TCLCommand::CheckMAC()
  813. {
  814. return false;
  815. }
  816. bool TCLCommand::CheckHDCP()
  817. {
  818. return false;
  819. }
  820. bool TCLCommand::CheckHDCP22()
  821. {
  822. return false;
  823. }
  824. bool TCLCommand::CheckNetflixESN()
  825. {
  826. return false;
  827. }
  828. bool TCLCommand::CheckWidi()
  829. {
  830. return false;
  831. }
  832. bool TCLCommand::CheckWidevine()
  833. {
  834. return false;
  835. }
  836. bool TCLCommand::CheckCikey()
  837. {
  838. return false;
  839. }
  840. bool TCLCommand::StarWarmUpMode()
  841. {
  842. return false;
  843. }
  844. bool TCLCommand::StopWarmUpMode()
  845. {
  846. return false;
  847. }
  848. bool TCLCommand::SetProjectId(int pid)
  849. {
  850. return false;
  851. }
  852. bool TCLCommand::SetProjectId(LPCTSTR lpPid)
  853. {
  854. return false;
  855. }