我正在嘗試從 Windows 表單應用程式內部運行 bash 腳本。我的代碼如下所示:
// Command String
string flashNVS = "c:\\msys32\\usr\\bin\\env MSYSTEM=MINGW32 "
"c:\\msys32\\usr\\bin\\bash -l -c "
"\"cd " unixProdPath
" && ./provision-device-secure " deviceId " \"";
cmdProcess.ExecuteCommandSync(flashNVS);
使用 cmdProcess 代碼:
public static void ExecuteCommandSync(object command)
{
flashOK = 0;
try
{
// create the ProcessStartInfo using "cmd" as the program to be run, and "/c " as the parameters.
// Incidentally, /c tells cmd that we want it to execute the command that follows, and then exit.
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " command);
// The following commands are needed to redirect the standard output.
//This means that it will be redirected to the Process.StandardOutput StreamReader.
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardError = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
// Get the output into a string
string result = proc.StandardOutput.ReadToEnd();
//resultString = result;
if ( (result.IndexOf("Hard resetting via RTS pin") > 0) || // Success FW Flash OR NVS Flash!
(result.IndexOf("Hash of data verified") > 0) ) // Success NVS Flash
{
Console.WriteLine("SUCCESS : FlashOK = 1");
flashOK = 1;
}
// Display the command output.
Console.WriteLine(result);
}
catch (Exception objException)
{
// Log the exception
Console.WriteLine("ExecuteCommandSync failed" objException.Message);
}
}
檔案 ./provision-device-secure 腳本做了很多事情。它作業正常,除了一行:
$IDF_PATH/components/esptool_py/esptool/esptool.py -p $ESPPORT --before default_reset --after no_reset write_flash 0x3E0000 mfg_config_encrypted.bin 0x3E8000 nvs_keys.bin
完全莫名其妙的是,如果我直接從 git bash 內部運行腳本,它作業正常。但是從 Windows 表單應用程式內部,該行沒有正確完成......我需要它作為我正在撰寫的 Windows 應用程式的一部分運行。關于如何調整我的代碼以使其運行的任何建議?并解釋為什么不這樣做?
只是為了描述失敗。在 git bash 中,它使用以下輸出執行:
Created encryption keys: ===> C:\Users\thebi\esp\esp32_repo\11_SN_WI_003_WiFi\prod\keys\nvs_keys.bin
Creating NVS binary with version: V2 - Multipage Blob Support Enabled
Created NVS binary: ===> C:\Users\thebi\esp\esp32_repo\11_SN_WI_003_WiFi\prod\mfg_config_encrypted.bin
但在 Windows 表單中,它只執行以下操作:
Created encryption keys: ===> C:/Users/thebi/esp/esp32_repo/11_SN_WI_003_WiFi/prod/keys/nvs_keys.bin
Creating NVS binary with version: V2 - Multipage Blob Support Enabled
并且輸出檔案 (mfg_config_encrypted.bin) 的長度為零...
uj5u.com熱心網友回復:
首先,您應該參考您的引數,因為它們里面有空格。其次......我會給你我寫的課程:
public delegate void ProcessMessageDelegate(object sender, string msg);
public class ProcessManager
{
public event ProcessMessageDelegate OnOutputData;
public event ProcessMessageDelegate one rrorData;
public event EventHandler OnProcessEnd;
Process process;
List<string> outputBuffer = new List<string>();
public string GetProcessOutput(string cmdName, List<string> args)
{
InitProcess(cmdName, args);
string result = "";
if (process.Start())
{
try
{
process.BeginOutputReadLine();
process.StandardError.ReadToEnd();
process.WaitForExit();
return OutputBufferToStr();
}
catch (Exception ex)
{
if (string.IsNullOrWhiteSpace(result))
{
one rrorData?.Invoke(this, ex.Message);
return null;
}
else
return result;
}
}
return result;
}
public void RunProcessAsync(string cmdName, List<string> args)
{
InitProcess(cmdName, args);
if (process.Start())
{
process.BeginOutputReadLine();
process.StandardError.ReadToEnd();
}
}
void InitProcess(string cmdName, List<string> args)
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = cmdName;
List<string> escapedArgs = new List<string>();
foreach (var arg in args)
{
string validArg = arg;
if (validArg.Contains(" "))
validArg = $"\"{validArg}\"";
escapedArgs.Add(validArg);
}
psi.Arguments = string.Join(" ", escapedArgs);
psi.ErrorDialog = true;
psi.UseShellExecute = false;
psi.RedirectStandardError = true;
psi.RedirectStandardOutput = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.CreateNoWindow = true;
process = new Process();
process.StartInfo = psi;
process.OutputDataReceived = Process_OutputDataReceived;
process.Exited = Process_Exited;
process.Disposed = Process_Disposed;
process.EnableRaisingEvents = true;
outputBuffer.Clear();
}
string OutputBufferToStr()
{
return string.Join(Environment.NewLine, outputBuffer);
}
private void Process_Disposed(object sender, EventArgs e)
{
OnProcessEnd?.Invoke(this, EventArgs.Empty);
}
private void Process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
outputBuffer.Add(e.Data);
one rrorData?.Invoke(this, e.Data);
}
private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
outputBuffer.Add(e.Data);
OnOutputData?.Invoke(this, e.Data);
}
private void Process_Exited(object sender, EventArgs e)
{
if (process != null)
process.Dispose();
OnProcessEnd?.Invoke(this, EventArgs.Empty);
}
}
隨意使用它。現在,您有兩個主要方法:
GetProcessOutput同步運行行程并將整個輸出回傳給您。您只需將路徑傳遞給行程和引數串列。
第二種方法是RunProcessAsync- 它異步運行行程。你用同樣的方式呼叫它,但你必須連接到事件: OnOutputData - 每次寫入輸出 one rrorData 時都會呼叫它 - 當錯誤發生時 OnProcessEnd - 當行程結束時。
我希望你會喜歡它。
uj5u.com熱心網友回復:
就是要把這個扔掉。我在這里非常不穩定,因為我并不真正了解這些東西的任何細節。但最終有效的是更改命令以選擇 git bash,而不是 MINW32。像這樣:
string flashNVS = "\"C:\\Program Files\\Git\\git-bash.exe\" -c \"cd " unixProdPath
" && ./provision-device-secure test-dev > unixLog.txt \"'";
Console.WriteLine(flashNVS);
不過,我不會接受我自己的答案,因為它沒有解釋為什么會這樣。如果有人能填空,那就太好了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/391157.html
