using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Web.UI.WebControls; using System.Windows.Forms; using System.Xml; using ZJ_BYD.Common; using ZJ_BYD.DB; using ZJ_BYD.Enums; using ZJ_BYD.Model; using ZJ_BYD.Model.HttpApiParam; using ZJ_BYD.Untils; using ZJ_BYD.ViewModel; namespace ZJ_BYD { public class DoSomething : MyEventHandler { private StationInfo _stationInfo; private List _logs; private List _plcPoints; //打印条码 private string BarCode = ""; private bool startprint = true; private bool savedb = true; private bool runMainMatch = true; private bool runSubMatch = true; private bool runRequestMes = true; private bool runStationData = true; private static int tempMachineType = -1;//临时存放机型 #region 公共操作 private (bool ok, string value) ReadPlcValue(string pointCode) { try { bool ok = false; string value = null; var point = _plcPoints.FirstOrDefault(m => m.Key == pointCode); if (point != null) { var plcValue = PlcHelper.GetPlcVal(point.DataType, point.Address); ok = plcValue.ok; if (plcValue.ok) { value = plcValue.val.Replace("\r", ""); } else { LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>读取{pointCode}值为失败"); } } else { LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>{pointCode}地址不存在"); } return (ok, value); } catch (Exception ex) { var errMsg = ex == null ? "未知异常" : ex.Message; MessageBox.Show($"DoSomething中ReadPlcValue方法出发异常:{errMsg},请联系管理员!"); return (false, ""); } } private void LogHandler(string log) { _logs.Add(new LogVm { StationCode = _stationInfo.StationCode, HisLog = Log }); StationCode = _stationInfo.StationCode; Log = log + Environment.NewLine; } #endregion #region 数据处理逻辑 /// /// 开始读取PLC数据 /// /// /// public void ReadPlc(StationInfo stationInfo, List logs) { try { _stationInfo = stationInfo; _logs = logs; _plcPoints = Program.PointKeyValues.Where(m => m.StationCode == _stationInfo.StationCode).ToList(); Task.Factory.StartNew(() => { while (true) { Thread.Sleep(Program.readplcrate); DataChange(); } }); } catch (Exception ex) { var errMsg = ex == null ? "未知异常" : ex.Message; MessageBox.Show($"DoSomething中ReadPlc方法出发异常:{errMsg},请联系管理员!"); } } /// /// 轮训读取PLC数据,并进行处理 /// private void DataChange() { try { if (_plcPoints != null && _plcPoints.Count > 0) { foreach (var plcPoint in _plcPoints) { var plcResult = PlcHelper.GetPlcVal(plcPoint.DataType, plcPoint.Address,plcPoint.Length); if (plcResult.ok) { string strVal = plcResult.val; switch (plcPoint.Key) { //工位状态 case "M3": var stationEnumVal = EnumHelper.GetEnumByValue(typeof(StationStatusEnum), int.Parse(plcResult.val)); if (stationEnumVal != null) { strVal = EnumHelper.GetEnumDescription(stationEnumVal); } else { strVal = "UNDEFINED"; } break; //产品总状态 case "M5": var enumVal = EnumHelper.GetEnumByValue(typeof(StatusEnum), int.Parse(plcResult.val)); if (enumVal != null) { strVal = EnumHelper.GetEnumDescription(enumVal); } else { strVal = "UNDEFINED"; } break; //机台状态 case "M7": strVal = plcResult.val.ToLower() == "true" ? "手动" : "自动"; break; //机型 case "M14": MacType_M14(strVal, plcPoint.DataType, plcPoint.Address); break; //请求条码打印 case "M16": Print_M16_M17(strVal); break; //请求主条码匹配 case "M19": MainMatch_M19_M20(strVal); break; //请求组件条码匹配 case "M21": SubMatch_M21_M22(strVal); break; //请求数据上传 case "M23": RequestMes_M23_M24(strVal); break; //请求指定工位数据 case "M25": GetStationData_M25_M26(strVal); break; default: break; } plcPoint.Val = strVal; } } } } catch (Exception ex) { var errMsg = ex == null ? "未知异常" : ex.Message; LogHandler($"数据处理异常:{errMsg}"); } } /// /// 机型 /// /// private void MacType_M14(string strVal, string dataType, string address) { try { //重新读取PLC点位值 var readResult = PlcHelper.GetPlcVal(dataType, address); if (readResult.ok) { strVal = readResult.val; } else { LogHelper.WriteErrorLog($"MacType_M14方法读取机型失败,机型值={strVal}"); } if (strVal.Contains(".")) { strVal = strVal.Substring(0, strVal.LastIndexOf(".")); } int.TryParse(strVal, out int sortIndex); //如果PLC传入的机型和上一次的不一样,则重新赋值,并且重新查数据库校验-2023-04-10 if (tempMachineType != sortIndex) { if (sortIndex > 0) { if (_stationInfo == null) { LogHelper.WriteLog($"MacType_M14方法中的工位信息为空,机型值={strVal}"); return; } var maskCode = Program.t_MaskCodes.FirstOrDefault(m => m.StationCode == _stationInfo.StationCode && m.SortIndex == sortIndex); if (maskCode == null) { var msg = $"{DateTime.Now:HH:mm:ss}>>>>>{_stationInfo.StationCode}工位PLC发送机型在一体机不存在:{sortIndex}{Environment.NewLine}"; LogHelper.WriteLog(msg); _logs.Add(new LogVm { StationCode = _stationInfo.StationCode, HisLog = msg }); StationCode = _stationInfo.StationCode; Log = msg; } else { bool needChange = false; Program.MachineType = maskCode.Category;//机型 if (!maskCode.IsUsed) { needChange = true; } if (needChange) { var rows = MskCodeHelper.UpdateMskCodeIsUsedBySortIndex(sortIndex); if (rows > 0) { var msg = $"{DateTime.Now:HH:mm:ss}>>>>>{_stationInfo.StationCode}工位进行了机型切换,机型={sortIndex},请重启上位机{Environment.NewLine}"; LogHelper.WriteLog(msg); _logs.Add(new LogVm { StationCode = _stationInfo.StationCode, HisLog = msg }); StationCode = _stationInfo.StationCode; Log = msg; } else { var msg = $"{DateTime.Now:HH:mm:ss}>>>>>{_stationInfo.StationCode}工位进行机型切换失败,机型={sortIndex}{Environment.NewLine}"; LogHelper.WriteLog(msg); _logs.Add(new LogVm { StationCode = _stationInfo.StationCode, HisLog = msg }); StationCode = _stationInfo.StationCode; Log = msg; } } } } } } catch (Exception ex) { var errMsg = ex == null ? "未知异常" : ex.Message; MessageBox.Show($"DoSomething中MacType_M14方法出发异常:{errMsg},请联系管理员!"); } } /// /// 打印条码 /// /// private void Print_M16_M17(string strVal) { if ((!string.IsNullOrWhiteSpace(strVal)) && (strVal.ToLower() == "true" || strVal == "1") && startprint && _stationInfo.IsShowPrintBtn) { startprint = false; var plcMsg = $"{DateTime.Now:HH:mm:ss}>>>>>PLC请求打印{Environment.NewLine}"; _logs.Add(new LogVm { StationCode = _stationInfo.StationCode, HisLog = plcMsg }); StationCode = _stationInfo.StationCode; Log = plcMsg; //主条码点位 var M28 = _plcPoints.FirstOrDefault(m => m.Key == "M28"); //打印条码反馈点位 var M17 = _plcPoints.FirstOrDefault(m => m.Key == "M17"); //机种信息 var M14 = _plcPoints.FirstOrDefault(m => m.Key == "M14"); //产品总状态 var M5 = _plcPoints.FirstOrDefault(m => m.Key == "M5"); //启动反馈点位 var M20 = _plcPoints.FirstOrDefault(m => m.Key == "M20"); if (M28 != null && !string.IsNullOrWhiteSpace(M28.Val)) { try { //调用打印 var (ok, resultBarcode, msg) = PrintByHttp_1(M28.Val, _stationInfo); if (ok) { BarCode = resultBarcode; //打印反馈 Program.siemensS7Net.Write(M17.Address, ushort.Parse("1")); } else { //打印反馈 Program.siemensS7Net.Write(M17.Address, ushort.Parse("2")); } _logs.Add(new LogVm { StationCode = _stationInfo.StationCode, HisLog = msg }); StationCode = _stationInfo.StationCode; Log = msg; } catch (Exception ex) { startprint = true; var msg = ex == null ? "操作异常" : ex.Message; LogHelper.WriteErrorLog($"标签打印时异常:{msg}"); //打印反馈 Program.siemensS7Net.Write(M17.Address, ushort.Parse("2")); } } else { var msg = $"{DateTime.Now:HH:mm:ss}>>>>>一体机打印时反馈主条码为空无法匹配机型{Environment.NewLine}"; _logs.Add(new LogVm { StationCode = _stationInfo.StationCode, HisLog = msg }); StationCode = _stationInfo.StationCode; Log = msg; startprint = true; } } else { startprint = true; } } /// /// 主条码变化-匹配 /// private void MainMatch_M19_M20(string strVal) { if (!string.IsNullOrWhiteSpace(strVal) && strVal == "1" && runMainMatch) { runMainMatch = false; LogHandler($"{Environment.NewLine}{DateTime.Now:HH:mm:ss}>>>>>PLC请求主条码匹配"); var M20 = _plcPoints.FirstOrDefault(m => m.Key == "M20"); try { //主条码校验 var (ok, valresult, msg) = CheckCode.StartCheck(_stationInfo, _plcPoints); _logs.Add(new LogVm { StationCode = _stationInfo.StationCode, HisLog = $"{DateTime.Now:HH:mm:ss}>>>>>主条码匹配反馈:{(ok ? "OK" : "NG")},详情:{msg}{Environment.NewLine}" }); StationCode = _stationInfo.StationCode; Log = $"{DateTime.Now:HH:mm:ss}>>>>>主条码匹配反馈:{(ok ? "OK" : "NG")},详情:{msg}{Environment.NewLine}"; if (ok) { Program.siemensS7Net.Write(M20.Address, ushort.Parse("1")); } else { Program.siemensS7Net.Write(M20.Address, ushort.Parse("2")); } } catch (Exception ex) { Program.siemensS7Net.Write(M20.Address, ushort.Parse("2")); var errMsg = ex == null ? "未知异常" : ex.Message; LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>{errMsg}"); runMainMatch = true; } } else { runMainMatch = true; } } /// /// 组件条码变化-匹配 /// private void SubMatch_M21_M22(string strVal) { if (!string.IsNullOrWhiteSpace(strVal) && (strVal == "1" || strVal == "2") && runSubMatch) { runSubMatch = false; LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>PLC请求组件条码匹配"); try { string errmsg; //机种 var M14 = _plcPoints.FirstOrDefault(m => m.Key == "M14"); int.TryParse(M14.Val, out int intMachineType); var mskCode = Program.t_MaskCodes.FirstOrDefault(m => m.StationCode == _stationInfo.StationCode && m.SortIndex == intMachineType); if (mskCode == null) { LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>组件条码匹配反馈:NG(未查询到掩码表数据,工位:{_stationInfo.StationCode},机型={M14.Val})"); runSubMatch = true; CodeMatchUploadResult(false); return; } //支线查询逻辑 T_BranchInfo branchInfo = new T_BranchInfo(); var subPoints = _plcPoints.Where(m => m.IsSub).OrderBy(m => m.Key).ToList(); if (mskCode.IsUseStator) { branchInfo = BranchInfoHelper.GetBranchInfoByStationCodeAndProductSfcCode(_stationInfo.StationCode, mskCode.ProductSfcCode); if (branchInfo == null) { LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>验证组件码反馈:NG(未查询到支线配置信息,工位:{_stationInfo.StationCode},机壳条码:{mskCode.ProductSfcCode})"); runSubMatch = true; CodeMatchUploadResult(false); return; } } if (subPoints.Count <= 0) { errmsg = $"{_stationInfo.StationName}组件条码本地匹配反馈:NG(PLC点位表未获取到组件条码配置)"; LogHelper.WriteLog(errmsg); LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>{errmsg}"); runSubMatch = true; CodeMatchUploadResult(false); return; } var subCodeVms = new List(); for (int i = 0; i < subPoints.Count; i++) { //重新读取组件条码 if (string.IsNullOrWhiteSpace(subPoints[i].Val)) { subPoints[i].Val = PlcHelper.GetPlcVal(subPoints[i].DataType, subPoints[i].Address, subPoints[i].Length).val; } if (!string.IsNullOrWhiteSpace(subPoints[i].Val)) { subCodeVms.Add(new SubCodeVM { SubCode = subPoints[i].Val, SortIndex = i + 1 }); } } if (subCodeVms.Count <= 0) { errmsg = $"{_stationInfo.StationName}组件条码本地匹配反馈:NG(未获取到组件条码)"; LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>{errmsg}"); runSubMatch = true; CodeMatchUploadResult(false); return; } //组件条码本地校验 var (ok, msg) = CheckCode.MachCode(mskCode, subCodeVms); LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>{msg}"); //本地校验失败,就无需往下执行 if (!ok) { CodeMatchUploadResult(ok); runSubMatch = true; return; } //在线模式 if (Program.SysMode) { //支线查询逻辑 if (mskCode.IsUseStator) { //构建查询参数 var subCode = subCodeVms.FirstOrDefault()?.SubCode; string[] lins = null; if (!string.IsNullOrWhiteSpace(branchInfo.BranchResource)) { if (branchInfo.BranchResource.Contains(",")) { lins = branchInfo.BranchResource.Split(','); } else { lins = new string[1]; lins[0] = branchInfo.BranchResource; } } var checkSubResult = CheckCode.CheckSubCodesByOnLine(_stationInfo, lins, branchInfo.BranchSite, branchInfo.BranchProcedure, branchInfo.BranchPara, subCodeVms); ok = checkSubResult.ok; if (ok) { LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>{checkSubResult.msg}"); } else { LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>{checkSubResult.msg}"); CodeMatchUploadResult(ok); runSubMatch = true; return; } } } CodeMatchUploadResult(ok); runSubMatch = true; } catch (Exception ex) { LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>{ex}"); CodeMatchUploadResult(false); runSubMatch = true; } } else { runSubMatch = true; } } /// /// 上报MES操作 /// /// /// /// /// private void RequestMes_M23_M24(string strVal) { if (!string.IsNullOrWhiteSpace(strVal) && strVal == "1" && savedb && runRequestMes) { runRequestMes = false; //执行结果状态 bool isCheck = false; try { #region 获取主条码 //主条码 var M28 = _plcPoints.FirstOrDefault(m => m.Key == "M28"); //重新读取主条码 var plcResult = PlcHelper.GetPlcVal(M28?.DataType, M28?.Address); if (plcResult.ok) { M28.Val = plcResult.val.Replace("\r", ""); } var plcMsg = $"{DateTime.Now:HH:mm:ss}>>>>>PLC请求数据上传,主条码={M28.Val}{Environment.NewLine}"; _logs.Add(new LogVm { StationCode = _stationInfo.StationCode, HisLog = plcMsg }); StationCode = _stationInfo.StationCode; Log = plcMsg; if (string.IsNullOrWhiteSpace(M28.Val) || M28.Val == "ERROR") { var errMsg = $"一体机数据上传反馈:{_stationInfo.StationCode}工位数据上传NG,壳体条码异常!"; _logs.Add(new LogVm { StationCode = _stationInfo.StationCode, HisLog = $"{DateTime.Now:HH:mm:ss}>>>>>{errMsg}{Environment.NewLine}" }); StationCode = _stationInfo.StationCode; Log = $"{DateTime.Now:HH:mm:ss}>>>>>{errMsg}{Environment.NewLine}"; DataUploadResult(false); return; } savedb = false; #endregion #region 本地存储 //存本地库 var (ok, msg,_result) = CheckCode.SaveDb(_stationInfo.StationCode, BarCode); _logs.Add(new LogVm { StationCode = _stationInfo.StationCode, HisLog = $"{DateTime.Now:HH:mm:ss}>>>>>{msg}{Environment.NewLine}" }); StationCode = _stationInfo.StationCode; Log = $"{DateTime.Now:HH:mm:ss}>>>>>{msg}{Environment.NewLine}"; if (!ok) { DataUploadResult(ok); return; } #endregion #region 上传MES if (Program.SysMode) { var M14 = _plcPoints.FirstOrDefault(m => m.Key == "M14"); var mskCode = Program.t_MaskCodes.First(m => m.StationCode == _stationInfo.StationCode && m.SortIndex == int.Parse(M14.Val)); if (mskCode != null) { var M5 = _plcPoints.FirstOrDefault(m => m.Key == "M5"); var testList = Program.PointKeyValues .Where(m => m.StationCode == _stationInfo.StationCode && m.IsTestItem).ToList(); WorkData workData = new WorkData(); List dParams = new List(); if (testList != null) { foreach (var item in testList) { DParams param = new DParams(); param.n = item.KeyDesc; param.ng = item.Result; param.v = item.Val; dParams.Add(param); } } workData.dparams = dParams; workData._operator = _result.LoginUser; workData.snList = _result.BulkParts; workData.qc = _result.TotalStatus == "OK" ? 0 : 1; workData.productCode = _result.Category; workData.workStationCode = _result.StationCode; workData.rfId = _result.ProductSfcCode; ResponseParam result = HttpUtilApi.submitWorkStationData(workData); if (result.code != 200) { ok = false; } _logs.Add(new LogVm { StationCode = _stationInfo.StationCode, HisLog = $"{DateTime.Now:HH:mm:ss}>>>>>{result.msg}{Environment.NewLine}" }); } } else { isCheck = ok; } #endregion #region Delete By wenjy 2024-03-07 //if (Program.SysMode) //{ // //机种信息 // var M14 = _plcPoints.FirstOrDefault(m => m.Key == "M14"); // var mskCode = Program.t_MaskCodes.First(m => m.StationCode == _stationInfo.StationCode && m.SortIndex == int.Parse(M14.Val)); // if (mskCode != null) // { // var M5 = _plcPoints.FirstOrDefault(m => m.Key == "M5"); // var testList = Program.PointKeyValues // .Where(m => m.StationCode == _stationInfo.StationCode && m.IsTestItem).ToList(); // var showTestList = testList.Where(m => m.IsShowMain).ToList(); // if (showTestList.Count > 0) // { // for (int i = 0; i < showTestList.Count; i++) // { // var data = testList.FirstOrDefault(m => m.Key.Contains(showTestList[i].Key) && !m.IsShowMain); // if (data != null) // { // showTestList[i].Result = (data.Val == "1" || data.Val == "1.000") ? "OK" : ((data.Val == "2" || data.Val == "2.000") ? "NG" : $"{data.Val}"); // } // } // var postMesList = showTestList.Select(m => new PostMesTestItemVM // { // TestItem = m.KeyDesc, // TestResult = m.Result, // TestVal = m.Val // }).ToList(); // List list_postMesList_Sub = new List(); // if (_stationInfo.StationCode == Program.DongJingPanST) // { // var M29_value = ReadPlcValue("M29"); // list_postMesList_Sub.Add(new PostMesSubInfo() // { // SubCode = M29_value.value, // MaterialNum = mskCode.Sub1SfcItemCode, // VersionNo = mskCode.Sub1ItemVersion, // ItemType = mskCode.Sub1ItemType // }); // var M30_value = ReadPlcValue("M30"); // list_postMesList_Sub.Add(new PostMesSubInfo() // { // SubCode = M30_value.value, // MaterialNum = mskCode.Sub2SfcItemCode, // VersionNo = mskCode.Sub2ItemVersion, // ItemType = mskCode.Sub2ItemType // }); // } // else // { // var sub_Code = _plcPoints.Where(m => m.IsSub && m.Key != "M100").ToList(); // LogHelper.WriteLog($"离散绑定时获取到的组件条码:{JsonConvert.SerializeObject(sub_Code)}"); // foreach (var item_Sub_code in sub_Code) // { // if (!string.IsNullOrWhiteSpace(item_Sub_code.Val)) // { // var postMesList_Sub = new PostMesSubInfo // { // SubCode = item_Sub_code.Val, // MaterialNum = mskCode.Sub1SfcItemCode, // VersionNo = mskCode.Sub1ItemVersion, // ItemType = mskCode.Sub1ItemType // }; // list_postMesList_Sub.Add(postMesList_Sub); // } // } // } // if (_stationInfo.IsBindAssembleCode) // { // Stopwatch stopwatch = new Stopwatch(); // stopwatch.Start(); // //离散装配条码绑定 // var MesResult = RequestMes.BindCode(mskCode.Site, M28.Val, mskCode.Resource, mskCode.Procedure, CurrentUser.UserName, list_postMesList_Sub); // stopwatch.Stop(); // if (MesResult.ok) // { // _logs.Add(new LogVm // { // StationCode = _stationInfo.StationCode, // HisLog = $"{DateTime.Now:HH:mm:ss}>>>>>离散绑定组件成功:{MesResult.msg},耗时:{stopwatch.ElapsedMilliseconds}{Environment.NewLine}" // }); // StationCode = _stationInfo.StationCode; // Log = $"{DateTime.Now:HH:mm:ss}>>>>>离散绑定组件成功:{MesResult.msg},耗时:{stopwatch.ElapsedMilliseconds}{Environment.NewLine}"; // } // else // { // _logs.Add(new LogVm // { // StationCode = _stationInfo.StationCode, // HisLog = $"{DateTime.Now:HH:mm:ss}>>>>>离散绑定组件NG:{MesResult.msg},请注意,耗时:{stopwatch.ElapsedMilliseconds}{Environment.NewLine}" // }); // StationCode = _stationInfo.StationCode; // Log = $"{DateTime.Now:HH:mm:ss}>>>>>离散绑定组件NG:{MesResult.msg},请注意,耗时:{stopwatch.ElapsedMilliseconds}{Environment.NewLine}"; // var updateRow = ResultHelper.UpdateResultByProductSfcCode(_stationInfo.StationCode, M28.Val, false, MesResult.msg); // if (updateRow > 0) // { // LogHelper.WriteLog($"调用MES的离散绑定接口失败时更新结果表成功,工位编码={_stationInfo.StationCode},条码={M28.Val}"); // } // else // { // LogHelper.WriteLog($"调用MES的离散绑定接口失败时更新结果表失败,工位编码={_stationInfo.StationCode},条码={M28.Val}"); // } // return; // } // } // //PASS // if (M5.Val.ToUpper() == "UNDEFINED") // { // var M5PlcResult = PlcHelper.GetPlcVal(M5.DataType, M5.Address); // if (M5PlcResult.ok) // { // M5.Val = M5PlcResult.val; // } // else // { // LogHelper.WriteLog($"调用MES接口前,读取M5点位:{M5.Address},值时失败"); // } // } // if (M5.Val == "OK" || M5.Val == "1" || M5.Val == "1.000") // { // Stopwatch stopwatch = new Stopwatch(); // stopwatch.Start(); // var MesResult = RequestMes.Pass(mskCode.Site, M28.Val, mskCode.Resource, mskCode.Procedure, CurrentUser.UserName, Program.testfilename, Program.programversion, postMesList); // stopwatch.Stop(); // isCheck = MesResult.ok; // if (MesResult.ok) // { // Log = $"{DateTime.Now:HH:mm:ss}>>>>>PASS接口调用成功,主条码={M28.Val},上传MES成功!,耗时:{stopwatch.ElapsedMilliseconds}{Environment.NewLine}"; // _logs.Add(new LogVm // { // StationCode = _stationInfo.StationCode, // HisLog = Log, // }); // } // else // { // try // { // LogHelper.WriteLog($"调用MES的PASS接口失败,工位编码={_stationInfo.StationCode},条码={M28.Val}"); // var updateRow = ResultHelper.UpdateResultByProductSfcCode(_stationInfo.StationCode, M28.Val, false, MesResult.msg); // if (updateRow > 0) // { // LogHelper.WriteLog($"调用MES的PASS接口失败时更新结果表成功,工位编码={_stationInfo.StationCode},条码={M28.Val}"); // } // else // { // LogHelper.WriteLog($"调用MES的PASS接口失败时更新结果表失败,工位编码={_stationInfo.StationCode},条码={M28.Val}"); // } // } // catch (Exception ex) // { // var errMsg = ex == null ? "修改结果表失败" : $"修改结果表失败:{ex.Message}"; // LogHelper.WriteLog(errMsg); // } // _logs.Add(new LogVm // { // StationCode = _stationInfo.StationCode, // HisLog = $"{DateTime.Now:HH:mm:ss}>>>>>PASS接口调用NG,主条码={M28.Val},耗时:{stopwatch.ElapsedMilliseconds},详情:{MesResult.msg},请取走产品!{Environment.NewLine}" // }); // StationCode = _stationInfo.StationCode; // Log = $"{DateTime.Now:HH:mm:ss}>>>>>PASS接口调用NG,主条码={M28.Val},耗时:{stopwatch.ElapsedMilliseconds},详情:{MesResult.msg},请取走产品!{Environment.NewLine}"; // } // } // else // { // Stopwatch stopwatch = new Stopwatch(); // stopwatch.Start(); // var MesResult = RequestMes.Error(mskCode.Site, M28.Val, mskCode.Resource, mskCode.Procedure, CurrentUser.UserName, Program.testfilename, Program.programversion, mskCode.NgCode, postMesList); // stopwatch.Stop(); // isCheck = MesResult.ok; // if (MesResult.ok) // { // _logs.Add(new LogVm // { // StationCode = _stationInfo.StationCode, // HisLog = $"{DateTime.Now:HH:mm:ss}>>>>>ERROR接口调用成功,耗时:{stopwatch.ElapsedMilliseconds}{Environment.NewLine}" // }); // StationCode = _stationInfo.StationCode; // Log = $"{DateTime.Now:HH:mm:ss}>>>>>ERROR接口调用成功,耗时:{stopwatch.ElapsedMilliseconds}{Environment.NewLine}"; // } // else // { // try // { // LogHelper.WriteLog($"调用MES的ERROR接口NG,工位编码={_stationInfo.StationCode},条码={M28.Val}"); // var updateRow = ResultHelper.UpdateResultByProductSfcCode(_stationInfo.StationCode, M28.Val, false, MesResult.msg); // if (updateRow > 0) // { // LogHelper.WriteLog($"调用MES的ERROR接口失败时更新结果表成功,工位编码={_stationInfo.StationCode},条码={M28.Val}"); // } // else // { // LogHelper.WriteLog($"调用MES的ERROR接口失败时更新结果表失败,工位编码={_stationInfo.StationCode},条码={M28.Val}"); // } // } // catch (Exception ex) // { // var errMsg = ex == null ? "修改结果表失败" : $"修改结果表失败:{ex.Message}"; // LogHelper.WriteLog(errMsg); // } // _logs.Add(new LogVm // { // StationCode = _stationInfo.StationCode, // HisLog = $"{DateTime.Now:HH:mm:ss}>>>>>ERROR接口调用NG,耗时:{stopwatch.ElapsedMilliseconds},详情:{MesResult.msg},请取走产品!{Environment.NewLine}" // }); // StationCode = _stationInfo.StationCode; // Log = $"{DateTime.Now:HH:mm:ss}>>>>>ERROR接口调用NG,耗时:{stopwatch.ElapsedMilliseconds},详情:{MesResult.msg},请取走产品!{Environment.NewLine}"; // } // } // } // } //} //else //{ // //离线模式 // isCheck = ok; //} #endregion } catch (Exception ex) { LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>{ex.ToString()}"); } #region 返回结果 DataUploadResult(isCheck); #endregion } else { runRequestMes = true; } } /// /// 请求指定工位数据 /// /// private void GetStationData_M25_M26(string strVal) { if (!string.IsNullOrWhiteSpace(strVal) && strVal != "0" && runStationData) { runStationData = false; var plcMsg = $"{DateTime.Now:HH:mm:ss}>>>>>PLC请求指定工位数据,M25.val={strVal}{Environment.NewLine}"; try { _logs.Add(new LogVm { StationCode = _stationInfo.StationCode, HisLog = plcMsg }); StationCode = _stationInfo.StationCode; Log = plcMsg; if (strVal.Length != 5) { var checkMsg = $"{DateTime.Now:HH:mm:ss}>>>>>PLC请求指定工位数据时传递的值NG:{strVal}{Environment.NewLine}"; _logs.Add(new LogVm { StationCode = _stationInfo.StationCode, HisLog = checkMsg }); StationCode = _stationInfo.StationCode; Log = checkMsg; } else { //在线模式 if (Program.SysMode) { if (_stationInfo.StationCode == Program.YZCS_ST) { var M8 = _plcPoints.FirstOrDefault(m => m.Key == "M8"); if (M8 != null) { var (ok, val) = PlcHelper.GetPlcVal(M8.DataType, M8.Address); //判断是否是返修品上线,此处只有补油工位使用 if (ok && val.ToLower() == "true") { CheckCode.SearchEmptyTotalWeight(_plcPoints, out var emptyTotalWeight); var M31 = _plcPoints.FirstOrDefault(m => m.Key == "M31"); var writeOk = Program.siemensS7Net.Write(M31.Address, emptyTotalWeight); LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>一体机启动反馈:获取压缩机总空重成功,数据值:{emptyTotalWeight},写入M31"); var M26 = _plcPoints.FirstOrDefault(m => m.Key == "M26"); if (writeOk.IsSuccess) { Program.siemensS7Net.Write(M26.Address, ushort.Parse("1")); } else { Program.siemensS7Net.Write(M26.Address, ushort.Parse("2")); } } else { BYKZ(); } } else { BYKZ(); } } } else { //请求指定工位数据 var (ok, msg) = CheckCode.SearchAskData(_plcPoints); var M26 = _plcPoints.FirstOrDefault(m => m.Key == "M26"); if (ok.HasValue) { if (ok.Value) { Program.siemensS7Net.Write(M26.Address, ushort.Parse("1")); } else { Program.siemensS7Net.Write(M26.Address, ushort.Parse("2")); } } _logs.Add(new LogVm { StationCode = _stationInfo.StationCode, HisLog = $"{DateTime.Now:HH:mm:ss}>>>>>{msg}{Environment.NewLine}" }); StationCode = _stationInfo.StationCode; Log = $"{DateTime.Now:HH:mm:ss}>>>>>{msg}{Environment.NewLine}"; } } } catch (Exception ex) { LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>NG:{ex}"); } } else { runStationData = true; } } #region 动静盘操作 //动静盘相关参数 double? _dongPanValue = null; double? _jingPanValue = null; /// /// 动静盘操作(先扫动盘,再扫静盘) /// private bool DongJingPan(List subCodeVMs) { bool isOk = false; var _dongPanCode = subCodeVMs.FirstOrDefault(m => m.SortIndex == 1)?.SubCode; var jingPanCode = subCodeVMs.FirstOrDefault(m => m.SortIndex == 2)?.SubCode; if (Program.DongJingDvalue.Count > 0) { if (_dongPanValue == null) { //置空动静盘缓存值 ClearDongJingCache(); //动盘判断,获取M29数据 isOk = DongPan_M29(_dongPanCode); } else { //静盘判断,获取M30数据 isOk = JingPan_M30(jingPanCode); //置空动静盘缓存值 ClearDongJingCache(); } } else { if (Program.DongPanLines != null && Program.DongPanLines.Length > 0) { isOk = DongPan_M29(_dongPanCode); } if (Program.JingPanLines != null && Program.JingPanLines.Length > 0) { isOk = JingPan_M30(jingPanCode); } } if (!isOk) { LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>调用MES获取动盘/静盘数据时校验未通过!"); } return isOk; } /// /// 获取动盘参数值 /// /// /// /// /// private bool DongPan_M29(string _dongPanCode) { //执行结果状态 bool isOk = false; try { //在线模式,读取MES数据 if (Program.DongPanLines != null && Program.DongPanLines.Length > 0) { List dongpanParam = new List() { Program.DongPan_Param }; foreach (string line in Program.DongPanLines) { var info = RequestMes.GetIntegratedData("查询动盘支线参数", _stationInfo.Site, _dongPanCode, line, Program.DongPanProcedure, CurrentUser.UserName, dongpanParam); //若没有配置则不校验 if (Program.DongJingDvalue.Count > 0) { if (info.ok) { var panValue = ParsePanValue(info.msg); isOk = panValue.ok; if (panValue.ok) { _dongPanValue = panValue.value; LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>获取动盘支线参数:{_dongPanValue},PASS"); break; } } } else { isOk = info.ok; if (info.ok) { LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>验证动动盘成功,PASS"); break; } } } } else { isOk = true; } } catch (Exception ex) { var errMsg = ex == null ? "未知异常" : ex.Message; LogHelper.WriteErrorLog($"DoSomething中DongPan_M29方法出发异常:{errMsg},请联系管理员!"); } return isOk; } /// /// 获取静盘参数值 /// /// /// /// /// private bool JingPan_M30(string jingCode) { try { //执行结果状态 bool isOk = false; //在线模式,读取MES数据 if (Program.JingPanLines != null && Program.JingPanLines.Length > 0) { List jingpanParam = new List() { Program.JingPan_Param }; foreach (string line in Program.JingPanLines) { var info = RequestMes.GetIntegratedData("查询静盘支线参数", _stationInfo.Site, jingCode, line, Program.JingPanProcedure, CurrentUser.UserName, jingpanParam); //若没有配置则不校验 if (Program.DongJingDvalue.Count > 0) { if (info.ok) { var panValue = ParsePanValue(info.msg); isOk = panValue.ok; if (panValue.ok) { _jingPanValue = panValue.value; LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>获取静盘支线参数:{_jingPanValue},PASS"); //动静盘验证 var computeResult = CheckDongJingPanValue(); isOk = computeResult.ok; if (computeResult.ok) { LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>验证动静盘成功,PASS"); break; } } } } else { isOk = info.ok; if (info.ok) { LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>验证动静盘成功,PASS"); } else { LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>" + info.msg); } break; } } } else { isOk = true; } return isOk; } catch (Exception ex) { var errMsg = ex == null ? "未知异常" : ex.Message; LogHelper.WriteErrorLog($"DoSomething中JingPan_M30方法出发异常:{errMsg},请联系管理员!"); return false; } } /// /// 置空动静盘缓存值 /// private void ClearDongJingCache() { //置空动静盘缓存值 _dongPanValue = null; _jingPanValue = null; } /// /// 解析参数返回值(动静盘) /// /// /// private (bool ok, double value, string error) ParsePanValue(string msg) { bool ok = false; double value = 0; string error = string.Empty; try { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(msg.Replace("Return info:", "").Trim()); XmlNode xmlNode = xmlDoc.SelectSingleNode("/PRODUCTION_RESPONSE/SIDE/LIST/ITEM"); string[] values = xmlNode.InnerText.Split(','); value = Convert.ToDouble(values[2]); ok = true; } catch (Exception ex) { ok = false; value = 0; error = "解析数据错误," + ex.Message; } return (ok, value, error); } /// /// 检验动静盘验证结果 /// /// private (bool ok, double dvalue, string error) CheckDongJingPanValue() { double dvalue = new double(); try { if (_jingPanValue == null && _jingPanValue.Value == 0.0) { return (false, dvalue, "静盘板厚平均值为空,NG"); } if (_dongPanValue == null && _dongPanValue.Value == 0.0) { return (false, dvalue, "动盘板厚平均值为空,NG"); } dvalue = _jingPanValue.Value - _dongPanValue.Value; LogHandler($"当前动静盘差值:{dvalue},设定的范围({Program.DongJingDvalue[0]}-{Program.DongJingDvalue[1]}),OK"); if (dvalue < Program.DongJingDvalue[0] || dvalue > Program.DongJingDvalue[1]) { return (false, dvalue, $"当前动静盘差值超过设定的范围,NG"); } return (true, dvalue, string.Empty); } catch (Exception ex) { return (false, dvalue, ex.Message); } } #endregion #region 补油空重 /// /// 补油空重 /// private void BYKZ() { try { bool isCheck = false; //请求MES“空重”参数 var M28 = ReadPlcValue("M28"); if (M28.ok) { var aheadParas = AheadParaHelper.QueryAheadVM(_stationInfo.StationCode, _stationInfo.Category).ToList().Select(m => m.PointCode).ToList(); var aheadlist_stationcode = AheadParaHelper.Query_Ahead(_stationInfo.StationCode, _stationInfo.MachineType); var mskCode_ahead = MskCodeHelper.QueryMaskCodeByStationCodeAndLastStationCode(_stationInfo.StationCode, aheadlist_stationcode); if (aheadParas.Count > 0) { var mesResult = RequestMes.GetIntegratedData("查询上工位数据", _stationInfo.Site, M28.value, mskCode_ahead.Resource, mskCode_ahead.Procedure, CurrentUser.UserName, aheadParas); isCheck = mesResult.ok; if (mesResult.ok) { //获取数值 var values = GetItemValues(mesResult.msg); if (values.ok && values.keyValues != null) { if (values.keyValues.Count > 0) { var value = values.keyValues.ElementAt(0); var M31 = _plcPoints.FirstOrDefault(m => m.Key == "M31"); if (string.IsNullOrWhiteSpace(value.Value)) { LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>一体机启动反馈:BYKZ()获取值为空!"); } else { var parseResult = float.TryParse(value.Value, out var weight); if (!parseResult) { LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>一体机启动反馈:BYKZ()值转换float失败,值={value.Value}!"); } else { Program.siemensS7Net.Write(M31.Address, weight); LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>一体机启动反馈:获取[{value.Key}]成功,数据值:{value.Value},写入M31"); } } } if (values.keyValues.Count > 1) { var value = values.keyValues.ElementAt(1); var M32 = _plcPoints.FirstOrDefault(m => m.Key == "M32"); Program.siemensS7Net.Write(M32.Address, float.Parse(value.Value)); LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>一体机启动反馈:获取[{value.Key}]成功,数据值:{value.Value},写入M32"); } } LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>一体机启动反馈:调用上工位接口成功"); } else { LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>一体机启动反馈:调用上工位接口失败," + mesResult.msg); } } else { LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>一体机启动反馈:NG(未匹配到{_stationInfo.StationName}工位前置参数的配置记录)"); } } var M26 = _plcPoints.FirstOrDefault(m => m.Key == "M26"); if (isCheck) { Program.siemensS7Net.Write(M26.Address, ushort.Parse("1")); } else { Program.siemensS7Net.Write(M26.Address, ushort.Parse("2")); } } catch (Exception ex) { var errMsg = ex == null ? "未知异常" : ex.Message; MessageBox.Show($"DoSomething中BYKZ方法出发异常:{errMsg},请联系管理员!"); } } private (bool ok, Dictionary keyValues, string error) GetItemValues(string msg) { bool ok = false; Dictionary keyValues = null; string error = string.Empty; try { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(msg.Replace("Return info:", "").Trim()); XmlNodeList xmlNodeList = xmlDoc.SelectNodes("/PRODUCTION_RESPONSE/SIDE/LIST"); if (xmlNodeList != null && xmlNodeList.Count > 0) { keyValues = new Dictionary(); foreach (XmlNode xmlNode in xmlNodeList) { string[] arrays = xmlNode.InnerText.Split(','); keyValues.Add(arrays[0], arrays[2]); } } ok = true; } catch (Exception ex) { ok = false; keyValues = null; error = "解析数据错误," + ex.Message; } return (ok, keyValues, error); } #endregion #region 贴标打印 /// /// 通过BatTender打印条码 /// /// /// /// public (bool ok, string barCode, string msg) PrintByHttp_1(string productSfcCode, StationInfo stationInfo) { var mskcode = MskCodeHelper.QueryMask_codeByStationAndSfcCode(stationInfo.StationCode, productSfcCode); if (mskcode == null) { return (false, "", $"{DateTime.Now:HH:mm:ss}>>>>>一体机反馈打印时根据工位和机壳码查询掩码表异常,工位编码={stationInfo.StationCode},机壳码={productSfcCode}{Environment.NewLine}"); } string barCode = Program.PrintBarCode; LogHelper.WriteLog($"打印时生成的barcode:{barCode}"); LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>一体机反馈打印时开始进行232通讯"); (bool isOk, string barCode, string msg) result = (false, "", ""); string _serialNum = string.Empty;//序列号 //根据掩码表配置的程序块号调用不通的通讯方法 switch (mskcode.ProgramBlockNum) { case 1: //CAN1通讯 result = CAN1Helper.CanCommunication(mskcode, barCode); break; case 2: //CAN2通讯 result = CAN2Helper.CanCommunication(mskcode, barCode, Program.SerialNum, out _serialNum); break; case 3: //LIN通讯 result = LINHelper.LinCommunication(mskcode, barCode); break; default: break; } if (!result.isOk) { return result; } //http打印 try { string version = $"SW:{result.msg}/BC34-1200V-C-V0.11"; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); T_PrintInfo t_PrintInfo = new T_PrintInfo { Id = Guid.NewGuid().ToString(), FileName = mskcode.ProductSfcCode.Trim('*'), BarCode = barCode, SoftVersion = version, CreatedTime = DateTime.Now }; var row = MskCodeHelper.AddPrintInfo(t_PrintInfo); LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>一体机反馈打印时存储打印参数,存储结果:{row}"); if (row <= 0) { return (false, barCode, $"{DateTime.Now:HH:mm:ss}>>>>>一体机反馈:打印信息发送失败,保存打印参数耗时:{stopwatch.ElapsedMilliseconds}毫秒{Environment.NewLine}"); } #region 监听打印结果 bool printResult = false; Stopwatch stopwatch1 = new Stopwatch(); stopwatch1.Start(); bool doWhile = true; while (doWhile) { Thread.Sleep(500); doWhile = false; if (stopwatch1.ElapsedMilliseconds >= 10000) { break;//超时跳出循环 } var printInfo = MskCodeHelper.QueryPrintInfo(barCode); if (printInfo == null) { break;//查询打印参数为空跳出循环 } else if (printInfo.PrintResult.HasValue && printInfo.PrintResult.Value) { printResult = true; break;//打印成功跳出循环 } else if (printInfo.PrintResult.HasValue && !printInfo.PrintResult.Value) { break;//打印失败跳出循环 } doWhile = true; } #endregion stopwatch.Stop(); if (printResult) { return (true, barCode, $"{DateTime.Now:HH:mm:ss}>>>>>一体机反馈:打印成功,打印条码信息:{barCode},序列号:{_serialNum},耗时:{stopwatch.ElapsedMilliseconds}毫秒{Environment.NewLine}"); } else { return (false, barCode, $"{DateTime.Now:HH:mm:ss}>>>>>一体机反馈:打印失败,序列号:{_serialNum},耗时:{stopwatch.ElapsedMilliseconds}毫秒{Environment.NewLine}"); } } catch (Exception ex) { var msg = ex == null ? "操作异常" : ex.Message; return (false, barCode, $"{DateTime.Now:HH:mm:ss}>>>>>一体机反馈:调用外挂打印时失败,{msg}"); } } #endregion /// /// 数据上传反馈 /// /// private void DataUploadResult(bool isOk) { //数据上传反馈 var M24 = _plcPoints.FirstOrDefault(m => m.Key == "M24"); if (M24 != null) { if (isOk) { var result = Program.siemensS7Net.Write(M24.Address, ushort.Parse("1")); if (!result.IsSuccess) { Program.siemensS7Net.Write(M24.Address, ushort.Parse("1")); } savedb = true; LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>写入PLC地址M24,数据值:1"); } else { var result = Program.siemensS7Net.Write(M24.Address, ushort.Parse("2")); if (!result.IsSuccess) { Program.siemensS7Net.Write(M24.Address, ushort.Parse("2")); } savedb = true; LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>写入PLC地址M24,数据值:2"); } } else { LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>M24地址不存在"); } } /// /// 条码匹配反馈 /// /// private void CodeMatchUploadResult(bool isOk) { var M22 = _plcPoints.FirstOrDefault(m => m.Key == "M22"); if (M22 != null) { if (isOk) { Program.siemensS7Net.Write(M22.Address, ushort.Parse("1")); savedb = true; LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>写入PLC地址M22,数据值:1"); } else { Program.siemensS7Net.Write(M22.Address, ushort.Parse("2")); savedb = true; LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>写入PLC地址M22,数据值:2"); } } else { LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>M22地址不存在"); } } } } #endregion