12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Text;
- namespace ST_LINK_MES.Service
- {
- public class STLinkCliWrapService
- {
- public STLinkCliWrapService(string cliPath)
- {
- if (string.IsNullOrEmpty(cliPath))
- {
- cliPath = "C:\\Program Files (x86)\\STMicroelectronics\\STM32 ST-LINK Utility\\ST-LINK Utility\\ST-LINK_CLI.exe";
- }
- this.cliPath = cliPath;
- }
- public delegate void OnMsgRecevicedEvent(string msg);
- public event OnMsgRecevicedEvent OnMsgReceviced;
- public string CliPath => cliPath;
- private readonly string cliPath;
- public int SetDefaultOptions()
- {
- return RunConsole(cliPath, $"-OB RDP=0 BOR_LEV=3 IWDG_SW=1 nRST_STOP=1 nRST_STDBY=1");
- }
- public int StartProgram(string filePath)
- {
- return RunConsole(cliPath, $"-P \"{filePath}\" 0x08000000");
- }
- public int Rest()
- {
- return RunConsole(cliPath, $"-Rst");
- }
- public int Check(string filePath)
- {
- return RunConsole(cliPath, $"-CmpFile \"{filePath}\"");
- }
- public int GetCheckSum(string filePath)
- {
- return RunConsole(cliPath, $"-Cksum \"{filePath}\"");
- }
- private int RunConsole(string exePath, string param)
- {
- var pInfo = new ProcessStartInfo() {
- FileName = exePath,
- Arguments = param,
- RedirectStandardOutput = true,
- CreateNoWindow = true,
- UseShellExecute = false,
-
- };
- var p = Process.Start(pInfo);
- p.OutputDataReceived += P_OutputDataReceived;
- p.BeginOutputReadLine();
- p.WaitForExit();
- p.Exited += P_Exited;
- return p.ExitCode;
- }
- private void P_Exited(object sender, EventArgs e)
- {
- }
- private void P_OutputDataReceived(object sender, DataReceivedEventArgs e)
- {
- OnMsgReceviced?.Invoke(e.Data);
- }
- }
- }
|