STLinkCliPrograrmService.cs 8.5 KB

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