HandClientManager.cs
5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using HalconDotNet;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Management;
using System.Windows.Forms;
using TSA_V.Common;
namespace TSA_V
{
public class HandClientManager
{
public HandClientManager() { }
private static Process process = null;
public static void StartClient()
{
//判断是否启用
if (!Setting_NInit.Device_HandsVideo)
{
LogUtil.info($"hand start : Device_HandsVideo={Setting_NInit.Device_HandsVideo}");
return;
}
if( Setting_NInit.Hand_ServerPort <= 0)
{
LogUtil.info($"hand start : Hand_ServerPort={Setting_NInit.Hand_ServerPort}");
return;
}
int count = GetCameraCount();
if(count<=0)
{
LogUtil.info($"hand start : 未查询到相机");
//return;
}
//脚本所在地址
string CurrentPath = System.IO.Directory.GetCurrentDirectory();
string ScriptFileName = Application.StartupPath + @"\hands\defMTest.py";
if(!File.Exists(ScriptFileName))
{
LogUtil.error($"hand start : 文件{ScriptFileName}不存在");
return;
}
string sArguments = ScriptFileName; //脚本执行文件
//用于执行程序最后的
string sep = "-u";
process = new Process();
try
{
// process.StartInfo.FileName = @"python.exe";
//process.StartInfo.FileName = "D:\\python-3.9.6-embed-amd64\\python.exe";
string pyFile = Application.StartupPath + @"\python-3.9.6-embed-amd64\python.exe";
if (!File.Exists(pyFile))
{
LogUtil.error($"hand start : 文件{pyFile}不存在");
return;
}
process.StartInfo.FileName = pyFile;
//传递给进程
process.StartInfo.Arguments = sArguments;
process.StartInfo.UseShellExecute = false;//是否使用操作系统shell启动
process.StartInfo.CreateNoWindow = true; //是否在新窗口中启动该进程的值 (不显示程序窗口)
process.StartInfo.RedirectStandardInput = true; // 接受来自调用程序的输入信息
process.StartInfo.RedirectStandardOutput = true; // 由调用程序获取输出信息
process.StartInfo.RedirectStandardError = true; //重定向标准错误输出
process.Start();// 启动程序
process.BeginOutputReadLine();
process.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceivedText);
LogUtil.info($"defMTest.py启动成功:{process.ProcessName},{process.Id}");
//process.WaitForExit(); //等待程序执行完退出进程
//process.Close();
}
catch (Win32Exception e)
{
LogUtil.error($"文件{ScriptFileName}启动出错:" + e.ToString());
}catch(Exception ex)
{
LogUtil.error($"文件{ScriptFileName}启动出错:" + ex.ToString());
}
}
private static int GetCameraCount()
{
int cameraCount = 0;
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE Caption LIKE '%camera%'");
ManagementObjectCollection devices = searcher.Get();
foreach (ManagementObject device in devices)
{
LogUtil.info("devices : " + device.Path.Path);
cameraCount++;
}
}
catch (Exception ex)
{
LogUtil.error("出错:" + ex.ToString());
}
LogUtil.info("Number of built-in cameras: " + cameraCount);
return cameraCount;
}
public static void pythonDetector4(string[] strArr)
{
}
private static void p_OutputDataReceivedText(object sender, DataReceivedEventArgs e)
{
if (!string.IsNullOrEmpty(e.Data))
{
if (e.Data.EndsWith("200"))
{
//LogUtil.debug("pyLog: " + e.Data);
}
else
{
LogUtil.info("pyLog: " + e.Data);
}
}
}
public static void StopClient()
{
try
{
if (process != null)
{
if (!process.HasExited)
{
process.Kill();
LogUtil.info($"停止{"defMTest.py"} ");
}
}
}
catch (Exception ex)
{
LogUtil.error("出错:" + ex.ToString());
}
}
}
}