You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.6 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace BPA.Model.Attributes
  7. {
  8. public class VariableMonitorAttribute : Attribute
  9. {
  10. /// <summary>
  11. /// 变量描述
  12. /// </summary>
  13. /// <param name="Notes">描述</param>
  14. /// <param name="PLCAddress">PLC 地址</param>
  15. /// <param name="ModbusTcpAddress">Modbus TCP 地址</param>
  16. public VariableMonitorAttribute(string Notes, string PLCAddress = "", string ModbusTcpAddress = "")
  17. {
  18. this.PLCAddress = PLCAddress;
  19. this.ModbusTcpAddress = GetModbusTcpAdd(PLCAddress);// ModbusTcpAddress;
  20. this.Notes = Notes;
  21. }
  22. private string GetModbusTcpAdd(string address)
  23. {
  24. if (address == null)
  25. 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)
  33. return (int.Parse(res) + 4096).ToString();
  34. }
  35. else if (address.ToUpper().Contains("M") && address.Length >= 4)
  36. {
  37. var res = address.Substring(1).Split('.');
  38. if (res != null && res.Length == 2)
  39. {
  40. if (int.TryParse(res[0], out int firstAddress) && int.TryParse(res[1], out int ExitAddress))
  41. {
  42. if (ExitAddress >= 0 && ExitAddress <= 7)
  43. {
  44. return ((firstAddress * 8) + 320 + ExitAddress).ToString();
  45. }
  46. }
  47. }
  48. }
  49. else if (address.ToUpper().Contains("GI") && address.Length >= 3)
  50. {
  51. var res = address.Remove(0, 2);
  52. if (res != null && res.Length > 0)
  53. return res;
  54. }
  55. else if (address.ToUpper().Contains("LB") && address.Length >= 3)
  56. {
  57. var res = address.Substring(2);
  58. if (res != null && res.Length > 0)
  59. {
  60. if (int.TryParse(res, out int firstAddress))
  61. return firstAddress.ToString();
  62. }
  63. }
  64. else if ((address.ToUpper().Contains("VW") || address.ToUpper().Contains("VD")) && address.Length >= 3)
  65. {
  66. var res = address.Substring(2);
  67. if (res != null && int.TryParse(res, out int tempAddress))
  68. {
  69. return ((tempAddress / 2) + 100).ToString();
  70. }
  71. }
  72. else if (address.ToUpper().Contains("LW") && address.Length >= 3)
  73. {
  74. var res = address.Substring(2);
  75. if (res != null && int.TryParse(res, out int LwAddress))
  76. {
  77. return LwAddress.ToString();
  78. }
  79. }
  80. }
  81. return "";
  82. }
  83. /// <summary>
  84. /// PLC 地址
  85. /// </summary>
  86. public string PLCAddress { get; set; }
  87. /// <summary>
  88. /// Modbus TCP 地址
  89. /// </summary>
  90. public string ModbusTcpAddress { get; set; }
  91. /// <summary>
  92. /// 描述
  93. /// </summary>
  94. public string Notes { get; set; }
  95. }
  96. }