banboshi_V1/halftoneproject-master/Code/DevContainer.cs
2023-10-31 13:19:29 +08:00

191 lines
8.9 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 Newtonsoft.Json.Linq;
using ProductionControl.Device;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static ProductionControl.Device.AxisDev;
namespace ProductionControl
{
public class DevContainer
{
public AxisDev devAxis;//=new AxisDev();
public CodeScannerDev devCodeScanner;//= new CodeScannerDev();
public HeightDev devHeight;//=new HeightDev();
public IOCardDev devIOCard;//=new IOCardDev();
public LightDev devLight;//=new LightDev();
public ScannerDev devScannerGentl;// = new ScannerDev( ScannerDev.ScannerType.GENTL);
public ScannerDev devScannerCC;// = new ScannerDev(ScannerDev.ScannerType.CC);
public SmallAxisDev devSmallAxis;// = new SmallAxisDev();
public TensionDev devTension;//=new TensionDev();
public DefectLib libDefect;//=new DefectLib();
public SizeLib libSize;
public ForLib libFor;
public IFLib libIF;
public Action<WarningEnum, string> WarningEvent;
public Action<bool, string> StateChange;
public Action<string, string> OutDebugEvent;
public Action<int, AxisPosType, double> axisPosEvent;
public bool state = false;
private Thread t;
private IntPtr preView1, preView2;
public void start(IntPtr view1, IntPtr view2)
{
this.preView1 = view1;
this.preView2 = view2;
devCodeScanner = new CodeScannerDev();
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 { devAxis.stop(); } catch { }
try { devHeight.stop(); } catch { }
try { devIOCard.stop(); } catch { }
try { devLight.stop(); } catch { }
try { devScannerGentl.stop(); devScannerGentl.close(); } catch { }
try { devScannerCC.stop(); devScannerCC.close(); } catch { }
try { devSmallAxis.stop(); } catch { }
try { devTension.stop(); } catch { }
try { libDefect.stop(); } catch { }
try { libSize.stop(); } catch { }
try { devCodeScanner.stop(); } catch { }
}
catch { }
}
private void run()
{
try
{
devAxis = new AxisDev();
devAxis.WarningEvent = WarningEvent;
devAxis.axisPosEvent = axisPosEvent;//位置POS
devHeight = new HeightDev();
devHeight.WarningEvent = WarningEvent;
devIOCard = new IOCardDev();
devIOCard.WarningEvent = WarningEvent;
devIOCard.OUTEvent = (index, data) =>
{
if (Config.HeightDevIOState && devAxis.IsInit && (devAxis.isMoveing(0) || devAxis.isMoveing(2)))
{
WarningEvent?.Invoke(WarningEnum.High, "厚度IO已打开急停请检查设备防止损坏");
//devAxis.stopNow();
}
};
devLight = new LightDev(Config.Light_Name);
devLight.WarningEvent = WarningEvent;
devScannerGentl = new ScannerDev(ScannerDev.ScannerType.GENTL);
devScannerGentl.WarningEvent = WarningEvent;
devScannerCC = new ScannerDev(ScannerDev.ScannerType.CC);
devScannerCC.WarningEvent = WarningEvent;
devSmallAxis = new SmallAxisDev();
devSmallAxis.WarningEvent = WarningEvent;
devTension = new TensionDev();
devTension.WarningEvent = WarningEvent;
libDefect = new DefectLib();
libDefect.WarningEvent = WarningEvent;
libSize = new SizeLib();
libSize.WarningEvent = WarningEvent;
libFor = new ForLib();
libIF = new IFLib();
//启动
if (!devAxis.start(Config.Axis_PulseOutMode)) throw new Exception("运动板卡初始化失败!");
//if (!devCodeScanner.start()) throw new Exception("扫码枪初始化失败!");
if (!Config.SkipHeight && !devHeight.start(Config.HeightDev_IP, Config.HeightDev_Port)) throw new Exception("厚度测量设备初始化失败!");
if (!devIOCard.start(Config.IOCard_DeviceNum)) throw new Exception("I/O板卡初始化失败");
if (!Config.SkipLight && !devLight.start(Config.Light_PortNum)) throw new Exception("光源设备初始化失败!");
if (!Config.SkipScannerGL && !devScannerGentl.open()) throw new Exception("相机初始化失败!");
if (!Config.SkipScannerGL && !devScannerGentl.start(this.preView1, null)) throw new Exception("相机打开失败!");
if (!Config.SkipScannerCC && !devScannerCC.open()) throw new Exception("相机初始化失败!");
if (!Config.SkipScannerCC && !devScannerCC.start(this.preView2, Config.appBasePath+"\\temp")) throw new Exception("相机打开失败!");
if (!Config.SkipSmallAxis && !devSmallAxis.start(Config.SmallAxis_ComName, 9600)) throw new Exception("镜头电机初始化失败!");
if (!Config.SkipTension && !devTension.start(Config.Tension_PortNum)) throw new Exception("张力检测设备初始化失败!");
if (!libDefect.start()) throw new Exception("缺陷库初始化失败!");
if (!libSize.start(Config.SizeEnginePath)) throw new Exception("尺寸库初始化失败!");
//
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;
}
}
}