123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using ProjectBase.Data.BaseDAL;
- using ProjectBase.Data.Logs;
- using SIMDP.BLL;
- using SIMDP.Device;
- using SIMDP.Model;
- using SIMDP.Util;
- using static System.Reflection.MethodBase;
- using System.Configuration;
- using ACTMULTILib;
- namespace SIMDP.Project
- {
- public class MXComponent_FEM : PlcMXComponent
- {
- #region 重写父类函数
- public override void actionStart()
- {
- try
- {
- //重新连接PLC时,为了防止断网期间丢失了复位信号,特地在此处复位20220525
- ResetFeedbackSignal();
- }
- catch (Exception ex)
- {
- LogHelper.log.Error($"{GetCurrentMethod().Name}: ex.Message={ex.Message}");
- throw;
- }
- }
- private void ResetFeedbackSignal()
- {
- Read("读取产品型号和VIN码请求", out bool signal1);
- if (!signal1)
- {
- bool res = Write("读取产品型号和VIN码请求完成", false);
- LogHelper.log.Info($"PLC重连:当前 读取产品型号和VIN码请求 = 0,复位 读取产品型号和VIN码请求完成:{res}");
- }
- Read("读取其他结果请求", out bool signal2);
- if (!signal2)
- {
- bool res = Write("读取其他结果请求完成", false);
- LogHelper.log.Info($"PLC重连:当前 读取其他结果请求 = 0,复位 读取其他结果请求完成:{res}");
- }
- }
- bool pcHeartBeat = false;
- public override void actionTimer()
- {
- try
- {
- pcHeartBeat = !pcHeartBeat;
- bool bres = Write("PC心跳信号", pcHeartBeat);
- if (!bres)
- {
- this.State = false;
- return;
- }
- foreach (var p in monitorPoints)
- {
- if (!Read(p, out bool value))
- {
- this.State = false;
- break;
- }
- if (value != Convert.ToBoolean(p.value))
- {
- p.value = value;
- if (p.proc != null)
- p.proc.Invoke(this, new object[] { value });
- }
- }
- redis.Publish<string>(SysEnvironment.OpcDataChannel, "");
- }
- catch (Exception ex)
- {
- this.State = false;
- LogHelper.log.Error($"{GetCurrentMethod().Name}: ex.Message={ex.Message}");
- }
- }
- #endregion
- #region 生产数据
- protected class ProductionData
- {
- List<string> keys = new List<string>();
- public Dictionary<string, string> dict = new Dictionary<string, string>();
- public MoProductData model = new MoProductData();
- public ProductionData()
- {
- model.RuleId = BLLFactory<BLL.BlRule>.Instance.GetRuleIdByName("FEM");
- keys.Add("产品型号");
- keys.Add("力矩1");
- keys.Add("角度1");
- keys.Add("状态1");
- keys.Add("力矩2");
- keys.Add("角度2");
- keys.Add("状态2");
- keys.Add("力矩3");
- keys.Add("角度3");
- keys.Add("状态3");
- keys.Add("力矩4");
- keys.Add("角度4");
- keys.Add("状态4");
- foreach (var key in keys)
- {
- dict.Add(key, "");
- }
- }
- public string Dict2String()
- {
- StringBuilder builder = new StringBuilder();
- foreach (var d in dict)
- {
- if (builder.Length > 0)
- builder.Append(",");
- builder.Append(d.Value);
- }
- return builder.ToString();
- }
- public MoProductData toModel()
- {
- model.DataValue = this.Dict2String();
- return model;
- }
- public void LoadData(MoProductData data)
- {
- model.DataId = data.DataId;
- model.DataValue = data.DataValue;
- model.RuleId = data.RuleId;
- model.RuleTime = data.RuleTime;
- model.Batchid = data.Batchid;
- string[] values = model.DataValue.Split(',');
- if (values.Length > keys.Count)
- return;
- for (int i = 0; i < keys.Count; i++)
- {
- dict[keys[i]] = values[i];
- }
- }
- }
- #endregion
- #region 生产过程
- public static string vinCode = "";
- public void 读取产品型号和VIN码请求(object value)
- {
- bool signal = Convert.ToBoolean(value);
- LogHelper.log.Debug($"{GetCurrentMethod().Name}: signal = {signal} ");
- Read("自动模式", out bool automode);
- if (!automode) return;
- if (!signal)
- {
- Write("读取产品型号和VIN码请求完成", false);
- return;
- }
- Read("产品型号", out int productType);
- Read("VIN码", out vinCode);
- try
- {
- ProductionData data = new ProductionData();
- data.model.Batchid = vinCode;
- data.model.RuleTime = DateTime.Now;
- data.dict["产品型号"] = productType.ToString();
- bool res = BLLFactory<BlProductData>.Instance.Insert(data.toModel());
- LogHelper.log.Debug($"{GetCurrentMethod().Name}: 数据保存:{res} ");
- }
- catch (Exception ex)
- {
- LogHelper.log.Debug($"{GetCurrentMethod().Name}: {ex.Message} ");
- }
- Write("读取产品型号和VIN码请求完成", true);
- return;
- }
- public void 读取其他结果请求(object value)
- {
- bool signal = Convert.ToBoolean(value);
- LogHelper.log.Debug($"{GetCurrentMethod().Name}: signal = {signal} ");
- if (!signal)
- {
- Write("读取其他结果请求完成", false);
- return;
- }
- Read("拧紧结果1", out short itightRes1);
- Read("拧紧结果2", out short itightRes2);
- Read("拧紧结果3", out short itightRes3);
- Read("拧紧结果4", out short itightRes4);
- Read("拧紧力矩1", out float moment1);
- Read("拧紧力矩2", out float moment2);
- Read("拧紧力矩3", out float moment3);
- Read("拧紧力矩4", out float moment4);
- Read("拧紧角度1", out float angle1);
- Read("拧紧角度2", out float angle2);
- Read("拧紧角度3", out float angle3);
- Read("拧紧角度4", out float angle4);
- try
- {
- if (vinCode == "")
- {
- LogHelper.log.Debug($"{GetCurrentMethod().Name}:拧紧数据存储失败:当前VIN码为空,无法查找到对应记录");
- Write("读取其他结果请求完成", true);
- return;
- }
- MoProductData model = BLLFactory<BlProductData>.Instance.FindByBatch(vinCode);
- if (model == null)
- {
- LogHelper.log.Debug($"{GetCurrentMethod().Name}:拧紧数据存储失败:当前VIN码 = {vinCode},无法查找到对应记录");
- Write("读取其他结果请求完成", true);
- return;
- }
- ProductionData data = new ProductionData();
- data.LoadData(model);
- data.dict["状态1"] = GetTightResult(itightRes1);
- data.dict["状态2"] = GetTightResult(itightRes2);
- data.dict["状态3"] = GetTightResult(itightRes3);
- data.dict["状态4"] = GetTightResult(itightRes4);
- data.dict["力矩1"] = moment1.ToString();
- data.dict["力矩2"] = moment2.ToString();
- data.dict["力矩3"] = moment3.ToString();
- data.dict["力矩4"] = moment4.ToString();
- data.dict["角度1"] = angle1.ToString();
- data.dict["角度2"] = angle2.ToString();
- data.dict["角度3"] = angle3.ToString();
- data.dict["角度4"] = angle4.ToString();
- data.model.RuleTime = DateTime.Now;
- bool res = BLLFactory<BlProductData>.Instance.Update(data.toModel(), data.model.DataId);
- LogHelper.log.Debug($"{GetCurrentMethod().Name}: 数据保存:{res} ");
- }
- catch (Exception ex)
- {
- LogHelper.log.Debug($"{GetCurrentMethod().Name}: {ex.Message} ");
- }
- Write("读取其他结果请求完成", true);
- return;
- }
- private string GetTightResult(int ires)
- {
- switch (ires)
- {
- case 1:
- return "OK";
- case 2:
- return "NG";
- case 0:
- return "未拧紧";
- default:
- return ires.ToString();
- }
- }
- #endregion
- #region 本地测试
- public class TestRunner
- {
- public MXComponent_FEM plc;
- public TestRunner(MXComponent_FEM parent)
- {
- plc = parent;
- }
- CancellationTokenSource ts = new CancellationTokenSource();//线程取消标记
- public void Run()
- {
- Task.Run(() =>
- {
- plc.Write("自动模式", true);
- plc.Write("产品型号", Convert.ToInt32(1));
- plc.Write("VIN码", "LFV2A21K363553763");
- plc.Write("读取产品型号和VIN码请求", true);
- ReadUntil("读取产品型号和VIN码请求完成", true);
- plc.Write("读取产品型号和VIN码请求", false);
- // ReadUntil("读取产品型号和VIN码请求完成", false);
- plc.Write("拧紧结果1", 1);
- plc.Write("拧紧力矩1", 12.3f);
- plc.Write("拧紧角度1", 22.52f);
- plc.Write("读取其他结果请求", true);
- ReadUntil("读取其他结果请求完成", true);
- plc.Write("读取其他结果请求", false);
- //ReadUntil("读取其他结果请求完成", false);
- // ts.Cancel();// 如出现此句,则下面抛出异常跳出
- try
- {
- ts.Token.ThrowIfCancellationRequested();
- }
- catch { }
- }, ts.Token);
- }
- private void ReadUntil(string pointName, bool target)
- {
- while (true)
- {
- plc.Read(pointName, out bool value);
- if (value == target) break;
- else Thread.Sleep(100);
- }
- }
- }
- #endregion
- }
- }
|