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.

171 lines
4.9 KiB

  1. using S7.Net;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace HKControl
  9. {
  10. internal class Siemens : CommunicationBase
  11. {
  12. Plc myPlc;
  13. public bool IsConnected => myPlc is null ? false : myPlc.IsConnected;
  14. private CpuType myCpuType;
  15. private string MyIp;
  16. private int Myport;
  17. /// <summary>
  18. /// 打开连接
  19. /// </summary>
  20. /// <param name="cpuType">PLC CPU 类型</param>
  21. /// <param name="ip">plc ip 地址</param>
  22. /// <param name="port">plc 端口号</param>
  23. /// <param name="rack">PLC 机架号</param>
  24. /// <param name="solt"> PLC 插槽号</param>
  25. public void Connect(CpuType cpuType, string ip, int port = 102, short rack = 0, short solt = 0)
  26. {
  27. try
  28. {
  29. myCpuType = cpuType;
  30. MyIp = ip;
  31. Myport = port;
  32. myPlc = new Plc(cpuType, ip, port, rack, solt);
  33. while (!myPlc.IsConnected)
  34. {
  35. myPlc.Open();
  36. if (!myPlc.IsConnected) Thread.Sleep(3000);
  37. }
  38. if (myPlc.IsConnected)
  39. {
  40. ConnectOk?.Invoke();
  41. HKLog.HKLogImport.WriteInfo($"设备{ip}连接成功");
  42. //Debug.WriteLine($"设备{ip}连接成功");
  43. }
  44. }
  45. catch (Exception ex)
  46. {
  47. HKLog.HKLogImport.WriteInfo($"设备连接出错:{ip}");
  48. Thread.Sleep(3000);
  49. Connect(cpuType,ip,port);
  50. //Debug.WriteLine(ex.ToString());
  51. }
  52. }
  53. private void ExceptionDis()
  54. {
  55. myPlc?.Close();
  56. myPlc = null;
  57. Connect(myCpuType, MyIp, Myport);
  58. }
  59. /// <summary>
  60. /// 断开和PLC的连接
  61. /// </summary>
  62. public void Disconnect()
  63. {
  64. myPlc?.Close();
  65. }
  66. public TResult Read<TResult>(string address)
  67. {
  68. try
  69. {
  70. if (!IsConnected) return default;
  71. return (TResult)myPlc?.Read(address);
  72. }
  73. catch (Exception ex)
  74. {
  75. ExceptionDis();
  76. return default;
  77. }
  78. }
  79. public bool[] ReadBools(int address, int count)
  80. {
  81. if (!IsConnected) return default;
  82. var res = Read(DataType.Memory, 0, address, VarType.Bit, count);
  83. if (res != null && res is bool[] bools) return bools;
  84. return default;
  85. }
  86. public ushort[] ReadMW(int address, int count)
  87. {
  88. if (!IsConnected) return default;
  89. var res = Read(DataType.Memory, 0, address, VarType.Word, count);
  90. if (res != null && res is ushort[] ReturnValue) return ReturnValue;
  91. return default;
  92. }
  93. public float[] ReadMD(int address, int count)
  94. {
  95. if (!IsConnected) return default;
  96. var res = Read(DataType.Memory, 0, address, VarType.Real, count);
  97. if (res != null && res is float[] ReturnValue) return ReturnValue;
  98. return default;
  99. }
  100. private object Read(DataType dataType, int db, int address, VarType varType, int count)
  101. {
  102. try
  103. {
  104. if (!IsConnected) return default;
  105. return myPlc?.Read(dataType, db, address, varType, count);
  106. }
  107. catch (Exception ex)
  108. {
  109. ExceptionDis();
  110. return default;
  111. }
  112. }
  113. public void Write<TValue>(string address, TValue value)
  114. {
  115. try
  116. {
  117. myPlc?.Write(address, value);
  118. }
  119. catch (Exception ex)
  120. {
  121. ExceptionDis();
  122. }
  123. }
  124. public ReadT ReadStruct<ReadT>(int db, int startAddress = 0)
  125. {
  126. if (!IsConnected) return default;
  127. return (ReadT)myPlc.ReadStruct(typeof(ReadT), db, startAddress);
  128. }
  129. public void WriteStruct(object structValue, int db, int startAddress = 0)
  130. {
  131. myPlc?.WriteStruct(structValue, db, startAddress);
  132. }
  133. public int ReadClass(object sourceClass, int db, int startAddress = 0)
  134. {
  135. if (!IsConnected) return -1;
  136. return myPlc.ReadClass(sourceClass, db, startAddress);
  137. }
  138. public TResult ReadClass<TResult>(int db, int startAddress = 0) where TResult : class
  139. {
  140. if (!IsConnected) return default;
  141. return myPlc.ReadClass<TResult>(db, startAddress);
  142. }
  143. public void WriteClass(object sourceClass, int db, int startAddress = 0)
  144. {
  145. myPlc?.WriteClass(sourceClass, db, startAddress);
  146. }
  147. }
  148. }