终端一体化运控平台
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

155 líneas
6.0 KiB

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