STLinkCliWrapService.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. namespace ST_LINK_MES.Service
  8. {
  9. public class STLinkCliWrapService
  10. {
  11. public STLinkCliWrapService(string cliPath)
  12. {
  13. if (string.IsNullOrEmpty(cliPath))
  14. {
  15. cliPath = "C:\\Program Files (x86)\\STMicroelectronics\\STM32 ST-LINK Utility\\ST-LINK Utility\\ST-LINK_CLI.exe";
  16. }
  17. this.cliPath = cliPath;
  18. }
  19. public delegate void OnMsgRecevicedEvent(string msg);
  20. public event OnMsgRecevicedEvent OnMsgReceviced;
  21. public string CliPath => cliPath;
  22. private readonly string cliPath;
  23. public int SetDefaultOptions()
  24. {
  25. return RunConsole(cliPath, $"-OB RDP=0 BOR_LEV=3 IWDG_SW=1 nRST_STOP=1 nRST_STDBY=1");
  26. }
  27. public int StartProgram(string filePath)
  28. {
  29. return RunConsole(cliPath, $"-P \"{filePath}\" 0x08000000");
  30. }
  31. public int Rest()
  32. {
  33. return RunConsole(cliPath, $"-Rst");
  34. }
  35. public int Check(string filePath)
  36. {
  37. return RunConsole(cliPath, $"-CmpFile \"{filePath}\"");
  38. }
  39. public int GetCheckSum(string filePath)
  40. {
  41. return RunConsole(cliPath, $"-Cksum \"{filePath}\"");
  42. }
  43. private int RunConsole(string exePath, string param)
  44. {
  45. var pInfo = new ProcessStartInfo() {
  46. FileName = exePath,
  47. Arguments = param,
  48. RedirectStandardOutput = true,
  49. CreateNoWindow = true,
  50. UseShellExecute = false,
  51. };
  52. var p = Process.Start(pInfo);
  53. p.OutputDataReceived += P_OutputDataReceived;
  54. p.BeginOutputReadLine();
  55. p.WaitForExit();
  56. p.Exited += P_Exited;
  57. return p.ExitCode;
  58. }
  59. private void P_Exited(object sender, EventArgs e)
  60. {
  61. }
  62. private void P_OutputDataReceived(object sender, DataReceivedEventArgs e)
  63. {
  64. OnMsgReceviced?.Invoke(e.Data);
  65. }
  66. }
  67. }