TFLH/ViewModel/StandVM.cs
2026-08-06 09:50:46 +08:00

480 lines
14 KiB
C#

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 HalconDotNet;
using System.Collections.ObjectModel;
using System.Threading;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Web.UI.WebControls.Expressions;
using System.Windows.Media.Media3D;
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;
VisualShow = new HalconVisual();
}
#endregion
#region 站别状态
private string dataInfo = "0";
/// <summary>
/// 实时数据
/// </summary>
public string DataInfo
{
get { return dataInfo; }
set
{
dataInfo = value;
this.RaisePropertyChanged("DataInfo");
}
}
private string pcbData = "0";
/// <summary>
/// PCB数据
/// </summary>
public string PCBData
{
get { return pcbData; }
set
{
pcbData = value;
this.RaisePropertyChanged("PCBData");
}
}
private string glassData = "0";
/// <summary>
/// 玻璃数据
/// </summary>
public string GlassData
{
get { return glassData; }
set
{
glassData = value;
this.RaisePropertyChanged("GlassData");
}
}
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; }
private HalconVisual visualShow;
/// <summary>
/// 视觉演示
/// </summary>
public HalconVisual VisualShow
{
get { return visualShow; }
set { visualShow = value; RaisePropertyChanged("VisualShow"); }
}
/// <summary>
/// 前站连接
/// </summary>
public CommunicationManagementClass Upstand { get; set; }
bool fir { get; set; }
DateTime t { get; set; }
/// <summary>
/// 运行
/// </summary>
public void Run()
{
if (!fir)
{
MyPLC.Conneced();
fir = true;
}
if (MyPLC.PLC.PlcStatus.StatusBackground != ColorStr.Green)
{
return;
}
//if (Upstand.Showmsg.StatusBackground != ColorStr.Green)
//{
// if (t.AddMilliseconds(300) < DateTime.Now)
// {
// if (!Upstand.ConnectSwitch)
// {
// Upstand.ConnectSwitch = true;
// t = DateTime.Now;
// }
// else
// {
// Upstand.ConnectSwitch = false;
// t = DateTime.Now;
// }
// }
//}
//ReadMachineData();
}
/// <summary>
/// TODO:KT_2024-05-02读取PLC生产资讯数据
/// </summary>
private void ReadMachineData()
{
//心跳 持续 写入1
MyPLC.PLC.Write("D6500", 0);
//批量读取机台数据
int aa = MyPLC.PLC.ReadInt32("D1500");
if (aa > 0)
{
try
{
//站别运行状态
switch (aa)
{
case 1:
MyStand.StandState = new ShowInfo() { StatuInfo = "运行中...", StatusBackground = ColorStr.Green };
break;
case 2:
MyStand.StandState = new ShowInfo() { StatuInfo = "待机中...", StatusBackground = ColorStr.Yellow };
break;
case 3:
MyStand.StandState = new ShowInfo() { StatuInfo = "异常中...", StatusBackground = ColorStr.Red };
break;
case 4:
MyStand.StandState = new ShowInfo() { StatuInfo = "复位中...", StatusBackground = ColorStr.Blue };
PCtoPLCList[0].RunStep = GengralProcessEnum.End; //PLC复位重置流程
break;
default:
MyStand.StandState = new ShowInfo() { StatuInfo = "未知...", StatusBackground = ColorStr.Red };
break;
}
}
catch (Exception)
{
MyStand.StandState = new ShowInfo() { StatusBackground = ColorStr.Red, StatuInfo = "机台数据有误", ID = 10 };
}
}
else
{
MyStand.StandState = new ShowInfo() { StatusBackground = ColorStr.Red, StatuInfo = "读取机台数据失败", ID = 10 };
}
}
#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>
/// <summary>
/// 记录信息到文件
/// </summary>
/// <param name="info"></param>
private void RecordLog(string showMag)
{
lock (this)
{
path = @"D:\\RecordLog"; //文件夹路径
paths = @"D:\\RecordLog\" + 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") + showMag + "\r\n");
//开始写入
FS.Write(data, 0, data.Length);
//清空缓冲区、关闭流
FS.Flush();
FS.Close();
}
}
/// <summary>
/// 记录螺柱定位孔距离结果日志
/// </summary>
/// <param name="CM"></param>
private void DisHoleAndNutResultLog(List<Point> list)
{
lock (this)
{
if (list == null && list.Count < 0)
{
return;
}
path = @"D:\\TestResultLog\\" + CurrMachine.MachineName + "\\DisHoleAndNutResultLog";
paths = @"D:\\TestResultLog\\" + CurrMachine.MachineName + "\\DisHoleAndNutResultLog\\" + DateTime.Now.ToString("D") + ".CSV";
//实例化字符串流
StringBuilder sb = new StringBuilder();
//创建文件夹
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
//创建文件
if (!File.Exists(paths))
{
string ss = string.Format("{0},{1},{2},{3},", "时间", "机种", "PCB_SN", "TrayNum");
sb.Append(ss);
//RDAAB
if (CurrMachine.ID == 1)
{
//左铁件
for (int i = 1; i <= 8; i++)
{
sb.AppendFormat("{0},{1},", i + "点螺柱X测量值L", i + "点螺柱Y测量值L");
}
//中间铁件
for (int i = 1; i <= 12; i++)
{
sb.AppendFormat("{0},{1},", i + "点螺柱X测量值M", i + "点螺柱Y测量值M");
}
//右铁件
for (int i = 1; i <= 8; i++)
{
sb.AppendFormat("{0},{1},", i + "点螺柱X测量值R", i + "点螺柱Y测量值R");
}
sb.Append("\r");
}
if (CurrMachine.ID == 5) //RDAAC
{
//左铁件
for (int i = 1; i <= 5; i++)
{
sb.AppendFormat("{0},{1},", i + "点螺柱X测量值L", i + "点螺柱Y测量值L");
}
//中间铁件
for (int i = 1; i <= 14; i++)
{
sb.AppendFormat("{0},{1},", i + "点螺柱X测量值M", i + "点螺柱Y测量值M");
}
//右铁件
for (int i = 1; i <= 5; i++)
{
sb.AppendFormat("{0},{1},", i + "点螺柱X测量值R", i + "点螺柱Y测量值R");
}
sb.Append("\r");
}
//RDAAE
if (CurrMachine.ID == 6)
{
//左铁件
for (int i = 1; i <= 6; i++)
{
sb.AppendFormat("{0},{1},", i + "点螺柱X测量值L", i + "点螺柱Y测量值L");
}
//中间铁件
for (int i = 1; i <= 5; i++)
{
sb.AppendFormat("{0},{1},", i + "点螺柱X测量值M", i + "点螺柱Y测量值M");
}
//右铁件
for (int i = 1; i <= 6; i++)
{
sb.AppendFormat("{0},{1},", i + "点螺柱X测量值R", i + "点螺柱Y测量值R");
}
sb.Append("\r");
}
}
}
}
#endregion
}
}