geboshi_V1/LeatherProject/LeatherApp/DevContainer.cs
2024-03-07 14:03:22 +08:00

192 lines
8.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using LeatherApp.Device;
using LeatherApp.Interface;
using Newtonsoft.Json.Linq;
using S7.Net;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace LeatherApp
{
public class DevContainer
{
public PLCDev devPlc;
public PhotoLib libPhoto;
public ScannerCodeDev devCodeScanner;//= new CodeScannerDev();
public IOCardDev devIOCard;//=new IOCardDev();
public LightDev devLight;//=new LightDev();
public ABSCamerCardDev devCamer1;
public ABSCamerCardDev devCamer2;
public DefectLib libDefect;//=new DefectLib();
public Action<DateTime,WarningEnum, string> WarningEvent;
public Action<bool, string> StateChange;
public Action<string, string> OutDebugEvent;
public bool state = false;
private Thread t;
private PictureBox preView1, preView2;
public void start(PictureBox view1, PictureBox view2)
{
this.preView1 = view1;
this.preView2 = view2;
//devCodeScanner = new ScannerCodeDev();
//devCodeScanner.WarningEvent = WarningEvent;
//if (!devCodeScanner.start()) throw new Exception("扫码器初始化失败!");
t = new System.Threading.Thread(run);
t.IsBackground = true;
t.Start();
}
public void stop()
{
try
{
state = false;
try { devIOCard.stop(); } catch { }
try { devPlc.stop(); } catch { }
try { devLight.stop(); } catch { }
try { devCamer1.stop(); devCamer1.close(); } catch { }
try { devCamer2.stop(); devCamer2.close(); } catch { }
try { devCodeScanner.stop(); } catch { }
try { libDefect.stop(); } catch { }
try { libPhoto.stop(); } catch { }
}
catch { }
}
private void run()
{
try
{
WarningEvent?.BeginInvoke(DateTime.Now,WarningEnum.Normal, "初始化设备...",null,null);
devCodeScanner = new ScannerCodeDev();
devCodeScanner.WarningEvent = WarningEvent;
if (!Config.StopCodeScanner)
devCodeScanner.start();
devPlc = new PLCDev();
devPlc.WarningEvent = WarningEvent;
libPhoto = new PhotoLib();
libPhoto.WarningEvent = WarningEvent;
libDefect = new DefectLib();
libDefect.WarningEvent = WarningEvent;
devIOCard = new IOCardDev();
devIOCard.WarningEvent = WarningEvent;
devLight = new LightDev(Config.Light_Name);
devLight.WarningEvent = WarningEvent;
if (Config.Camer_Name == CamerDevNameEnum.)
{
devCamer2 = new CamerCardDev();
devCamer1 = new CamerCardDev();
}
else
{
devCamer1 = new CamerCardDevIK();
devCamer2 = new CamerCardDevIK();
}
devCamer1.WarningEvent = WarningEvent;
devCamer2.WarningEvent = WarningEvent;
//启动
string appBasePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
WarningEvent?.Invoke(DateTime.Now,WarningEnum.Normal, "1");
//WarningEvent?.Invoke(DateTime.Now,WarningEnum.Normal, "打开设备...");
if (!Config.StopIO && !devIOCard.start(Config.IOCard_DeviceNum)) throw new Exception("I/O板卡初始化失败");
if (!Config.StopPLC && !devPlc.start(CpuType.S71200, Config.PlcIPAdrees, Config.PlcRackN, Config.PlcSolt)) throw new Exception("Plc连接失败");
if (!Config.StopLight && !devLight.start(int.Parse(Config.Light_PortName.Substring(3)))) throw new Exception("光源设备初始化失败!");
if (!libPhoto.start()) throw new Exception("图像库初始化失败!");
WarningEvent?.Invoke(DateTime.Now,WarningEnum.Normal, "2");
if (!libDefect.start()) throw new Exception("缺陷库初始化失败!");
WarningEvent?.Invoke(DateTime.Now,WarningEnum.Normal, "3");
if (!File.Exists(appBasePath+ "\\DevCfg\\" + Config.Carmer1ConfigFilePath) || !File.Exists(appBasePath + "\\DevCfg\\" + Config.Carmer2ConfigFilePath))
throw new Exception($"相机配置文件不存在({Config.Carmer1ConfigFilePath}或{Config.Carmer2ConfigFilePath})");
string scanner1Path = appBasePath + "\\temp\\";
if (!Directory.Exists(scanner1Path + "scanner\\")) Directory.CreateDirectory(scanner1Path + "scanner\\");
if (!devCamer2.open(1, 0)) throw new Exception("相机初始化失败!");
if (!devCamer2.start(this.preView1, scanner1Path + "scanner\\")) throw new Exception("相机1打开失败");
if (!devCamer1.open(0, 0)) throw new Exception("相机初始化失败!");
if (!devCamer1.start(this.preView2, scanner1Path + "scanner\\")) throw new Exception("相机0打开失败");
//
state = true;
StateChange?.Invoke(true, "成功");
}
catch (Exception ex)
{
stop();
StateChange?.Invoke(false, ex.Message);
}
}
/// <summary>
/// I/O指令输出
/// </summary>
/// <param name="key">CMDName</param>
/// <param name="isStrobe">频闪</param>
/// <param name="recover">输出sleep下后恢复原信号</param>
/// <param name="recoverWaitTime">sleep多久后反转</param>
/// <returns></returns>
public bool io_output(CMDName key, bool isStrobe = false, bool recover = false, int recoverWaitTime = 100)
{
if (Config.CMDProcess.ContainsKey(key))
return io_output(key.ToString(), Config.CMDProcess[key], isStrobe, recover, recoverWaitTime);
return false;
}
/// <summary>
/// I/O指令输出
/// </summary>
/// <param name="processParam"></param>
/// <param name="isStrobe">频闪</param>
/// <param name="recover">输出sleep(recoverWaitTime)下,后恢复原信号</param>
/// <param name="recoverWaitTime">sleep 后反转</param>
public bool io_output(string tagName, JObject processParam, bool isStrobe = false, bool recover = false, int recoverWaitTime = 100)
{
bool result = false;
string[] OUT_OP_SHOW = processParam.Value<JArray>("OUT_OP_SHOW").ToObject<List<string>>().ToArray();
OUT_OP_SHOW = Utils.Util.IODataFormatBinaryStr(OUT_OP_SHOW, true);
for (int i = 0; i < OUT_OP_SHOW.Length; i++)
{
for (int j = 0; j < OUT_OP_SHOW[i].Length; j++)
{
int jj = OUT_OP_SHOW[i].Length - j - 1;
if (OUT_OP_SHOW[i][jj] == 'L' || OUT_OP_SHOW[i][jj] == 'H')
{
if (recover)
{
if (recoverWaitTime > 0)
{
OutDebugEvent?.Invoke(tagName, $"向I/O输出引脚{i}-{j},信号{OUT_OP_SHOW[i][jj]},输出时长:{recoverWaitTime}ms");
devIOCard.writeBitState(i, j, OUT_OP_SHOW[i][jj] == 'H', isStrobe);
Thread.Sleep(recoverWaitTime);
}
OutDebugEvent?.Invoke(tagName, $"向I/O输出引脚{i}-{j},信号{OUT_OP_SHOW[i][jj] == 'L'}");
devIOCard.writeBitState(i, j, OUT_OP_SHOW[i][jj] == 'L');
}
else
{
OutDebugEvent?.Invoke(tagName, $"向I/O输出引脚{i}-{j},信号{OUT_OP_SHOW[i][jj]}");
devIOCard.writeBitState(i, j, OUT_OP_SHOW[i][jj] == 'H', isStrobe);
}
result = true;
}
}
}
return result;
}
}
}