TaskBookBehavior_IOAddress.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using Pms.DataLibrary.Order;
  2. using ProjectManagementSystem.Common.Config;
  3. using ProjectManagementSystem.Common.Core;
  4. using ProjectManagementSystem.Common.Extenions;
  5. using ProjectManagementSystem.Common.WebApi;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace ProjectManagementSystem.TaskBookEvent
  12. {
  13. /// <summary>
  14. /// IO交互行为码,使用类似PLC地址的方式,输出Y1-Y8,输入X1-X8
  15. /// </summary>
  16. public class TaskBookBehavior_IOAddress : TaskBookBehavior_PLC<bool>
  17. {
  18. //private static readonly object locker = new object();
  19. public TaskBookBehavior_IOAddress(int behavior) : base(behavior)
  20. {
  21. }
  22. protected override bool Interaction(int flag, string[] addrArray, string valueArray, TaskData taskDetailInfo, StepData taskStepInfo, bool logicBit)
  23. {
  24. if (addrArray.Length <= 0) return true;
  25. var routeConfig = ExcelConfig.Instance.GetRouteConfig(taskStepInfo.WareHouseID);
  26. if (routeConfig == null)
  27. {
  28. Log(taskDetailInfo, taskStepInfo, "查找库位配置信息失败");
  29. return false;
  30. }
  31. if (routeConfig.IOTransID <= 0)
  32. {
  33. Log(taskDetailInfo, taskStepInfo, "获取库位对接盒ID失败");
  34. return false;
  35. }
  36. int id = routeConfig.IOTransID;
  37. if (!DeviceControl.Instance.Communication.InfoIODictionary.ContainsKey(id))
  38. {
  39. Log(taskDetailInfo, taskStepInfo, $"获取库位对接盒{id},没有此盒子数据,检查盒子是否连接网络");
  40. return false;
  41. }
  42. var infoIO = DeviceControl.Instance.Communication.InfoIODictionary[id];
  43. if (!infoIO.DataValid)
  44. {
  45. Log(taskDetailInfo, taskStepInfo, $"获取库位对接盒{id},盒子已掉线!");
  46. return false;
  47. }
  48. //构造地址数组
  49. string[] realAddrArray = new string[addrArray.Length];
  50. for (int i = 0; i < addrArray.Length; i++)
  51. {
  52. string addrDesc = addrArray[i];
  53. string plcAddr = taskStepInfo.WareHouseID.GetPlcTaskAddr(addrDesc);
  54. if (string.IsNullOrEmpty(plcAddr))
  55. {
  56. Log(taskDetailInfo, taskStepInfo, $"查找地址失败:“{addrDesc}”");
  57. return false;
  58. }
  59. realAddrArray[i] = plcAddr;
  60. }
  61. //构造地址值数组
  62. var tempRealValues = valueArray.ToValueArray<bool>();
  63. var realValues = new bool[realAddrArray.Length];
  64. for (int i = 0; i < realValues.Length; i++)
  65. {
  66. realValues[i] = GetArrayValue(tempRealValues, i);
  67. //根据逻辑位来确定写的值
  68. if (PlcBehavior.LogicBitTrue > 0
  69. && logicBit == false)
  70. {
  71. realValues[i] = false;
  72. }
  73. }
  74. if (flag == 0)
  75. {
  76. //写入
  77. var result = DeviceControl.Instance.SetOutputIO_ThenCheck(id, realAddrArray, realValues, out string setMsg);
  78. if(!result)
  79. {
  80. Log(taskDetailInfo, taskStepInfo, setMsg);
  81. return result;
  82. }
  83. //DeviceControl.Instance.SetOutputIO(id, realAddrArray, realValues);
  84. //System.Threading.Thread.Sleep(100);
  85. //for (int i = 0; i < realAddrArray.Length; i++)
  86. //{
  87. // string plcAddr = realAddrArray[i];
  88. // bool plcValue = realValues[i];
  89. // bool result = DeviceControl.Instance.GetOutputIO(id, realAddrArray[i]);
  90. // if (result != plcValue)
  91. // {
  92. // Log(taskDetailInfo, taskStepInfo, $"写入失败({plcAddr}写入{plcValue})");
  93. // return false;
  94. // }
  95. //}
  96. }
  97. else
  98. {
  99. //读取
  100. var result = DeviceControl.Instance.CheckInputIO(id, realAddrArray, realValues, out string setMsg);
  101. if (!result)
  102. {
  103. Log(taskDetailInfo, taskStepInfo, setMsg);
  104. return result;
  105. }
  106. //for (int i = 0; i < realAddrArray.Length; i++)
  107. //{
  108. // string plcAddr = realAddrArray[i];
  109. // bool plcValue = realValues[i];
  110. // bool result2 = DeviceControl.Instance.GetInputIO(id, realAddrArray[i]);
  111. // if (result2 != plcValue)
  112. // {
  113. // Log(taskDetailInfo, taskStepInfo, $"等待信号({plcAddr}={plcValue})");
  114. // return false;
  115. // }
  116. //}
  117. }
  118. return true;
  119. }
  120. private bool GetArrayValue(bool[] array, int index)
  121. {
  122. if (array.Length == 0) return false;
  123. return array.Length > index ? array[index] : array[array.Length - 1];
  124. }
  125. }
  126. }