RobotManage.cs
8.8 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using Asa;
using OnlineStore;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using X_Ray;
namespace DeviceLibrary
{
public static class RobotManage
{
public static MainMachine mainMachine;
public static Robot_Config Config;
public static bool IsLoadOk = true;
public static bool IsConfigMode = false;
public static bool InoutDebugMode = false;
public delegate void LoadFinish(bool state,string msg);
public static event LoadFinish LoadFinishEvent;
public static event EventHandler<bool> UserPauseSet;
public static volatile bool isRunning = false;
public static Dictionary<string, InCarReelPostion> InCarReelPostion;
public static Dictionary<string, OutCarReelPostion> OutCarReelPostion;
public static Dictionary<string, LabellingPostion> LabellingPostion;
static List<string> _positionNumList=null;
public static List<string> PositionNumList { get {
if (_positionNumList == null)
{
_positionNumList =new List<string>();
_positionNumList =_positionNumList.Union(InCarReelPostion.Keys.ToArray()).ToList();
_positionNumList =_positionNumList.Union(OutCarReelPostion.Keys.ToArray()).ToList();
_positionNumList =_positionNumList.Union(LabellingPostion.Keys.ToArray()).ToList();
}
return _positionNumList;
}
}
static readonly string baseDir = Application.StartupPath;
static Thread mainThread;
public static HIKCamera CameraA=new HIKCamera();
public static PrinterHelper printerHelper = new PrinterHelper();
public static XRay XRay = new XRay("XRay", XRayType.Spellman);
public static XrayImage xrayImage;
public static void Init() {
string msg = "";
try
{
mainMachine = null;
string configFile = Path.Combine(baseDir, "config\\Config.csv");
Config = new Robot_Config(0, "", configFile);
Config = (Robot_Config)CSVConfigReader.LoadConfig(Config);
string carReelPostionFile = "config\\InCarReelPostion.csv";
if (File.Exists(carReelPostionFile))
{
LogUtil.info("加载In料串位置文件:" + carReelPostionFile);
InCarReelPostion = CSVPositionReader<InCarReelPostion>.LoadCSVFile(carReelPostionFile);
}
else {
IsLoadOk = false;
msg += "找不到料串位置文件" + "\n";
}
string outcarReelPostionFile = "config\\OutCarReelPostion.csv";
if (File.Exists(carReelPostionFile))
{
LogUtil.info("加载Out料串位置文件:" + outcarReelPostionFile);
OutCarReelPostion = CSVPositionReader<OutCarReelPostion>.LoadCSVFile(outcarReelPostionFile);
}
else
{
IsLoadOk = false;
msg += "找不到料串位置文件" + "\n";
}
xrayImage = new XrayImage("XrayImage", XrayImage.DeviceType.HAOBO);
string labellingPostionFile = "config\\LabellingPostion.csv";
if (File.Exists(labellingPostionFile))
{
LogUtil.info("加载贴标位置:" + labellingPostionFile);
LabellingPostion = CSVPositionReader<LabellingPostion>.LoadCSVFile(labellingPostionFile);
}
else
{
IsLoadOk = false;
msg += "找不到贴标位置" + "\n";
}
CodeManager.LoadConfig();
mainMachine = new MainMachine(RobotManage.Config);
LogUtil.info("load 50%");
if (!IOManager.ConnectionIOList(new List<string>()))
{
IsLoadOk = false;
msg += crc.GetString(L.iocard_init_fail, "IO板卡初始化失败")+ "\n";
}else
LogUtil.info("IO板卡初始化完成");
if (!XRay.Open(Setting_Init.XRay_Port, string.Format("{0:yyyy-MM-dd}", DateTime.Now)))
{
msg += "X光管通讯失败:"+ Setting_Init.XRay_Port+"\n";
IsLoadOk = false;
}
else
LogUtil.info("xray init ok");
Thread.Sleep(500);
if (!XRay.SetVC(Setting_Init.XRay_Voltage.ToString(), Setting_Init.XRay_Current.ToString()))
{
msg += string.Format("X光电压电流设置失败:{0},{1}", Setting_Init.XRay_Voltage, Setting_Init.XRay_Current) + "\n";
IsLoadOk = false;
}
else
LogUtil.info($"xray V:{Setting_Init.XRay_Voltage} ,C:{Setting_Init.XRay_Current} set ok");
if (!xrayImage.Open())
{
msg += "图像平板打开失败"+ "\n";
IsLoadOk = false;
}else
LogUtil.info("图像平板打开成功");
if (!printerHelper.Connection(Setting_Init.Printer_IP)) {
msg += "标签打印机打开失败" + "\n";
IsLoadOk = false;
}else
LogUtil.info("标签打印机打开成功");
LogUtil.info("设备初始化完成");
}
catch (Exception ex) {
LoadFinishEvent?.Invoke(false, ex.Message);
LogUtil.error(ex.ToString());
return;
}
LoadFinishEvent?.Invoke(IsConfigMode ? IsConfigMode : IsLoadOk, msg);
}
public static void LoadDebug() {
LoadFinishEvent?.Invoke(true, crc.GetString(L.open_debug_mode,"打开调试模式"));
}
public static void Start() {
//Init();
if (!IsLoadOk)
{
LogUtil.info("系统还未加载完毕,无法启动");
if (!IsConfigMode)
return;
}
lock (mainMachine)
{
if (isRunning)
{
LogUtil.info("系统已经在运行");
return;
}
isRunning = true;
mainThread = new Thread(new ThreadStart(mainMachine.Start));
mainThread.Start();
}
GC.KeepAlive(mainThread);
Task.Run(()=> {
AxisBean.List.ForEach((x) => { AxisManager.AlarmClear(x.Config.DeviceName, x.Config.GetAxisValue()); });
Task.Delay(1000).Wait();
if (mainMachine.DeviceCheck())
mainMachine.BeginHomeReset(true);
});
}
public static void Stop()
{
LogUtil.info("开始停止系统.");
if (mainMachine != null)
{
mainMachine.Stop();
mainMachine.UserPause = false;
}
isRunning = false;
}
public static void ShutDown()
{
LogUtil.info("开始关闭系统.");
IOManager.CloseAllConnection();
CameraA.stopCamera();
}
public static void UserPause(bool userpause)
{
UserPause("", userpause);
}
public static void UserPause(string msg="",bool userpause=true) {
UserPauseSet?.Invoke(null, userpause);
mainMachine.UserPause = userpause;
if (userpause)
{
if(string.IsNullOrEmpty(msg))
LogUtil.info("用户暂停");
else
LogUtil.info("系统暂停: "+msg);
}
else
LogUtil.info("用户取消暂停:"+ msg);
}
public static void IgnoreSafecheck(bool s)
{
mainMachine.IgnoreSafecheck = s;
if (s)
LogUtil.info("用户设置忽略安全检查");
else
LogUtil.info("用户取消忽略安全检查");
}
public static void IgnoreGratingSignal(bool s)
{
mainMachine.IgnoreGratingSignal = s;
if (s)
LogUtil.info("用户设置忽略安全光栅");
else
LogUtil.info("用户取消忽略安全光栅");
}
}
public enum StoreType {
TypeA,TypeB
}
}