Ver Fonte

临时提交 。

sat23 há 4 anos atrás
pai
commit
6bb1a165ca
3 ficheiros alterados com 56 adições e 25 exclusões
  1. 2 1
      Serail-Demo/Demo/Demo.cpp
  2. 46 12
      Serail-Demo/Demo/stdafx.cpp
  3. 8 12
      Serail-Demo/Demo/stdafx.h

+ 2 - 1
Serail-Demo/Demo/Demo.cpp

@@ -28,7 +28,8 @@ int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
 	}
 	else
 	{
-		parse_cmd_param_from_file("F:\\source\\scbc_repos\\Serail-Demo\\Demo\\command.data");
+		std::vector<CommandParam> vtCommandParams;
+		parse_cmd_param_from_file("F:\\source\\scbc_repos\\Serail-Demo\\Demo\\command.data", vtCommandParams);
 		// TODO: 在此处为应用程序的行为编写代码。
 		CBaseSerial sb;
 		sb.Open(23, _T("baud=115200 parity=N data=8 stop=1") );

+ 46 - 12
Serail-Demo/Demo/stdafx.cpp

@@ -16,7 +16,7 @@ bool parse_key(std::string &RetValue, std::string strLine, TCHAR *lpszText)
 	strLine.erase(std::remove_if(strLine.begin(), strLine.end(), [](unsigned char x) {return std::isspace(x); }), strLine.end()); //C++17
 #else
 	for (std::string::iterator it = strLine.begin(); it != strLine.end();)	{
-		isspace(*it) ? it++ : it = it = strLine.erase(it);
+		!isspace(*it) ? it++ : it = it = strLine.erase(it);
 	}
 #endif
 
@@ -32,13 +32,39 @@ bool parse_key(std::string &RetValue, std::string strLine, TCHAR *lpszText)
 	return false;
 }
 
-int parse_cmd_param_from_file(char *file_name)
+// 计算时间;
+// 如: 100ms 100m 100s
+int pares_time_value(std::string strTime)
 {
-	char buf[MAX_PATH] = {0};
+	int nTimes = 0;
+	if ( _tcsstr(strTime.c_str(), _T("ms")) || _tcsstr(strTime.c_str(), _T("MS")))
+	{
+		nTimes = _tstol(strTime.c_str());
+	}
+	else if (_tcsstr(strTime.c_str(), _T("s")) || _tcsstr(strTime.c_str(), _T("S")))
+	{
+		nTimes = _tstol(strTime.c_str()) * 1000;
+	}
+	else if (_tcsstr(strTime.c_str(), _T("m")) || _tcsstr(strTime.c_str(), _T("M")))
+	{
+		nTimes = _tstol(strTime.c_str()) * 6000;
+	}
+	else
+	{
+		// 不带单位或其他的,默认ms;
+		nTimes = _tstol(strTime.c_str());
+	}
+
+	return nTimes;
+}
+
+int parse_cmd_param_from_file(TCHAR *file_name, std::vector<CommandParam> &vtCommandParams)
+{
+	TCHAR buf[MAX_PATH] = {0};
 	TCHAR name[MAX_PATH] = {0};
 	TCHAR head[MAX_PATH] = {0};
 	TCHAR code[MAX_PATH] = {0};
-	TCHAR mark[MAX_PATH] = {0};
+	TCHAR param[MAX_PATH] = {0};
 	TCHAR multicode[MAX_PATH] = {0};
 	TCHAR cmd_wait_time[MAX_PATH] = {0};
 	TCHAR read_wait_time[MAX_PATH] = {0};
@@ -46,7 +72,7 @@ int parse_cmd_param_from_file(char *file_name)
 	int ret = -1;
 	FILE *fp = NULL;
 	
-	if(!file_name)
+	if(!file_name || file_name[0] == '\0' )
 		return ret;
 
 	fp = fopen(file_name, "r");
@@ -59,7 +85,7 @@ int parse_cmd_param_from_file(char *file_name)
 		unsigned int protocol = 0;
 		unsigned int scancode = 0;
 
-		tmp_len = _tcslen((char*)buf);
+		tmp_len = _tcslen(buf);
 		if(tmp_len >= 1) {
 			if (buf[tmp_len - 1] == '\r' || buf[tmp_len - 1] == '\n')
 				buf[tmp_len - 1] = 0;
@@ -70,22 +96,30 @@ int parse_cmd_param_from_file(char *file_name)
 		}
 
 #if _MSC_VER >= 1500 
-		//if ( sscanf_s(buf, "%[^;];%[^;];%[^;];%[^;];%[^;];%[^;];%[^;]", name, MAX_PATH, head, MAX_PATH, code, MAX_PATH, mark, MAX_PATH, multicode, MAX_PATH, cmd_wait_time, MAX_PATH, read_wait_time, MAX_PATH) == 7)
-		if ( sscanf_s(buf, "%[^;];%[^;];%[^;];%[^;];%[^;];%[^;];%s", name, MAX_PATH, head, MAX_PATH, code, MAX_PATH, mark, MAX_PATH, multicode, MAX_PATH, cmd_wait_time, MAX_PATH, read_wait_time, MAX_PATH) == 7)
+		//if ( _stscanf_s(buf, "%[^;];%[^;];%[^;];%[^;];%[^;];%[^;];%[^;]", name, MAX_PATH, head, MAX_PATH, code, MAX_PATH, param, MAX_PATH, multicode, MAX_PATH, read_wait_time, MAX_PATH, cmd_wait_time, MAX_PATH) == 7) // 等价下面;
+		if ( _stscanf_s(buf, "%[^;];%[^;];%[^;];%[^;];%[^;];%[^;];%s", name, MAX_PATH, head, MAX_PATH, code, MAX_PATH, param, MAX_PATH, multicode, MAX_PATH, read_wait_time, MAX_PATH, cmd_wait_time, MAX_PATH) == 7)
 #endif
 #if _MSC_VER < 1500 
-		if ( sscanf(buf, "%[^;];%[^;];%[^;];%[^;];%[^;];%[^;];%[^;]", name, head, code, mark, multicode, cmd_wait_time, read_wait_time) == 7)
+		if ( sscanf(buf, "%[^;];%[^;];%[^;];%[^;];%[^;];%[^;];%[^;]", name, head, code, param, multicode, read_wait_time, cmd_wait_time) == 7)
 #endif
 		{
 			CommandParam cp;
 			parse_key(cp.name, name, _T("Name"));
 			parse_key(cp.head, head, _T("HeadCode"));
 			parse_key(cp.code, code, _T("Command"));
-			parse_key(cp.mark, mark, _T("CMDParam"));
+			parse_key(cp.param, param, _T("CMDParam"));
+			
 			std::string value;
 			parse_key(value, multicode, _T("MultiParams"));
-			parse_key(value, cmd_wait_time, _T("ReadWaitTime"));
-			parse_key(value, read_wait_time, _T("CMDWaitTime"));
+			cp.bMulticode = !_tcsicmp(value.c_str(), _T("true")) ? true : false;
+
+			parse_key(value, read_wait_time, _T("ReadWaitTime"));
+			cp.read_wait_time = pares_time_value(value.c_str());
+
+			parse_key(value, cmd_wait_time, _T("CMDWaitTime"));
+			cp.cmd_wait_time = pares_time_value(value.c_str());
+
+			vtCommandParams.push_back(cp);
 		}
 	}
 

+ 8 - 12
Serail-Demo/Demo/stdafx.h

@@ -33,20 +33,16 @@
 
 typedef struct __CMDPARAM__
 {
-	std::string name; // 命令描述;
-	std::string head; // 命令头;
-	std::string code; // 命令码;
-	std::string mark; // 特殊标记;
-	bool bMulticode ;	// 命令码是否多参数;
-	/*TCHAR name[MAX_PATH];
-	TCHAR head[MAX_PATH];
-	TCHAR code[MAX_PATH];
-	TCHAR mark[MAX_PATH];
-	TCHAR multicode[MAX_PATH];*/
-	int cmd_wait_time ;	// 两条串口指令间隔时间;
+	std::string name;		// 命令描述;
+	std::string head;		// 命令头;
+	std::string code;		// 命令码;
+	std::string param;		// 命令码参数;
+	bool bMulticode ;		// 命令码是否多参数;
+	int cmd_wait_time ;		// 两条串口指令间隔时间;
 	int read_wait_time ;	// 写完串口后,等待多久读;
 } CommandParam, *pCommandParam;
 
+int pares_time_value(std::string strTime);
 bool parse_key(std::string &value, std::string strLine, TCHAR *lpszText);
-int parse_cmd_param_from_file(char *file_name);
+int parse_cmd_param_from_file(char *file_name, std::vector<CommandParam> &vtCommandParams);
 // TODO: 在此处引用程序需要的其他头文件