类文件:
namespace WaferSawRouteInspection
{
public class ClassVisionPythonScript
{
private Process p;
public ClassVisionPythonScript(string path_python_exe, string path_pyfile)
{
p = new Process();
p.StartInfo.FileName = path_python_exe;
p.StartInfo.Arguments = path_pyfile;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
}
// public string run(img)
public string run()
{
p.Start();
// img.GetArray(out var data);
// string value = string.Join(',', Array.ConvertAll(data, (byte d) => d.ToString()));
// p.StandardInput.WriteLine(value);
p.StandardInput.WriteLine("执行成功");
string result = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
return result;
}
}
}
调用方法:
string python_exe = @"C:\Users\xxxxx\AppData\Local\Programs\Python\Python38\python.exe";
var runpython = new ClassVisionPythonScript(python_exe, python_para);
AddToMainWindowLog("启动检测任务,传递参数:"+ python_para);
string msg = runpython.run();
msg获取脚本返回的结果