340 lines
14 KiB
C#
340 lines
14 KiB
C#
#define JM
|
||
using LeatherApp.Device;
|
||
using LeatherApp.Interface;
|
||
using Newtonsoft.Json.Linq;
|
||
using S7.Net;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.IO.Ports;
|
||
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;
|
||
|
||
private SerialPort lengthCounter;
|
||
|
||
private DL_EN1Dev dL_EN1Dev;
|
||
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 { }
|
||
#if JM
|
||
try { lengthCounter.Close(); } catch { }
|
||
#endif
|
||
try { devCamer1.stop(); devCamer1.close(); } catch { }
|
||
try { devCamer2.stop(); devCamer2.close(); } catch { }
|
||
try { devCodeScanner.stop(); } catch { }
|
||
|
||
try { libDefect.stop(); } catch { }
|
||
try { libPhoto.stop(); } catch { }
|
||
if (Config.OpenHouDuJiLu)
|
||
{
|
||
try { dL_EN1Dev.stopDev(); } 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 JM
|
||
lengthCounter = new SerialPort(Config.JM_PortName, 9600);
|
||
#endif
|
||
//开启测厚
|
||
if (Config.OpenHouDuJiLu)
|
||
{
|
||
dL_EN1Dev = new DL_EN1Dev();
|
||
if(!dL_EN1Dev.startDev(Config.CeHouIP, Config.CeHouPort))
|
||
throw new Exception("位移传感器初始化失败!");
|
||
double d1, d2, d3;
|
||
if(!GetThicknessValue(out d1, out d2, out d3))
|
||
{
|
||
throw new Exception($"位移传感器数据读取失败{d1}-{d2}-{d3}!");
|
||
}
|
||
else
|
||
WarningEvent?.Invoke(DateTime.Now, WarningEnum.Normal, $"DL-{d1}-{d2}-{d3}");
|
||
}
|
||
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 JM
|
||
try
|
||
{
|
||
lengthCounter.Open();
|
||
if (!lengthCounter.IsOpen)
|
||
throw new Exception("计米设备打开失败!");
|
||
}
|
||
catch {
|
||
throw new Exception("计米设备初始化失败!");
|
||
}
|
||
#endif
|
||
if (!libPhoto.start()) throw new Exception("图像库初始化失败!");
|
||
WarningEvent?.Invoke(DateTime.Now,WarningEnum.Normal, "2");
|
||
if (libDefect == null)
|
||
{
|
||
WarningEvent?.Invoke(DateTime.Now, WarningEnum.Normal, "算法库为空,重新初始化");
|
||
libDefect = new DefectLib();
|
||
libDefect.WarningEvent = WarningEvent;
|
||
}
|
||
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("相机2初始化失败!");
|
||
if (!devCamer2.start(this.preView1, scanner1Path + "scanner\\")) throw new Exception("相机1打开失败!");
|
||
if (!devCamer1.open(0, 0)) throw new Exception("相机1初始化失败!");
|
||
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);
|
||
}
|
||
}
|
||
|
||
#region 计米器控制
|
||
/// <summary>
|
||
/// 计米器清空
|
||
/// </summary>
|
||
public void ClearLengthCount()
|
||
{
|
||
//if (confMgr.SysConfigParams.OpenLengthCount)
|
||
{
|
||
if (lengthCounter.IsOpen)
|
||
{
|
||
for (int t = 0; t < 3; t++)
|
||
{
|
||
byte[] clearData = new byte[] { 0x01, 0x06, 0x00, 0x00, 0x00, 0x01, 0x48, 0x0a };
|
||
lengthCounter.Write(clearData, 0, 8);
|
||
//Thread.Sleep(100);
|
||
Thread.Sleep(20);
|
||
byte[] recv = new byte[64];
|
||
//string recvdata = serialPort.ReadLine();
|
||
//recv = recvdata.ToBytes();
|
||
int readCnt = lengthCounter.BytesToRead;
|
||
lengthCounter.Read(recv, 0, readCnt);
|
||
Thread.Sleep(100);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
private object JMLock = new object();
|
||
/// <summary>
|
||
/// 获取计米数据
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public double GetLength()
|
||
{
|
||
//lock (JMLock)
|
||
{
|
||
//bool isFile = true;
|
||
double length = -9999;
|
||
bool GetData = false;
|
||
if (lengthCounter.IsOpen)
|
||
{
|
||
//for (int t = 0; t < 5; t++)
|
||
{
|
||
byte[] data = new byte[] { 0x01, 0x03, 0x00, 0x21, 0x00, 0x02, 0x94, 0x01 };
|
||
lengthCounter.Write(data, 0, 8);
|
||
Thread.Sleep(20);
|
||
byte[] recv = new byte[64];
|
||
//string recvdata = serialPort.ReadLine();
|
||
//recv = recvdata.ToBytes();
|
||
int readCnt = lengthCounter.BytesToRead;
|
||
//readCnt = 9;
|
||
lengthCounter.Read(recv, 0, readCnt);
|
||
byte[] bytes = new byte[4];
|
||
for (int i = 0; i < readCnt; i++)
|
||
{
|
||
if (recv[i] == 0x01 && recv[i + 1] == 0x03 && recv[i + 2] == 0x04)
|
||
{
|
||
bytes[0] = recv[i + 3];
|
||
bytes[1] = recv[i + 4];
|
||
bytes[2] = recv[i + 5];
|
||
bytes[3] = recv[i + 6];
|
||
GetData = true;
|
||
}
|
||
}
|
||
if (GetData)
|
||
{
|
||
if (BitConverter.IsLittleEndian)
|
||
Array.Reverse(bytes);
|
||
int spddata = BitConverter.ToInt32(bytes, 0);
|
||
length = spddata / 100.0;
|
||
//break;
|
||
//if (isFile)
|
||
// length = -length;
|
||
}
|
||
Thread.Sleep(20);
|
||
}
|
||
if (length > 10000)
|
||
return -100000;
|
||
}
|
||
else
|
||
{ return -100000; }
|
||
return Math.Abs(length);
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 测厚duqu
|
||
public bool GetThicknessValue(out double d1, out double d2, out double d3)
|
||
{
|
||
if(dL_EN1Dev != null)
|
||
{
|
||
return dL_EN1Dev.GetValue(out d1, out d2, out d3);
|
||
}
|
||
else
|
||
{
|
||
d1 = d2 = d3 = -11;
|
||
return false; }
|
||
}
|
||
#endregion
|
||
|
||
/// <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;
|
||
}
|
||
|
||
}
|
||
}
|