geboshi_V1/LeatherProject/LeatherApp/DevContainer.cs

289 lines
12 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.

#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;
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 { }
}
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.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("相机初始化失败!");
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);
}
}
#region
/// <summary>
/// 计米器清空
/// </summary>
public void ClearLengthCount()
{
//if (confMgr.SysConfigParams.OpenLengthCount)
{
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);
}
}
/// <summary>
/// 获取计米数据
/// </summary>
/// <returns></returns>
public double GetLength()
{
//bool isFile = true;
double length = -99;
bool GetData = false;
if (lengthCounter.IsOpen)
{
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;
//if (isFile)
// length = -length;
}
if (length > 10000)
return -1;
}
else
{ return -100; }
return Math.Abs(length);
}
#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;
}
}
}