STLinkCliWrapService.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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, string port = "", string customDefaultOption = null)
  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. SetPortStringParam(port);
  19. SetCustomDefaultOptionParam(customDefaultOption);
  20. }
  21. public delegate void OnMsgRecevicedEvent(string msg);
  22. public event OnMsgRecevicedEvent OnMsgReceviced;
  23. public string CliPath => cliPath;
  24. private readonly string cliPath;
  25. private string portParamString;
  26. private string defaultOptionString;
  27. public int SetDefaultOptions()
  28. {
  29. return RunConsole(cliPath, $"{portParamString} {defaultOptionString}");
  30. }
  31. public int StartProgram(string filePath)
  32. {
  33. return RunConsole(cliPath, $"{portParamString} -P \"{filePath}\" 0x08000000 -V \"after_programing\"");
  34. }
  35. public int Rest()
  36. {
  37. return RunConsole(cliPath, $"{portParamString} -Rst");
  38. }
  39. public int Check(string filePath)
  40. {
  41. return RunConsole(cliPath, $"{portParamString} -CmpFile \"{filePath}\" 0x08000000");
  42. }
  43. public int GetCheckSum(string filePath)
  44. {
  45. return RunConsole(cliPath, $"{portParamString} -Cksum \"{filePath}\"");
  46. }
  47. private int RunConsole(string exePath, string param)
  48. {
  49. var pInfo = new ProcessStartInfo() {
  50. FileName = exePath,
  51. Arguments = param,
  52. RedirectStandardOutput = true,
  53. CreateNoWindow = true,
  54. UseShellExecute = false,
  55. };
  56. var p = Process.Start(pInfo);
  57. p.OutputDataReceived += P_OutputDataReceived;
  58. p.BeginOutputReadLine();
  59. p.WaitForExit();
  60. p.Exited += P_Exited;
  61. return p.ExitCode;
  62. }
  63. private void P_Exited(object sender, EventArgs e)
  64. {
  65. }
  66. private void P_OutputDataReceived(object sender, DataReceivedEventArgs e)
  67. {
  68. OnMsgReceviced?.Invoke(e.Data);
  69. }
  70. private void SetPortStringParam(string port)
  71. {
  72. if (string.IsNullOrEmpty(port))
  73. {
  74. portParamString = string.Empty;
  75. return;
  76. }
  77. portParamString = $"-c {port}";
  78. }
  79. private void SetCustomDefaultOptionParam(string customDefaultOption)
  80. {
  81. if (customDefaultOption != null)
  82. {
  83. defaultOptionString = customDefaultOption;
  84. return;
  85. }
  86. defaultOptionString = $"-OB RDP=0 BOR_LEV=3 IWDG_SW=1 nRST_STOP=1 nRST_STDBY=1";
  87. }
  88. }
  89. }