TCLCommand.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #pragma once
  2. #include "serial.h"
  3. enum CMDOPT
  4. {
  5. CMDOPT_None = 0,
  6. CMDOPT_Get = 1,
  7. CMDOPT_Set = 2
  8. };
  9. typedef struct __CMDPARAM__ {
  10. std::string name; // 命令描述;
  11. std::string head; // 命令头;
  12. std::string code; // 命令码;
  13. std::string param; // 命令码参数;
  14. // 命令属性类型:
  15. // 0=None,只返回一段数据包;
  16. // 1=Get,返回一段或两段数据包(成功时返回2段,失败时返回1段);
  17. // 2=Set,返回一段或两段数据包(成功时返回1段,失败时返回1段);
  18. int nOption;
  19. bool bMulticode; // 命令码是否多参数;
  20. int cmd_wait_time; // 两条串口指令间隔时间;
  21. int read_wait_time; // 写完串口后,等待多久读;
  22. byte _rtnCode;
  23. byte _rtnStatus;
  24. std::string _rtnData;
  25. std::string _rtnError;
  26. std::string _rtnContext;
  27. std::string _cmdContext;
  28. void UpdateRtnCode()
  29. {
  30. if (!_tcsicmp(_T("AA"), head.c_str())) { // 调试用命令代码引导码;
  31. _rtnCode = 0xAB;
  32. }
  33. else if (!_tcsicmp(_T("AC"), head.c_str())) { // 软件配屏参数调整命令代码引导码;
  34. _rtnCode = 0xAD;
  35. }
  36. else if (!_tcsicmp(_T("AE"), head.c_str())) { // 保留命令发送类型引导码;
  37. _rtnCode = 0xAF;
  38. }
  39. }
  40. __CMDPARAM__& operator=(const __CMDPARAM__& cls)
  41. {
  42. if (this != &cls)
  43. {
  44. name = cls.name;
  45. head = cls.head;
  46. code = cls.code;
  47. param = cls.param;
  48. nOption = cls.nOption;
  49. bMulticode = cls.bMulticode;
  50. cmd_wait_time = cls.cmd_wait_time;
  51. read_wait_time = cls.read_wait_time;
  52. _rtnCode = cls._rtnCode;
  53. _rtnStatus = cls._rtnStatus;
  54. _rtnData = cls._rtnData;
  55. _rtnError = cls._rtnError;
  56. _rtnContext = cls._rtnContext;
  57. _cmdContext = cls._cmdContext;
  58. }
  59. return *this;
  60. }
  61. void Clean()
  62. {
  63. //_rtnCode = 0;
  64. _rtnStatus = 0;
  65. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  66. _rtnData = "";
  67. _rtnError = "";
  68. _rtnContext = "";
  69. _cmdContext = "";
  70. #elif _MSC_VER >= 1500
  71. _rtnData.clear();
  72. _rtnError.clear();
  73. _rtnContext.clear();
  74. _cmdContext.clear();
  75. #endif
  76. }
  77. __CMDPARAM__() {
  78. _rtnCode = 0;
  79. _rtnStatus = 0;
  80. nOption = 0;
  81. bMulticode = false;
  82. cmd_wait_time = 100;
  83. read_wait_time = 100;
  84. }
  85. } CommandParam, * pCommandParam;
  86. class TCLCommand :public CBaseSerial
  87. {
  88. // 内部-低优先查找;
  89. std::vector<CommandParam> m_vtInternalCMDParams;
  90. // 外部-高优先查找;
  91. std::vector<CommandParam> m_vtExternalCMDParams;
  92. public:
  93. TCLCommand(bool bSync = true);
  94. ~TCLCommand(void);
  95. public:
  96. int pares_time_value(std::string strTime);
  97. bool parse_pair_key(std::string& RetValue, std::string strLine, TCHAR* lpszText);
  98. int parse_cmds_from_file(TCHAR* file_name, std::vector<CommandParam>& vtCommandParams);
  99. void parse_cmds_from_string(std::string str, std::vector<CommandParam>& vtCommandParams);
  100. bool GetCommandParams(std::string name, CommandParam& cmdPara);
  101. bool TheFirstPart(CommandParam& cmdPara, std::string data);
  102. bool TheSecondPart(CommandParam& cmdPara, std::string data);
  103. std::string PackingCommand(CommandParam& cmdPara, std::string data, const int& dataLen);
  104. void ParseResultString(CommandParam& cmdPara, std::string data, const int& dataLen);
  105. public:
  106. };