660 lines
22 KiB
C#
660 lines
22 KiB
C#
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
|
||
{
|
||
/// <summary>
|
||
/// 上报异常的委托
|
||
/// </summary>
|
||
/// <param name="hv"></param>
|
||
public delegate void delegeateExcep(string str);
|
||
/// <summary>
|
||
/// 上报异常的委托事件
|
||
/// </summary>
|
||
public event delegeateExcep eventExcepReport;
|
||
|
||
/// <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 SerialComModel myPLC;
|
||
/// <summary>
|
||
/// PLC连接对象
|
||
/// </summary>
|
||
public SerialComModel MyPLC
|
||
{
|
||
get { return myPLC; }
|
||
set
|
||
{
|
||
myPLC = value;
|
||
|
||
|
||
|
||
RaisePropertyChanged("MyPLC");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 取料点
|
||
/// <summary>
|
||
/// 取料点
|
||
/// </summary>
|
||
public RobotPoint TakePoint { get; set; }
|
||
#endregion
|
||
|
||
#region 放料点
|
||
/// <summary>
|
||
/// 放料点
|
||
/// </summary>
|
||
public RobotPoint PutPoint { get; set; }
|
||
#endregion
|
||
|
||
#region 指令集合
|
||
private List<CommonClass> cmdList;
|
||
/// <summary>
|
||
/// 指令集合
|
||
/// </summary>
|
||
public List<CommonClass> CmdList
|
||
{
|
||
get { return cmdList; }
|
||
set { cmdList = value; RaisePropertyChanged("CmdList"); }
|
||
}
|
||
#endregion
|
||
|
||
#region 选中的测试指令
|
||
private CommonClass selectCmd;
|
||
/// <summary>
|
||
/// 选中的测试指令
|
||
/// </summary>
|
||
public CommonClass SelectCmd
|
||
{
|
||
get { return selectCmd; }
|
||
set { selectCmd = value; RaisePropertyChanged("SelectCmd"); }
|
||
}
|
||
#endregion
|
||
|
||
#region 各种方法
|
||
/// <summary>
|
||
/// 手动发送测试指令
|
||
/// </summary>
|
||
public DelegateCommand ShouDongSendCmdCommand { get; set; }
|
||
|
||
/// <summary>
|
||
/// 生产数据清零
|
||
/// </summary>
|
||
public DelegateCommand CountClearedCommand { get; set; }
|
||
|
||
|
||
#endregion
|
||
|
||
/// <summary>
|
||
/// 当前机种对象
|
||
/// </summary>
|
||
public MachineModel CurrMachine { get; set; }
|
||
|
||
|
||
private bool isEnable;
|
||
/// <summary>
|
||
/// 编辑权限
|
||
/// </summary>
|
||
public bool IsEnable
|
||
{
|
||
get { return isEnable; }
|
||
set { isEnable = value; RaisePropertyChanged("IsEnable"); }
|
||
}
|
||
|
||
#region 构造函数
|
||
/// <summary>
|
||
/// 构造函数
|
||
/// </summary>
|
||
public StandVM(StandModel sm)
|
||
{
|
||
this.MyStand = sm;
|
||
|
||
|
||
CmdList = new List<CommonClass>() { 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<object>(ShouDongSendCmd) };
|
||
CountClearedCommand = new DelegateCommand() { ExecuteAction = new Action<object>(CountCleared) };
|
||
|
||
}
|
||
|
||
|
||
#endregion
|
||
|
||
#region 生产数据清零
|
||
/// <summary>
|
||
/// 生产数据清零
|
||
/// </summary>
|
||
/// <param name="obj"></param>
|
||
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 手动发送测试指令
|
||
/// <summary>
|
||
/// 手动发送测试指令
|
||
/// </summary>
|
||
/// <param name="obj"></param>
|
||
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;
|
||
/// <summary>
|
||
/// 测试盒开关
|
||
/// </summary>
|
||
public OutputSignal BoxSwitche
|
||
{
|
||
get { return boxSwitche; }
|
||
set
|
||
{
|
||
boxSwitche = value;
|
||
RaisePropertyChanged("BoxSwitche");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
|
||
/// <summary>
|
||
/// 测试盒子打开信号
|
||
/// </summary>
|
||
public InputSignal BoxOpen { get; set; }
|
||
/// <summary>
|
||
/// 测试盒关闭信号
|
||
/// </summary>
|
||
public InputSignal BoxClose { get; set; }
|
||
|
||
#region 开关箱子状态
|
||
private ShowInfo boxOpenState;
|
||
/// <summary>
|
||
/// 开关箱子状态
|
||
/// </summary>
|
||
public ShowInfo BoxOpenState
|
||
{
|
||
get { return boxOpenState; }
|
||
set
|
||
{
|
||
boxOpenState = value;
|
||
RaisePropertyChanged("BoxOpenState");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
|
||
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// MES连接对象
|
||
/// </summary>
|
||
public ERP_ParameterClass MyMes { get; set; }
|
||
|
||
|
||
private bool runSwitch;
|
||
/// <summary>
|
||
/// 运行开关
|
||
/// </summary>
|
||
public bool RunSwitch
|
||
{
|
||
get { return runSwitch; }
|
||
set { runSwitch = value; }
|
||
}
|
||
|
||
/// <summary>
|
||
/// 运行
|
||
/// </summary>
|
||
public void Run()
|
||
{
|
||
if (RunSwitch)
|
||
{
|
||
TestRun(TestFlow);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 测试流程
|
||
/// </summary>
|
||
public FlowModel TestFlow { get; set; }
|
||
|
||
#region 当前测试产品的SN
|
||
private string snCode;
|
||
/// <summary>
|
||
/// 当前测试产品的SN
|
||
/// </summary>
|
||
public string SnCode
|
||
{
|
||
get { return snCode; }
|
||
set { snCode = value; RaisePropertyChanged("SnCode"); }
|
||
}
|
||
#endregion
|
||
|
||
/// <summary>
|
||
/// 测试运行
|
||
/// </summary>
|
||
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;
|
||
/// <summary>
|
||
/// 测试站发来的结果
|
||
/// </summary>
|
||
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
|
||
|
||
/// <summary>
|
||
/// 测试电脑回调函数
|
||
/// </summary>
|
||
/// <param name="str"></param>
|
||
public void MyPLC_ReceiveCallback(string str)
|
||
{
|
||
if (!string.IsNullOrEmpty(str))
|
||
{
|
||
ReceiveRes = str;
|
||
}
|
||
}
|
||
#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 记录上传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
|
||
|
||
#region 字符串转HEX
|
||
/// <summary>
|
||
/// 字符串转HEX
|
||
/// </summary>
|
||
/// <param name="_str"></param>
|
||
/// <returns></returns>
|
||
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
|
||
|
||
}
|
||
|
||
|
||
}
|