liteon/ViewModel/StandVM.cs
2025-07-12 14:05:35 +08:00

650 lines
21 KiB
C#
Raw Permalink 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 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Community;
using System.IO;
using Model;
using System.Windows;
using Model.HSL;
using ViewModel.Vision;
using Model.Vision;
using System.Windows.Media;
using DAL;
using ViewModel.Robots;
using HslCommunication;
namespace ViewModel
{
public class StandVM : NotificationObject
{
/// <summary>
/// 上报异常的委托
/// </summary>
/// <param name="hv"></param>
public delegate void delegeateExcep(string str);
/// <summary>
/// 上报异常的委托事件
/// </summary>
public event delegeateExcep eventExcepReport;
/// <summary>
/// 视觉定位的委托
/// </summary>
/// <param name="hv"></param>
public delegate void Mydelegate(StandModel SGM);
/// <summary>
/// 视觉定位1的委托事件
/// </summary>
public event Mydelegate visionLocationDelegeat;
/// <summary>
/// 数据引擎
/// </summary>
MainWindowDAL dal = new MainWindowDAL();
#region 站别参数
private StandModel myStand;
/// <summary>
/// 站别参数
/// </summary>
public StandModel MyStand
{
get { return myStand; }
set
{
myStand = value;
RaisePropertyChanged("MyStand");
}
}
#endregion
#region PLC连接对象
private FeiYuHsl myPLC;
/// <summary>
/// PLC连接对象
/// </summary>
public FeiYuHsl MyPLC
{
get { return myPLC; }
set
{
myPLC = value;
if (value != null)
{
if (value.Brand == 1)
{
PCtoPLCList = new List<PCtoPLC>()
{
new PCtoPLC() { NameStr="视觉检查:", CmdAddress="D1000", StreamAddress1="D10090",StreamAddress2=""},
};
}
else
{
PCtoPLCList = new List<PCtoPLC>()
{
new PCtoPLC() { NameStr="视觉检查:", CmdAddress="D1000", StreamAddress1="D10090",StreamAddress2=""},
};
}
}
RaisePropertyChanged("MyPLC");
}
}
#endregion
private List<PCtoPLC> pctoPLCList;
/// <summary>
/// 站别与PLC的通讯管道对象集合
/// </summary>
public List<PCtoPLC> PCtoPLCList
{
get { return pctoPLCList; }
set
{
pctoPLCList = value;
RaisePropertyChanged("PCtoPLCList");
}
}
/// <summary>
/// 当前机种对象
/// </summary>
public MachineModel CurrMachine { get; set; }
#region 构造函数
/// <summary>
/// 构造函数
/// </summary>
public StandVM(StandModel sm)
{
this.MyStand = sm;
StandState = new ShowInfo();
StandState.StatusBackground = ColorStr.Transparent;
}
#endregion
#region 记录日志信息
byte[] data { get; set; }
/// <summary>
/// 文件夹路径
/// </summary>
string path { get; set; }
/// <summary>
/// 文件路径
/// </summary>
string paths { get; set; }
/// <summary>
/// 记录信息对象
/// </summary>
RecordInfo info { get; set; }
/// <summary>
/// 打开的文件流对象
/// </summary>
FileStream FS { get; set; }
/// <summary>
/// 记录日志信息到文件
/// </summary>
/// <param name="info"></param>
private void RecordLog(string showMag)
{
lock (this)
{
path = @"D:\\LAGLog\ATSLog"; //文件夹路径
paths = @"D:\\LAGLog\ATSLog\" + DateTime.Now.ToString("yyyyMMdd") + ".txt"; //文件路径 以年月日为名称
//创建文件夹
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
//打开或创建文件
FS = File.Open(paths, FileMode.Append);
//获得字节数组
data = System.Text.Encoding.Default.GetBytes(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff") + showMag + "\r\n");
//开始写入
FS.Write(data, 0, data.Length);
//清空缓冲区、关闭流
FS.Flush();
FS.Close();
}
}
/// <summary>
/// 记录测试结果
/// </summary>
/// <param name="CM"></param>
private void TestResultLog(CylinderMeasurementModel CM)
{
lock (this)
{
path = @"D:\\TestResultLog\";
paths = @"D:\\TestResultLog\" + DateTime.Now.ToString("D") + ".CSV";
//实例化字符串流
StringBuilder sb = new StringBuilder();
//创建文件夹
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
//创建文件
if (!File.Exists(paths))
{
sb.Append("测量项目").Append(",").Append("最大外径").Append(",").Append("最小外径").Append(",").Append("平均外径").Append(",").Append("跳动率").Append(",").Append("结果(NG/OK)").Append(",").Append("检查时间").Append(",\r\n");
}
using (FileStream fs = new FileStream(paths, FileMode.Append))
{
using (StreamWriter sw = new StreamWriter(fs, UnicodeEncoding.GetEncoding("GB2312"))) //UnicodeEncoding.GetEncoding("GB2312")
{
sb.Append(CM.CmName).Append(",").Append(CM.MaxDiam).Append(",").Append(CM.MinDiam).Append(",").Append(CM.AverageDiam).Append(",").Append(CM.BeatRate).Append(",").Append(CM.Result).Append(",").Append(DateTime.Now.ToLongTimeString().ToString());
//将字符串流数据写入文件
sw.WriteLine(sb);
//刷新文件流
sw.Flush();
}
}
}
}
#endregion
#region 站别状态
private ShowInfo standState;
/// <summary>
/// 站别状态
/// </summary>
public ShowInfo StandState
{
get { return standState; }
set
{
standState = value;
RaisePropertyChanged("StandState");
}
}
#endregion
/// <summary>
/// MES连接对象
/// </summary>
public ERP_ParameterClass MyMes { get; set; }
/// <summary>
/// 机械手对象
/// </summary>
public FeiYuRobotVM MyRobot { get; set; }
//TODO:kt_2024-05-02,修改变量VisualA/B 由public static 变更为public
/// <summary>
/// 视觉A
/// </summary>
public HalconVisual VisualA { get; set; }
/// <summary>
/// 视觉B
/// </summary>
public HalconVisual VisualB { get; set; }
/// <summary>
/// 视觉C
/// </summary>
public HalconVisual VisualC { get; set; }
public HalconVisual VisualD { get; set; }
public HalconVisual VisualE { get; set; }
public HalconVisual VisualF { get; set; }
bool fir { get; set; }
/// <summary>
/// 运行
/// </summary>
public void Run()
{
if (!fir)
{
MyPLC.Conneced();
fir = true;
}
if (MyPLC.PLC.PlcStatus.StatusBackground != ColorStr.Green)
{
return;
}
if (MyPLC.PLC.ConnectionType == 3)
{
ReadHeatData();
}
else
{
ReadMachineData();
}
// VisualInspection(PCtoPLCList[0]);
}
DateTime heatTime;
/// <summary>
/// 读取焊机温度
/// </summary>
private void ReadHeatData()
{
HeatValue = MyPLC.PLC.ReadShort("32");
Heat = HeatValue > 0 ? new ShowInfo() { StatuValue = HeatValue + "℃" } : new ShowInfo() { StatuValue = "0℃" };
//if (heatTime.AddMilliseconds(200) <= DateTime.Now)
//{
// HeatValue = MyPLC.PLC.ReadShort("32");
// Heat = HeatValue > 0 ? new ShowInfo() { StatuValue = HeatValue + "℃" } : new ShowInfo() { StatuValue = "0℃" };
// heatTime = DateTime.Now;
//}
}
public double HeatValue { get; set; }
/// <summary>
/// 焊接左
/// </summary>
public bool WeldLeft { get; set; }
/// <summary>
/// 焊接左
/// </summary>
public bool WeldRight { get; set; }
private ShowInfo heat;
/// <summary>
/// 温度
/// </summary>
public ShowInfo Heat
{
get { return heat; }
set { heat = value; RaisePropertyChanged("Heat"); }
}
private ShowInfo pressureL;
/// <summary>
/// 左压力
/// </summary>
public ShowInfo PressureL
{
get { return pressureL; }
set { pressureL = value; RaisePropertyChanged("PressureL"); }
}
private ShowInfo pressureR;
/// <summary>
/// 右压力
/// </summary>
public ShowInfo PressureR
{
get { return pressureR; }
set { pressureR = value; RaisePropertyChanged("PressureR"); }
}
/// <summary>
/// TODO:KT_2024-05-02读取PLC生产资讯数据
/// </summary>
private void ReadMachineData()
{
int L = MyPLC.PLC.ReadInt32("D200");
int R = MyPLC.PLC.ReadInt32("D250");
PressureL = L > 0 ? new ShowInfo() { StatuValue = Math.Round((double)L / 10 * 0.102, 3) + "kgf" } : new ShowInfo() { StatuValue = "0kgf" };
PressureR = R > 0 ? new ShowInfo() { StatuValue = Math.Round((double)R / 10 * 0.102, 3) + "kgf" } : new ShowInfo() { StatuValue = "0kgf" };
int HL = MyPLC.PLC.ReadShort("D10");
int HR = MyPLC.PLC.ReadShort("D11");
//读取PLC焊接信号
WeldLeft = HL == 85 ? true : false;
WeldRight = HR == 85 ? true : false;
//return;
//心跳 持续 写入1
MyPLC.PLC.Write("D6500", 1);
//批量读取机台数据
int aa = MyPLC.PLC.ReadShort("D80");
if (aa > 0)
{
try
{
//站别运行状态
switch (aa)
{
case 1:
MyStand.StandState = new ShowInfo() { StatuInfo = "待机中...", StatusBackground = ColorStr.Yellow };
break;
case 2:
MyStand.StandState = new ShowInfo() { StatuInfo = "运行中...", StatusBackground = ColorStr.Green };
break;
case 3:
MyStand.StandState = new ShowInfo() { StatuInfo = "异常中...", StatusBackground = ColorStr.Red };
break;
case 4:
MyStand.StandState = new ShowInfo() { StatuInfo = "待料中...", StatusBackground = ColorStr.Blue };
break;
default:
MyStand.StandState = new ShowInfo() { StatuInfo = "未知...", StatusBackground = ColorStr.Red };
break;
}
//int L = MyPLC.PLC.ReadInt32("D200");
//int R = MyPLC.PLC.ReadInt32("D250");
//HeatL = L != 0 ? new ShowInfo() { StatuValue = Math.Round((double)L / 1000, 3) } : new ShowInfo();
//HeatR = R != 0 ? new ShowInfo() { StatuValue = Math.Round((double)R / 1000, 3) } : new ShowInfo();
}
catch (Exception)
{
MyStand.StandState = new ShowInfo() { StatusBackground = ColorStr.Red, StatuInfo = "机台数据有误", ID = 10 };
}
}
else
{
MyStand.StandState = new ShowInfo() { StatusBackground = ColorStr.Red, StatuInfo = "读取机台数据失败", ID = 10 };
}
}
/// <summary>
/// 所有成功进入机台的载具集合
/// </summary>
public List<VehicleModel> VehicleList { get; set; }
#region 三、视觉检查
Task<bool> task1;
Task<bool> task2;
Task<bool> task3;
Task<bool> task4;
Task<bool> task5;
Task<bool> task6;
/// <summary>
/// 视觉检查结果
/// </summary>
List<bool> VisionCheckRes { get; set; }
int CheckCount { get; set; }
/// <summary>
/// 三、视觉检查
/// </summary>
private void VisualInspection(PCtoPLC Sp)
{
Sp.Cmd = MyPLC.PLC.ReadInt32(Sp.CmdAddress);
if (Sp.Cmd == 10)
{
Sp.RunStep = GengralProcessEnum.Step1;
CheckCount = 0;
}
switch (Sp.RunStep)
{
case GengralProcessEnum.Step1:
//把装配指令改了 避免重复触发
if (MyPLC.PLC.Write(Sp.CmdAddress, 1000))
{
if (!CurrMachine.RunningMode)
{
Sp.RunStep++;
Sp.TimeTemp = DateTime.Now;
}
else
{
Sp.ToPLC = 20;
Sp.RunStep = GengralProcessEnum.End;
MyPLC.PLC.Write(Sp.CmdAddress, Sp.ToPLC);
Sp.RunState = new ShowInfo() { StatuInfo = string.Format("{0}=>【{1}】", "视觉检查完成【空跑模式】", Sp.ToPLC), StatusBackground = ColorStr.Green };
}
}
break;
case GengralProcessEnum.Step2:
CheckCount++;
VisionCheckRes = new List<bool>();
task1 = Task.Run(() => Vision1());
task2 = Task.Run(() => Vision2());
task3 = Task.Run(() => Vision3());
task4 = Task.Run(() => Vision4());
task5 = Task.Run(() => Vision5());
task6 = Task.Run(() => Vision6());
Task.WaitAll(task1, task2, task3, task4, task5, task6);//等待视觉检查完成
VisionCheckRes.Add(task1.Result);
VisionCheckRes.Add(task2.Result);
VisionCheckRes.Add(task3.Result);
VisionCheckRes.Add(task4.Result);
VisionCheckRes.Add(task5.Result);
VisionCheckRes.Add(task6.Result);
Sp.RunStep++;
Sp.TimeTemp = DateTime.Now;
break;
case GengralProcessEnum.Step3:
if (VisionCheckRes.Count >= 6 && VisionCheckRes.Where(w => w == false).Count() <= 0)
{
Sp.ToPLC = 20;
Sp.RunStep = GengralProcessEnum.End;
MyPLC.PLC.Write(Sp.CmdAddress, Sp.ToPLC);
Sp.RunState = new ShowInfo() { StatuInfo = string.Format("{0}=>【{1}】", "视觉检查完成", Sp.ToPLC), StatusBackground = ColorStr.Green };
}
else
{
Sp.RunStep++;
Sp.TimeTemp = DateTime.Now;
}
break;
case GengralProcessEnum.Step4:
if (CheckCount >= 3)
{
Sp.ToPLC = 30;
Sp.RunStep = GengralProcessEnum.End;
MyPLC.PLC.Write(Sp.CmdAddress, Sp.ToPLC);
Sp.RunState = new ShowInfo() { StatuInfo = string.Format("{0}=>【{1}】", "视觉检查失败", Sp.ToPLC), StatusBackground = ColorStr.Red };
}
else if (Sp.TimeTemp.AddMilliseconds(200) < DateTime.Now)
{
Sp.RunStep = GengralProcessEnum.Step2;
Sp.TimeTemp = DateTime.Now;
}
break;
}
}
public void VisonTest()
{
task1 = Task.Run(() => Vision1());
task2 = Task.Run(() => Vision2());
task3 = Task.Run(() => Vision3());
task4 = Task.Run(() => Vision4());
task5 = Task.Run(() => Vision5());
task6 = Task.Run(() => Vision6());
Task.WaitAll(task1, task2, task3, task4, task5, task6);//等待视觉检查完成
}
/// <summary>
/// 视觉检查1
/// </summary>
private bool Vision1()
{
bool bl = false;
V_RectangleModel vm = VisualA.VisualEngineeringList[0].VisualEngineeringItemsList[0].MyRectangleModel;
bl = VisualA.FeatureMatchingIng(vm);
return bl;
}
/// <summary>
/// 视觉检查2
/// </summary>
private bool Vision2()
{
bool bl = false;
V_RectangleModel vm = VisualB.VisualEngineeringList[0].VisualEngineeringItemsList[0].MyRectangleModel;
bl = VisualB.FeatureMatchingIng(vm);
return bl;
}
/// <summary>
/// 视觉检查2
/// </summary>
private bool Vision3()
{
bool bl = false;
V_RectangleModel vm = VisualC.VisualEngineeringList[0].VisualEngineeringItemsList[0].MyRectangleModel;
bl = VisualC.FeatureMatchingIng(vm);
return bl;
}
private bool Vision4()
{
bool bl = false;
V_RectangleModel vm = VisualD.VisualEngineeringList[0].VisualEngineeringItemsList[0].MyRectangleModel;
bl = VisualD.FeatureMatchingIng(vm);
return bl;
}
private bool Vision5()
{
bool bl = false;
V_RectangleModel vm = VisualE.VisualEngineeringList[0].VisualEngineeringItemsList[0].MyRectangleModel;
bl = VisualE.FeatureMatchingIng(vm);
return bl;
}
private bool Vision6()
{
bool bl = false;
V_RectangleModel vm = VisualF.VisualEngineeringList[0].VisualEngineeringItemsList[0].MyRectangleModel;
bl = VisualF.FeatureMatchingIng(vm);
return bl;
}
#endregion
#region 记录上传MES日志信息到文件
/// <summary>
/// 记录上传MES日志信息到文件
/// </summary>
/// <param name="info"></param>
private void RecordMesLog(string showMag)
{
try
{
string path = @"D:\\Log\MesLog\\" + DateTime.Now.ToString("D") + "\\"; //文件夹路径
string filePath = path + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".txt"; //
//创建文件夹
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
//打开或创建文件
FS = File.Open(filePath, FileMode.Append);
//获得字节数组
data = System.Text.Encoding.Default.GetBytes(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff") + showMag + "\r\n");
//开始写入
FS.Write(data, 0, data.Length);
//清空缓冲区、关闭流
FS.Flush();
FS.Close();
}
catch (Exception)
{
}
}
#endregion
}
}