Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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