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 { /// /// 上报异常的委托 /// /// public delegate void delegeateExcep(string str); /// /// 上报异常的委托事件 /// public event delegeateExcep eventExcepReport; /// /// 视觉定位的委托 /// /// public delegate void Mydelegate(StandModel SGM); /// /// 视觉定位1的委托事件 /// public event Mydelegate visionLocationDelegeat; /// /// 数据引擎 /// MainWindowDAL dal = new MainWindowDAL(); #region 站别参数 private StandModel myStand; /// /// 站别参数 /// public StandModel MyStand { get { return myStand; } set { myStand = value; RaisePropertyChanged("MyStand"); } } #endregion #region PLC连接对象 private FeiYuHsl myPLC; /// /// PLC连接对象 /// public FeiYuHsl MyPLC { get { return myPLC; } set { myPLC = value; if (value != null) { if (value.Brand == 1) { PCtoPLCList = new List() { new PCtoPLC() { NameStr="视觉检查:", CmdAddress="D1000", StreamAddress1="D10090",StreamAddress2=""}, }; } else { PCtoPLCList = new List() { new PCtoPLC() { NameStr="视觉检查:", CmdAddress="D1000", StreamAddress1="D10090",StreamAddress2=""}, }; } } RaisePropertyChanged("MyPLC"); } } #endregion private List pctoPLCList; /// /// 站别与PLC的通讯管道对象集合 /// public List PCtoPLCList { get { return pctoPLCList; } set { pctoPLCList = value; RaisePropertyChanged("PCtoPLCList"); } } /// /// 当前机种对象 /// public MachineModel CurrMachine { get; set; } #region 构造函数 /// /// 构造函数 /// public StandVM(StandModel sm) { this.MyStand = sm; StandState = new ShowInfo(); StandState.StatusBackground = ColorStr.Transparent; VisualShow = new HalconVisual(); } #endregion #region 站别状态 private string dataInfo = "0"; /// /// 实时数据 /// public string DataInfo { get { return dataInfo; } set { dataInfo = value; this.RaisePropertyChanged("DataInfo"); } } private string pcbData = "0"; /// /// PCB数据 /// public string PCBData { get { return pcbData; } set { pcbData = value; this.RaisePropertyChanged("PCBData"); } } private string glassData = "0"; /// /// 玻璃数据 /// public string GlassData { get { return glassData; } set { glassData = value; this.RaisePropertyChanged("GlassData"); } } private ShowInfo standState; /// /// 站别状态 /// public ShowInfo StandState { get { return standState; } set { standState = value; RaisePropertyChanged("StandState"); } } #endregion /// /// MES连接对象 /// public ERP_ParameterClass MyMes { get; set; } private HalconVisual visualShow; /// /// 视觉演示 /// public HalconVisual VisualShow { get { return visualShow; } set { visualShow = value; RaisePropertyChanged("VisualShow"); } } /// /// 前站连接 /// public CommunicationManagementClass Upstand { get; set; } bool fir { get; set; } DateTime t { get; set; } /// /// 运行 /// 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(); } /// /// TODO:KT_2024-05-02读取PLC生产资讯数据 /// 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; } /// /// 文件夹路径 /// string path { get; set; } /// /// 文件路径 /// string paths { get; set; } /// /// 记录信息对象 /// RecordInfo info { get; set; } /// /// 打开的文件流对象 /// FileStream FS { get; set; } /// /// 记录日志信息到文件 /// /// /// /// 记录信息到文件 /// /// 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(); } } /// /// 记录螺柱定位孔距离结果日志 /// /// private void DisHoleAndNutResultLog(List 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 } }