STLinkCliPrograrmService.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. using ST_CUBE_MES.Model;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Threading.Tasks;
  9. namespace ST_CUBE_MES.Service
  10. {
  11. public class STLinkCliPrograrmService
  12. {
  13. public STLinkCliPrograrmService(string stlCliPath = "", string port = "", string customDefaultOption = null)
  14. {
  15. this._cli = new STLinkCliWrapService(stlCliPath, port, customDefaultOption);
  16. }
  17. public delegate void OnMsgRecevicedEvent(string msg);
  18. public event OnMsgRecevicedEvent OnMsgReceviced;
  19. public delegate void OnProgressChangedEvent(int currentStep, int maxStep);
  20. public event OnProgressChangedEvent OnProgressChanged;
  21. public string CliPath => _cli.CliPath;
  22. private readonly STLinkCliWrapService _cli;
  23. public Result StartProgramProcess(string filePath)
  24. {
  25. Result result = new Result() { IsSuccess = false };
  26. result.Step = 1;
  27. var setOptionByteResult = SetOptionByte();
  28. if (setOptionByteResult != null)
  29. {
  30. result.ErrorMsg = setOptionByteResult.ErrorMsg;
  31. foreach (var pair in setOptionByteResult.Data)
  32. {
  33. result.Data[pair.Key] = pair.Value;
  34. }
  35. }
  36. if (setOptionByteResult == null || !setOptionByteResult.IsSuccess)
  37. {
  38. return result;
  39. }
  40. OnProgressChanged?.Invoke(1, 4);
  41. result.Step = 2;
  42. var eraseResult = Erase();
  43. if (eraseResult != null)
  44. {
  45. result.ErrorMsg = eraseResult.ErrorMsg;
  46. foreach (var pair in eraseResult.Data)
  47. {
  48. result.Data[pair.Key] = pair.Value;
  49. }
  50. }
  51. if (eraseResult == null || !eraseResult.IsSuccess)
  52. {
  53. return result;
  54. }
  55. OnProgressChanged?.Invoke(2, 4);
  56. result.Step = 3;
  57. var startProgramResult = StartProgram(filePath);
  58. if (startProgramResult != null)
  59. {
  60. result.ErrorMsg = startProgramResult.ErrorMsg;
  61. foreach (var pair in startProgramResult.Data)
  62. {
  63. result.Data[pair.Key] = pair.Value;
  64. }
  65. }
  66. if (startProgramResult == null || !startProgramResult.IsSuccess)
  67. {
  68. return result;
  69. }
  70. OnProgressChanged?.Invoke(3, 4);
  71. result.Step = 4;
  72. var getCheckSumResult = GetCheckSum(filePath);
  73. if (getCheckSumResult != null)
  74. {
  75. result.ErrorMsg = getCheckSumResult.ErrorMsg;
  76. foreach (var pair in getCheckSumResult.Data)
  77. {
  78. result.Data[pair.Key] = pair.Value;
  79. }
  80. }
  81. if (getCheckSumResult == null || !getCheckSumResult.IsSuccess)
  82. {
  83. return result;
  84. }
  85. OnProgressChanged?.Invoke(4, 4);
  86. result.IsSuccess = true;
  87. return result;
  88. }
  89. public Result SetOptionByte()
  90. {
  91. var result = new Result() { IsSuccess = true };
  92. _cli.OnMsgReceviced += stlCli_OnMsgReceviced_SetDefaultOptions;
  93. var resultInt = _cli.SetOptionByte();
  94. _cli.OnMsgReceviced -= stlCli_OnMsgReceviced_SetDefaultOptions;
  95. result.IsSuccess = resultInt == 0;
  96. return result;
  97. void stlCli_OnMsgReceviced_SetDefaultOptions(string msg)
  98. {
  99. if (msg is null)
  100. return;
  101. if (msg.Contains("Error: No debug probe detected"))
  102. {
  103. result.ErrorMsg = "Error: No debug probe detected";
  104. }
  105. OnMsgReceviced?.Invoke(msg);
  106. }
  107. }
  108. public Result Erase()
  109. {
  110. var result = new Result() { IsSuccess = true };
  111. _cli.OnMsgReceviced += stlCli_OnMsgReceviced_SetDefaultOptions;
  112. var resultInt = _cli.Erase();
  113. _cli.OnMsgReceviced -= stlCli_OnMsgReceviced_SetDefaultOptions;
  114. result.IsSuccess = resultInt == 0;
  115. return result;
  116. void stlCli_OnMsgReceviced_SetDefaultOptions(string msg)
  117. {
  118. if (msg is null)
  119. return;
  120. if (msg.Contains("Error: No debug probe detected"))
  121. {
  122. result.ErrorMsg = "Error: No debug probe detected";
  123. }
  124. OnMsgReceviced?.Invoke(msg);
  125. }
  126. }
  127. public Result GetCheckSum(string filePath)
  128. {
  129. if (!File.Exists(filePath))
  130. {
  131. return new Result()
  132. {
  133. IsSuccess = false,
  134. ErrorMsg = "ERROR: Check file not found",
  135. };
  136. }
  137. var result = new Result() { IsSuccess = true };
  138. _cli.OnMsgReceviced += stlCli_OnMsgReceviced_StartProgram;
  139. var resultInt = _cli.GetCheckSum(filePath);
  140. _cli.OnMsgReceviced -= stlCli_OnMsgReceviced_StartProgram;
  141. result.IsSuccess = resultInt == 0;
  142. return result;
  143. void stlCli_OnMsgReceviced_StartProgram(string msg)
  144. {
  145. if (msg is null)
  146. return;
  147. Regex checksumRegex = new Regex("^(.*)Checksum = (0x.*)$");
  148. var match = checksumRegex.Match(msg);
  149. if (match.Success &&
  150. match.Groups.Count > 2 &&
  151. match.Groups[2].Value.StartsWith("0x"))
  152. {
  153. var checksum = match.Groups[2].Value;
  154. result.Data.Add("checksum", checksum);
  155. result.Data.Add("fileName", Path.GetFileName(filePath));
  156. OnMsgReceviced?.Invoke($"checksum: {checksum}");
  157. }
  158. if (msg.Contains("Error: No debug probe detected"))
  159. {
  160. result.ErrorMsg = "Error: No debug probe detected";
  161. }
  162. OnMsgReceviced?.Invoke(msg);
  163. }
  164. }
  165. public Result StartProgram(string filePath)
  166. {
  167. if (!File.Exists(filePath))
  168. {
  169. return new Result()
  170. {
  171. IsSuccess = false,
  172. ErrorMsg = "ERROR: Program file not found",
  173. };
  174. }
  175. var result = new Result() { IsSuccess = true };
  176. _cli.OnMsgReceviced += stlCli_OnMsgReceviced_StartProgram;
  177. var resultInt = _cli.StartProgram(filePath);
  178. _cli.OnMsgReceviced -= stlCli_OnMsgReceviced_StartProgram;
  179. result.IsSuccess = resultInt == 0;
  180. return result;
  181. void stlCli_OnMsgReceviced_StartProgram(string msg)
  182. {
  183. if (msg is null)
  184. return;
  185. if (msg.Contains("Error: No debug probe detected"))
  186. {
  187. result.ErrorMsg = "Error: No debug probe detected";
  188. }
  189. OnMsgReceviced?.Invoke(msg);
  190. }
  191. }
  192. public Result GetMemCheckSum()
  193. {
  194. var result = new Result() { IsSuccess = true };
  195. _cli.OnMsgReceviced += stlCli_OnMsgReceviced_SetDefaultOptions;
  196. var resultInt = _cli.GetMemCheckSum();
  197. _cli.OnMsgReceviced -= stlCli_OnMsgReceviced_SetDefaultOptions;
  198. result.IsSuccess = resultInt == 0;
  199. return result;
  200. void stlCli_OnMsgReceviced_SetDefaultOptions(string msg)
  201. {
  202. if (msg is null)
  203. return;
  204. Regex checksumRegex = new Regex("^(.*)Checksum : (0x.*)$");
  205. var match = checksumRegex.Match(msg);
  206. if (match.Success &&
  207. match.Groups.Count > 2 &&
  208. match.Groups[2].Value.StartsWith("0x"))
  209. {
  210. var checksum = match.Groups[2].Value;
  211. //result.Data.Add("checksum", checksum);
  212. OnMsgReceviced?.Invoke($"memory checksum: {checksum}");
  213. }
  214. if (msg.Contains("Error: No debug probe detected"))
  215. {
  216. result.ErrorMsg = "Error: No debug probe detected";
  217. }
  218. OnMsgReceviced?.Invoke(msg);
  219. }
  220. }
  221. }
  222. }