using Community; using DAL; using MahApps.Metro.Controls.Dialogs; using Model; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ViewModel { public class StandVM : NotificationObject { /// /// 上报异常的委托 /// /// public delegate void delegeateExcep(string str); /// /// 上报异常的委托事件 /// public event delegeateExcep eventExcepReport; /// /// 数据引擎 /// MainWindowDAL dal = new MainWindowDAL(); #region 站别参数 private StandModel myStand; /// /// 站别参数 /// public StandModel MyStand { get { return myStand; } set { myStand = value; RaisePropertyChanged("MyStand"); } } #endregion #region PLC连接对象 private SerialComModel myPLC; /// /// PLC连接对象 /// public SerialComModel MyPLC { get { return myPLC; } set { myPLC = value; RaisePropertyChanged("MyPLC"); } } #endregion #region 取料点 /// /// 取料点 /// public RobotPoint TakePoint { get; set; } #endregion #region 放料点 /// /// 放料点 /// public RobotPoint PutPoint { get; set; } #endregion #region 指令集合 private List cmdList; /// /// 指令集合 /// public List CmdList { get { return cmdList; } set { cmdList = value; RaisePropertyChanged("CmdList"); } } #endregion #region 选中的测试指令 private CommonClass selectCmd; /// /// 选中的测试指令 /// public CommonClass SelectCmd { get { return selectCmd; } set { selectCmd = value; RaisePropertyChanged("SelectCmd"); } } #endregion #region 各种方法 /// /// 手动发送测试指令 /// public DelegateCommand ShouDongSendCmdCommand { get; set; } /// /// 生产数据清零 /// public DelegateCommand CountClearedCommand { get; set; } #endregion /// /// 当前机种对象 /// public MachineModel CurrMachine { get; set; } private bool isEnable; /// /// 编辑权限 /// public bool IsEnable { get { return isEnable; } set { isEnable = value; RaisePropertyChanged("IsEnable"); } } #region 构造函数 /// /// 构造函数 /// public StandVM(StandModel sm) { this.MyStand = sm; CmdList = new List() { new CommonClass() { CommonName="状态询问",CmdStr="A0 01 00 E0" }, new CommonClass() { CommonName="发送SN",CmdStr="SendSN"}, new CommonClass() { CommonName="PCB放置完成",CmdStr="A0 03 00 E0"}, new CommonClass() { CommonName="开始测试",CmdStr="A0 04 00 E0"}, }; ShouDongSendCmdCommand = new DelegateCommand() { ExecuteAction = new Action(ShouDongSendCmd) }; CountClearedCommand = new DelegateCommand() { ExecuteAction = new Action(CountCleared) }; } #endregion #region 生产数据清零 /// /// 生产数据清零 /// /// private void CountCleared(object obj) { if (MyStand != null) { MyStand.BadQuantity = 0; MyStand.Capacity = 0; MyStand.CTime = 0; MyStand.InputQuantity = 0; MyStand.OutputQuantity = 0; MyStand.RunTime = ""; MyStand.Yield = 1; MyStand.NgCount = 0; } } #endregion #region 手动发送测试指令 /// /// 手动发送测试指令 /// /// private void ShouDongSendCmd(object obj) { if (SelectCmd != null) { MyPLC.HexSend = true; if (SelectCmd.CmdStr == "SendSN") { if (!string.IsNullOrEmpty(SnCode)) { string str = string.Format("A0 02 {0}E0", StringToHex16String(SnCode.Trim())); MyPLC.Send(str); } else { MyMessageBox.Show("SN错误"); } } else { MyPLC.Send(SelectCmd.CmdStr); } } } #endregion #region 测试盒开关 private OutputSignal boxSwitche; /// /// 测试盒开关 /// public OutputSignal BoxSwitche { get { return boxSwitche; } set { boxSwitche = value; RaisePropertyChanged("BoxSwitche"); } } #endregion /// /// 测试盒子打开信号 /// public InputSignal BoxOpen { get; set; } /// /// 测试盒关闭信号 /// public InputSignal BoxClose { get; set; } #region 开关箱子状态 private ShowInfo boxOpenState; /// /// 开关箱子状态 /// public ShowInfo BoxOpenState { get { return boxOpenState; } set { boxOpenState = value; RaisePropertyChanged("BoxOpenState"); } } #endregion /// /// MES连接对象 /// public ERP_ParameterClass MyMes { get; set; } private bool runSwitch; /// /// 运行开关 /// public bool RunSwitch { get { return runSwitch; } set { runSwitch = value; } } /// /// 运行 /// public void Run() { if (RunSwitch) { TestRun(TestFlow); } } /// /// 测试流程 /// public FlowModel TestFlow { get; set; } #region 当前测试产品的SN private string snCode; /// /// 当前测试产品的SN /// public string SnCode { get { return snCode; } set { snCode = value; RaisePropertyChanged("SnCode"); } } #endregion /// /// 测试运行 /// private void TestRun(FlowModel MyFlow) { switch (MyFlow.RunStep) { case GengralProcessEnum.Start: MyPLC.HexSend = true; MyPLC.Send("A0 01 00 E0");//询问测试机是否准备就绪 MyFlow.RunStep++; MyFlow.RunTime = DateTime.Now; MyStand.TestRes = new ShowInfo() { StatusBackground = ColorStr.Gray, StatuInfo = "unknown" }; break; case GengralProcessEnum.Step1: if (!string.IsNullOrEmpty(MyPLC.ReceiveStr)) { //准备就绪 if (MyPLC.ReceiveStr.Contains("A00101E0")) { MyStand.StandState = new ShowInfo() { StatuInfo = "测试机准备就绪", StatusBackground = ColorStr.Green }; MyFlow.RunStep++; MyFlow.RunTime = DateTime.Now; } else { MyStand.StandState = new ShowInfo() { StatuInfo = "测试机未就绪", StatusBackground = ColorStr.Red }; MyFlow.RunStep--; } } else if (MyFlow.RunTime.AddMilliseconds(5000) < DateTime.Now) { MyStand.StandState = new ShowInfo() { StatuInfo = "测试机回复超时", StatusBackground = ColorStr.Red }; MyStand.TestRes = new ShowInfo() { StatuInfo = "NG", StatusBackground = ColorStr.Red }; RunSwitch = false; eventExcepReport("测试机回复超时"); } break; case GengralProcessEnum.Step2: //发送SN码 MyPLC.HexSend = true; if (string.IsNullOrEmpty(MyStand.Product.StatuInfo)) { MyStand.Product.StatuInfo = "CN0P8HPMDFZ0046H00CQX01"; } string str = string.Format("A0 02 {0}E0", StringToHex16String(MyStand.Product.StatuInfo.Trim())); MyPLC.Send(str); MyFlow.RunStep++; MyFlow.RunTime = DateTime.Now; break; case GengralProcessEnum.Step3: if (!string.IsNullOrEmpty(MyPLC.ReceiveStr)) { //SN码OK if (MyPLC.ReceiveStr.Contains("A00201E0")) { MyFlow.RunStep++; MyFlow.RunTime = DateTime.Now; MyStand.StandState = new ShowInfo() { StatuInfo = "SN码正确", StatusBackground = ColorStr.Blue }; } else if (MyPLC.ReceiveStr.Contains("A00202E0")) { MyStand.StandState = new ShowInfo() { StatuInfo = "SN码错误", StatusBackground = ColorStr.Red }; MyStand.TestRes = new ShowInfo() { StatuInfo = "NG", StatusBackground = ColorStr.Red }; RunSwitch = false; eventExcepReport("测试机回复超时"); } } else if (MyFlow.RunTime.AddMilliseconds(5000) < DateTime.Now) { MyStand.StandState = new ShowInfo() { StatuInfo = "测试机回复超时", StatusBackground = ColorStr.Red }; MyStand.TestRes = new ShowInfo() { StatuInfo = "NG", StatusBackground = ColorStr.Red }; RunSwitch = false; eventExcepReport("测试机回复超时"); } break; case GengralProcessEnum.Step4: //发送产品已放置 MyPLC.HexSend = true; MyPLC.Send("A0 03 00 E0"); MyFlow.RunStep++; MyFlow.RunTime = DateTime.Now; break; case GengralProcessEnum.Step5: if (!string.IsNullOrEmpty(MyPLC.ReceiveStr)) { //测试机回应收到 if (MyPLC.ReceiveStr.Contains("A00301E0")) { MyFlow.RunStep++; MyFlow.RunTime = DateTime.Now; } } else if (MyFlow.RunTime.AddMilliseconds(5000) < DateTime.Now) { RunSwitch = false; MyStand.StandState = new ShowInfo() { StatuInfo = "测试机回复超时", StatusBackground = ColorStr.Red }; MyStand.TestRes = new ShowInfo() { StatuInfo = "NG", StatusBackground = ColorStr.Red }; eventExcepReport("测试机回复超时"); } break; case GengralProcessEnum.Step6: //开启测试 MyPLC.HexSend = true; MyPLC.Send("A0 04 00 E0"); MyFlow.RunStep++; MyFlow.RunTime = DateTime.Now; break; case GengralProcessEnum.Step7: if (!string.IsNullOrEmpty(MyPLC.ReceiveStr)) { //测试机回应收到 if (MyPLC.ReceiveStr.Contains("A00401E0")) { RunSwitch = false; MyFlow.RunStep++; MyFlow.RunTime = DateTime.Now; MyStand.StandState = new ShowInfo() { StatuInfo = "测试中", StatusBackground = ColorStr.Blue }; } } else if (MyFlow.RunTime.AddMilliseconds(5000) < DateTime.Now) { MyStand.StandState = new ShowInfo() { StatuInfo = "测试机回复超时", StatusBackground = ColorStr.Red }; MyStand.TestRes = new ShowInfo() { StatuInfo = "NG", StatusBackground = ColorStr.Red }; RunSwitch = false; eventExcepReport("测试机回复超时"); } break; } } #region 测试电脑回调函数 #region 测试站发来的结果 private string receiveRes; /// /// 测试站发来的结果 /// public string ReceiveRes { get { return receiveRes; } set { receiveRes = value; if (!string.IsNullOrEmpty(value)) { switch (value) { case "A00500E0": //测试OK MyStand.TestRes = new ShowInfo() { StatuInfo = "Pass", StatusBackground = ColorStr.Green }; MyStand.StandState = new ShowInfo() { StatuInfo = "测试OK待投入【" + value + "】", StatusBackground = ColorStr.Green }; MyStand.NgCount = 0; break; case "A00501E0": //测试NG MyStand.TestRes = new ShowInfo() { StatuInfo = "Fail", StatusBackground = ColorStr.Red }; MyStand.NgCount++; //if (MyStand.NgCount >= CurrMachine.NgCount) //{ // MyStand.StandState = new ShowInfo() { StatuInfo = string.Format("连续NG超{0}次", CurrMachine.NgCount), StatusBackground = ColorStr.Yellow }; //} //else //{ // MyStand.StandState = new ShowInfo() { StatuInfo = "测试NG待投入【" + value + "】", StatusBackground = ColorStr.Green }; //} break; case "A00101E0": MyStand.StandState = new ShowInfo() { StatuInfo = "准备就绪", StatusBackground = ColorStr.Green }; break; case "A00102E0": MyStand.StandState = new ShowInfo() { StatuInfo = "准备未就绪", StatusBackground = ColorStr.Red }; break; } } } } #endregion /// /// 测试电脑回调函数 /// /// public void MyPLC_ReceiveCallback(string str) { if (!string.IsNullOrEmpty(str)) { ReceiveRes = str; } } #endregion #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:\\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(); } } /// /// 记录测试结果 /// /// 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 记录上传MES日志信息到文件 /// /// 记录上传MES日志信息到文件 /// /// 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 #region 字符串转HEX /// /// 字符串转HEX /// /// /// public string StringToHex16String(string _str) { //将字符串转换成字节数组。 byte[] buffer = System.Text.Encoding.UTF8.GetBytes(_str); //定义一个string类型的变量,用于存储转换后的值。 string result = string.Empty; for (int i = 0; i < buffer.Length; i++) { //将每一个字节数组转换成16进制的字符串,以空格相隔开。 result += Convert.ToString(buffer[i], 16) + " "; } return result; } #endregion } }