123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- 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, string port = "", string customDefaultOption = null)
- {
- if (string.IsNullOrEmpty(cliPath))
- {
- cliPath = "C:\\Program Files (x86)\\STMicroelectronics\\STM32 ST-LINK Utility\\ST-LINK Utility\\ST-LINK_CLI.exe";
- }
- this.cliPath = cliPath;
- SetPortStringParam(port);
- SetCustomDefaultOptionParam(customDefaultOption);
- }
- public delegate void OnMsgRecevicedEvent(string msg);
- public event OnMsgRecevicedEvent OnMsgReceviced;
- public string CliPath => cliPath;
- private readonly string cliPath;
- private string portParamString;
- private string defaultOptionString;
- public int SetDefaultOptions()
- {
- return RunConsole(cliPath, $"{portParamString} {defaultOptionString}");
- }
- public int StartProgram(string filePath)
- {
- return RunConsole(cliPath, $"{portParamString} -P \"{filePath}\" 0x08000000 -V \"after_programing\"");
- }
- public int Rest()
- {
- return RunConsole(cliPath, $"{portParamString} -Rst");
- }
- public int Check(string filePath)
- {
- return RunConsole(cliPath, $"{portParamString} -CmpFile \"{filePath}\" 0x08000000");
- }
- public int GetCheckSum(string filePath)
- {
- return RunConsole(cliPath, $"{portParamString} -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);
- }
- private void SetPortStringParam(string port)
- {
- if (string.IsNullOrEmpty(port))
- {
- portParamString = string.Empty;
- return;
- }
- portParamString = $"-c {port}";
- }
- private void SetCustomDefaultOptionParam(string customDefaultOption)
- {
- if (customDefaultOption != null)
- {
- defaultOptionString = customDefaultOption;
- return;
- }
- defaultOptionString = $"-OB RDP=0 BOR_LEV=3 IWDG_SW=1 nRST_STOP=1 nRST_STDBY=1";
- }
- }
- }
|