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.

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