终端一体化运控平台
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

79 Zeilen
2.9 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace BPASmartClient.MorkCL
  8. {
  9. /// <summary>
  10. /// 地址转换类
  11. /// </summary>
  12. internal static class ExtensionMethod
  13. {
  14. /// <summary>
  15. /// Modbus 地址转换
  16. /// </summary>
  17. /// <param name="address"></param>
  18. /// <returns></returns>
  19. public static string ToModbusAdd(this string address)
  20. {
  21. if (address == null) return "";
  22. if (address.Length > 0)
  23. {
  24. address = address.Trim();
  25. if (address.ToUpper().Contains("GM") && address.Length >= 3)
  26. {
  27. var res = address.Remove(0, 2);
  28. if (res != null && res.Length > 0) return (int.Parse(res) + 4096).ToString();
  29. }
  30. else if (address.ToUpper().Contains("M") && address.Length >= 4)
  31. {
  32. var res = address.Substring(1).Split('.');
  33. if (res != null && res.Length == 2)
  34. {
  35. if (int.TryParse(res[0], out int firstAddress) && int.TryParse(res[1], out int ExitAddress))
  36. {
  37. if (ExitAddress >= 0 && ExitAddress <= 7)
  38. {
  39. return ((firstAddress * 8) + 320 + ExitAddress).ToString();
  40. }
  41. }
  42. }
  43. }
  44. else if (address.ToUpper().Contains("GI") && address.Length >= 3)
  45. {
  46. var res = address.Remove(0, 2);
  47. if (res != null && res.Length > 0) return res;
  48. }
  49. else if (address.ToUpper().Contains("LB") && address.Length >= 3)
  50. {
  51. var res = address.Substring(2);
  52. if (res != null && res.Length > 0)
  53. {
  54. if (int.TryParse(res, out int firstAddress)) return firstAddress.ToString();
  55. }
  56. }
  57. else if ((address.ToUpper().Contains("VW") || address.ToUpper().Contains("VD")) && address.Length >= 3)
  58. {
  59. var res = address.Substring(2);
  60. if (res != null && int.TryParse(res, out int tempAddress))
  61. {
  62. return ((tempAddress / 2) + 100).ToString();
  63. }
  64. }
  65. else if (address.ToUpper().Contains("LW") && address.Length >= 3)
  66. {
  67. var res = address.Substring(2);
  68. if (res != null && int.TryParse(res, out int LwAddress))
  69. {
  70. return LwAddress.ToString();
  71. }
  72. }
  73. }
  74. return "";
  75. }
  76. }
  77. }