终端一体化运控平台
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

VariableMonitorAttribute.cs 4.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using Microsoft.EntityFrameworkCore.Storage.ValueConversion.Internal;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace BPASmartClient.Device
  8. {
  9. public class VariableMonitorAttribute : Attribute
  10. {
  11. /// <summary>
  12. /// 变量描述
  13. /// </summary>
  14. /// <param name="Notes">描述</param>
  15. /// <param name="PLCAddress">PLC 地址</param>
  16. /// <param name="ModbusTcpAddress">Modbus TCP 地址</param>
  17. public VariableMonitorAttribute(string Notes, string PLCAddress = "", string ModbusTcpAddress = "")
  18. {
  19. this.PLCAddress = PLCAddress;
  20. this.ModbusTcpAddress = GetModbusTcpAdd(PLCAddress);// ModbusTcpAddress;
  21. this.Notes = Notes;
  22. }
  23. private string GetModbusTcpAdd(string address)
  24. {
  25. if (address == null) return "";
  26. if (address.Length > 0)
  27. {
  28. address = address.Trim();
  29. if (address.ToUpper().Contains("GM") && address.Length >= 3)
  30. {
  31. var res = address.Remove(0, 2);
  32. if (res != null && res.Length > 0) return (int.Parse(res) + 4096).ToString();
  33. }
  34. else if (address.ToUpper().Contains("M") && address.Length >= 4)
  35. {
  36. var res = address.Substring(1).Split('.');
  37. if (res != null && res.Length == 2)
  38. {
  39. if (int.TryParse(res[0], out int firstAddress) && int.TryParse(res[1], out int ExitAddress))
  40. {
  41. if (ExitAddress >= 0 && ExitAddress <= 7)
  42. {
  43. return ((firstAddress * 8) + 320 + ExitAddress).ToString();
  44. }
  45. }
  46. }
  47. }
  48. else if (address.ToUpper().Contains("GI") && address.Length >= 3)
  49. {
  50. var res = address.Remove(0, 2);
  51. if (res != null && res.Length > 0) return res;
  52. }
  53. else if (address.ToUpper().Contains("LB") && address.Length >= 3)
  54. {
  55. var res = address.Substring(2);
  56. if (res != null && res.Length > 0)
  57. {
  58. if (int.TryParse(res, out int firstAddress)) return firstAddress.ToString();
  59. }
  60. }
  61. else if ((address.ToUpper().Contains("VW") || address.ToUpper().Contains("VD")) && address.Length >= 3)
  62. {
  63. var res = address.Substring(2);
  64. if (res != null && int.TryParse(res, out int tempAddress))
  65. {
  66. return ((tempAddress / 2) + 100).ToString();
  67. }
  68. }
  69. else if (address.ToUpper().Contains("LW") && address.Length >= 3)
  70. {
  71. var res = address.Substring(2);
  72. if (res != null && int.TryParse(res, out int LwAddress))
  73. {
  74. return LwAddress.ToString();
  75. }
  76. }
  77. }
  78. return "";
  79. //if (address == null) return "";
  80. //if (address.Length > 0)
  81. //{
  82. // address = address.Trim();
  83. // if (address.ToUpper().Contains("M") && address.Length >= 4)
  84. // {
  85. // var res = address.Substring(1).Split('.');
  86. // if (res != null && res.Length == 2)
  87. // {
  88. // if (int.TryParse(res[0], out int firstAddress) && int.TryParse(res[1], out int ExitAddress))
  89. // {
  90. // if (ExitAddress >= 0 && ExitAddress <= 7)
  91. // {
  92. // return ((firstAddress * 8) + 320 + ExitAddress).ToString();
  93. // }
  94. // }
  95. // }
  96. // }
  97. // else if ((address.ToUpper().Contains("VW") || address.ToUpper().Contains("VD")) && address.Length >= 3)
  98. // {
  99. // var res = address.Substring(2);
  100. // if (res != null && int.TryParse(res, out int tempAddress))
  101. // {
  102. // return ((tempAddress / 2) + 100).ToString();
  103. // }
  104. // }
  105. //}
  106. //return "";
  107. }
  108. /// <summary>
  109. /// PLC 地址
  110. /// </summary>
  111. public string PLCAddress { get; set; }
  112. /// <summary>
  113. /// Modbus TCP 地址
  114. /// </summary>
  115. public string ModbusTcpAddress { get; set; }
  116. /// <summary>
  117. /// 描述
  118. /// </summary>
  119. public string Notes { get; set; }
  120. }
  121. }