终端一体化运控平台
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

147 linhas
5.8 KiB

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