终端一体化运控平台
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.
 
 
 

143 lines
5.7 KiB

  1. using System;
  2. namespace BPASmartClient.DosingSystem.Model
  3. {
  4. public static class Expand
  5. {
  6. /// <summary>
  7. /// 获取 M 区 和 VW 区的Modbus的地址
  8. /// </summary>
  9. /// <param name="address"></param>
  10. /// <returns></returns>
  11. public static string ToAdd(this string address)
  12. {
  13. if (address == null) return "";
  14. if (address.Length > 0)
  15. {
  16. address = address.Trim();
  17. if (address.ToUpper().Contains("GM") && address.Length >= 3)
  18. {
  19. var res = address.Remove(0, 2);
  20. if (res != null && res.Length > 0)
  21. {
  22. return (int.Parse(res) + 4096).ToString();
  23. }
  24. }
  25. else if (address.ToUpper().Contains("M") && address.Length >= 4)
  26. {
  27. var res = address.Substring(1).Split('.');
  28. if (res != null && res.Length == 2)
  29. {
  30. if (int.TryParse(res[0], out int firstAddress) && int.TryParse(res[1], out int ExitAddress))
  31. {
  32. if (ExitAddress >= 0 && ExitAddress <= 7)
  33. {
  34. return ((firstAddress * 8) + 320 + ExitAddress).ToString();
  35. }
  36. }
  37. }
  38. }
  39. else if (address.ToUpper().Contains("I") && address.Length >= 2)
  40. {
  41. var res = address.Substring(1).Split('.');
  42. if (res != null && res.Length == 2)
  43. {
  44. if (int.TryParse(res[0], out int firstAddress) && int.TryParse(res[1], out int ExitAddress))
  45. {
  46. if (ExitAddress >= 0 && ExitAddress <= 7)
  47. {
  48. return ((firstAddress * 8) + ExitAddress).ToString();
  49. }
  50. }
  51. }
  52. }
  53. else if (address.ToUpper().Contains("GI") && address.Length >= 3)
  54. {
  55. var res = address.Remove(0, 2);
  56. if (res != null && res.Length > 0)
  57. {
  58. return (int.Parse(res)).ToString();
  59. }
  60. }
  61. else if (address.ToUpper().Contains("LB") && address.Length >= 3)
  62. {
  63. var res = address.Substring(2);
  64. if (res != null && res.Length > 0)
  65. {
  66. if (int.TryParse(res, out int firstAddress))
  67. {
  68. return firstAddress.ToString();
  69. }
  70. }
  71. }
  72. else if ((address.ToUpper().Contains("VW") || address.ToUpper().Contains("VD")) && address.Length >= 3)
  73. {
  74. var res = address.Substring(2);
  75. if (res != null && int.TryParse(res, out int tempAddress))
  76. {
  77. return ((tempAddress / 2) + 100).ToString();
  78. }
  79. }
  80. else if (address.ToUpper().Contains("LW") && address.Length >= 3)
  81. {
  82. var res = address.Substring(2);
  83. if (res != null && int.TryParse(res, out int LwAddress))
  84. {
  85. return LwAddress.ToString();
  86. }
  87. }
  88. else if (address.ToUpper().Contains("D") && address.Length == 5)
  89. {
  90. try
  91. {
  92. //D1001
  93. string head = (System.Convert.ToInt32(address.Substring(1, 1))).ToString();
  94. int num = System.Convert.ToInt32(address.Substring(2, 3));
  95. int len = num.ToString().Length;
  96. string tail = string.Empty;
  97. switch (len)
  98. {
  99. case 1:
  100. if ((System.Convert.ToInt32(address.Substring(4, 1))).ToString().Length > 1)
  101. {
  102. tail = "0" + (System.Convert.ToInt32(address.Substring(4, 1))).ToString();
  103. }
  104. else
  105. {
  106. tail = "00" + (System.Convert.ToInt32(address.Substring(4, 1))).ToString();
  107. }
  108. break;
  109. case 2:
  110. if ((System.Convert.ToInt32(address.Substring(3, 2))).ToString().Length > 2)
  111. {
  112. tail = (System.Convert.ToInt32(address.Substring(3, 2))).ToString();
  113. }
  114. else
  115. {
  116. tail = "0" + (System.Convert.ToInt32(address.Substring(3, 2))).ToString();
  117. }
  118. break;
  119. case 3:
  120. tail = (System.Convert.ToInt32(address.Substring(2, 3))).ToString();
  121. break;
  122. }
  123. address = head + tail;
  124. return address;
  125. }
  126. catch (Exception)
  127. {
  128. //打印日志
  129. }
  130. }
  131. }
  132. return "";
  133. }
  134. }
  135. }