MXComponent_FEM.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using ProjectBase.Data.BaseDAL;
  7. using ProjectBase.Data.Logs;
  8. using SIMDP.BLL;
  9. using SIMDP.Device;
  10. using SIMDP.Model;
  11. using SIMDP.Util;
  12. using static System.Reflection.MethodBase;
  13. using System.Configuration;
  14. using ACTMULTILib;
  15. namespace SIMDP.Project
  16. {
  17. public class MXComponent_FEM : PlcMXComponent
  18. {
  19. #region 重写父类函数
  20. public override void actionStart()
  21. {
  22. try
  23. {
  24. //重新连接PLC时,为了防止断网期间丢失了复位信号,特地在此处复位20220525
  25. ResetFeedbackSignal();
  26. }
  27. catch (Exception ex)
  28. {
  29. LogHelper.log.Error($"{GetCurrentMethod().Name}: ex.Message={ex.Message}");
  30. throw;
  31. }
  32. }
  33. private void ResetFeedbackSignal()
  34. {
  35. Read("读取产品型号和VIN码请求", out bool signal1);
  36. if (!signal1)
  37. {
  38. bool res = Write("读取产品型号和VIN码请求完成", false);
  39. LogHelper.log.Info($"PLC重连:当前 读取产品型号和VIN码请求 = 0,复位 读取产品型号和VIN码请求完成:{res}");
  40. }
  41. Read("读取其他结果请求", out bool signal2);
  42. if (!signal2)
  43. {
  44. bool res = Write("读取其他结果请求完成", false);
  45. LogHelper.log.Info($"PLC重连:当前 读取其他结果请求 = 0,复位 读取其他结果请求完成:{res}");
  46. }
  47. }
  48. bool pcHeartBeat = false;
  49. public override void actionTimer()
  50. {
  51. try
  52. {
  53. pcHeartBeat = !pcHeartBeat;
  54. bool bres = Write("PC心跳信号", pcHeartBeat);
  55. if (!bres)
  56. {
  57. this.State = false;
  58. return;
  59. }
  60. foreach (var p in monitorPoints)
  61. {
  62. if (!Read(p, out bool value))
  63. {
  64. this.State = false;
  65. break;
  66. }
  67. if (value != Convert.ToBoolean(p.value))
  68. {
  69. p.value = value;
  70. if (p.proc != null)
  71. p.proc.Invoke(this, new object[] { value });
  72. }
  73. }
  74. redis.Publish<string>(SysEnvironment.OpcDataChannel, "");
  75. }
  76. catch (Exception ex)
  77. {
  78. this.State = false;
  79. LogHelper.log.Error($"{GetCurrentMethod().Name}: ex.Message={ex.Message}");
  80. }
  81. }
  82. #endregion
  83. #region 生产数据
  84. protected class ProductionData
  85. {
  86. List<string> keys = new List<string>();
  87. public Dictionary<string, string> dict = new Dictionary<string, string>();
  88. public MoProductData model = new MoProductData();
  89. public ProductionData()
  90. {
  91. model.RuleId = BLLFactory<BLL.BlRule>.Instance.GetRuleIdByName("FEM");
  92. keys.Add("产品型号");
  93. keys.Add("力矩1");
  94. keys.Add("角度1");
  95. keys.Add("状态1");
  96. keys.Add("力矩2");
  97. keys.Add("角度2");
  98. keys.Add("状态2");
  99. keys.Add("力矩3");
  100. keys.Add("角度3");
  101. keys.Add("状态3");
  102. keys.Add("力矩4");
  103. keys.Add("角度4");
  104. keys.Add("状态4");
  105. foreach (var key in keys)
  106. {
  107. dict.Add(key, "");
  108. }
  109. }
  110. public string Dict2String()
  111. {
  112. StringBuilder builder = new StringBuilder();
  113. foreach (var d in dict)
  114. {
  115. if (builder.Length > 0)
  116. builder.Append(",");
  117. builder.Append(d.Value);
  118. }
  119. return builder.ToString();
  120. }
  121. public MoProductData toModel()
  122. {
  123. model.DataValue = this.Dict2String();
  124. return model;
  125. }
  126. public void LoadData(MoProductData data)
  127. {
  128. model.DataId = data.DataId;
  129. model.DataValue = data.DataValue;
  130. model.RuleId = data.RuleId;
  131. model.RuleTime = data.RuleTime;
  132. model.Batchid = data.Batchid;
  133. string[] values = model.DataValue.Split(',');
  134. if (values.Length > keys.Count)
  135. return;
  136. for (int i = 0; i < keys.Count; i++)
  137. {
  138. dict[keys[i]] = values[i];
  139. }
  140. }
  141. }
  142. #endregion
  143. #region 生产过程
  144. public static string vinCode = "";
  145. public void 读取产品型号和VIN码请求(object value)
  146. {
  147. bool signal = Convert.ToBoolean(value);
  148. LogHelper.log.Debug($"{GetCurrentMethod().Name}: signal = {signal} ");
  149. Read("自动模式", out bool automode);
  150. if (!automode) return;
  151. if (!signal)
  152. {
  153. Write("读取产品型号和VIN码请求完成", false);
  154. return;
  155. }
  156. Read("产品型号", out int productType);
  157. Read("VIN码", out vinCode);
  158. try
  159. {
  160. ProductionData data = new ProductionData();
  161. data.model.Batchid = vinCode;
  162. data.model.RuleTime = DateTime.Now;
  163. data.dict["产品型号"] = productType.ToString();
  164. bool res = BLLFactory<BlProductData>.Instance.Insert(data.toModel());
  165. LogHelper.log.Debug($"{GetCurrentMethod().Name}: 数据保存:{res} ");
  166. }
  167. catch (Exception ex)
  168. {
  169. LogHelper.log.Debug($"{GetCurrentMethod().Name}: {ex.Message} ");
  170. }
  171. Write("读取产品型号和VIN码请求完成", true);
  172. return;
  173. }
  174. public void 读取其他结果请求(object value)
  175. {
  176. bool signal = Convert.ToBoolean(value);
  177. LogHelper.log.Debug($"{GetCurrentMethod().Name}: signal = {signal} ");
  178. if (!signal)
  179. {
  180. Write("读取其他结果请求完成", false);
  181. return;
  182. }
  183. Read("拧紧结果1", out short itightRes1);
  184. Read("拧紧结果2", out short itightRes2);
  185. Read("拧紧结果3", out short itightRes3);
  186. Read("拧紧结果4", out short itightRes4);
  187. Read("拧紧力矩1", out float moment1);
  188. Read("拧紧力矩2", out float moment2);
  189. Read("拧紧力矩3", out float moment3);
  190. Read("拧紧力矩4", out float moment4);
  191. Read("拧紧角度1", out float angle1);
  192. Read("拧紧角度2", out float angle2);
  193. Read("拧紧角度3", out float angle3);
  194. Read("拧紧角度4", out float angle4);
  195. try
  196. {
  197. if (vinCode == "")
  198. {
  199. LogHelper.log.Debug($"{GetCurrentMethod().Name}:拧紧数据存储失败:当前VIN码为空,无法查找到对应记录");
  200. Write("读取其他结果请求完成", true);
  201. return;
  202. }
  203. MoProductData model = BLLFactory<BlProductData>.Instance.FindByBatch(vinCode);
  204. if (model == null)
  205. {
  206. LogHelper.log.Debug($"{GetCurrentMethod().Name}:拧紧数据存储失败:当前VIN码 = {vinCode},无法查找到对应记录");
  207. Write("读取其他结果请求完成", true);
  208. return;
  209. }
  210. ProductionData data = new ProductionData();
  211. data.LoadData(model);
  212. data.dict["状态1"] = GetTightResult(itightRes1);
  213. data.dict["状态2"] = GetTightResult(itightRes2);
  214. data.dict["状态3"] = GetTightResult(itightRes3);
  215. data.dict["状态4"] = GetTightResult(itightRes4);
  216. data.dict["力矩1"] = moment1.ToString();
  217. data.dict["力矩2"] = moment2.ToString();
  218. data.dict["力矩3"] = moment3.ToString();
  219. data.dict["力矩4"] = moment4.ToString();
  220. data.dict["角度1"] = angle1.ToString();
  221. data.dict["角度2"] = angle2.ToString();
  222. data.dict["角度3"] = angle3.ToString();
  223. data.dict["角度4"] = angle4.ToString();
  224. data.model.RuleTime = DateTime.Now;
  225. bool res = BLLFactory<BlProductData>.Instance.Update(data.toModel(), data.model.DataId);
  226. LogHelper.log.Debug($"{GetCurrentMethod().Name}: 数据保存:{res} ");
  227. }
  228. catch (Exception ex)
  229. {
  230. LogHelper.log.Debug($"{GetCurrentMethod().Name}: {ex.Message} ");
  231. }
  232. Write("读取其他结果请求完成", true);
  233. return;
  234. }
  235. private string GetTightResult(int ires)
  236. {
  237. switch (ires)
  238. {
  239. case 1:
  240. return "OK";
  241. case 2:
  242. return "NG";
  243. case 0:
  244. return "未拧紧";
  245. default:
  246. return ires.ToString();
  247. }
  248. }
  249. #endregion
  250. #region 本地测试
  251. public class TestRunner
  252. {
  253. public MXComponent_FEM plc;
  254. public TestRunner(MXComponent_FEM parent)
  255. {
  256. plc = parent;
  257. }
  258. CancellationTokenSource ts = new CancellationTokenSource();//线程取消标记
  259. public void Run()
  260. {
  261. Task.Run(() =>
  262. {
  263. plc.Write("自动模式", true);
  264. plc.Write("产品型号", Convert.ToInt32(1));
  265. plc.Write("VIN码", "LFV2A21K363553763");
  266. plc.Write("读取产品型号和VIN码请求", true);
  267. ReadUntil("读取产品型号和VIN码请求完成", true);
  268. plc.Write("读取产品型号和VIN码请求", false);
  269. // ReadUntil("读取产品型号和VIN码请求完成", false);
  270. plc.Write("拧紧结果1", 1);
  271. plc.Write("拧紧力矩1", 12.3f);
  272. plc.Write("拧紧角度1", 22.52f);
  273. plc.Write("读取其他结果请求", true);
  274. ReadUntil("读取其他结果请求完成", true);
  275. plc.Write("读取其他结果请求", false);
  276. //ReadUntil("读取其他结果请求完成", false);
  277. // ts.Cancel();// 如出现此句,则下面抛出异常跳出
  278. try
  279. {
  280. ts.Token.ThrowIfCancellationRequested();
  281. }
  282. catch { }
  283. }, ts.Token);
  284. }
  285. private void ReadUntil(string pointName, bool target)
  286. {
  287. while (true)
  288. {
  289. plc.Read(pointName, out bool value);
  290. if (value == target) break;
  291. else Thread.Sleep(100);
  292. }
  293. }
  294. }
  295. #endregion
  296. }
  297. }