Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

177 строки
5.1 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) return default;
  77. return (TResult)myPlc?.Read(address);
  78. }
  79. catch (Exception ex)
  80. {
  81. ExceptionDis();
  82. return default;
  83. }
  84. }
  85. public bool[] ReadBools(int address, int count)
  86. {
  87. if (!IsConnected) return default;
  88. var res = Read(DataType.Memory, 0, address, VarType.Bit, count);
  89. if (res != null && res is bool[] bools) return bools;
  90. return default;
  91. }
  92. public ushort[] ReadMW(int address, int count)
  93. {
  94. if (!IsConnected) return default;
  95. var res = Read(DataType.Memory, 0, address, VarType.Word, count);
  96. if (res != null && res is ushort[] ReturnValue) return ReturnValue;
  97. return default;
  98. }
  99. public float[] ReadMD(int address, int count)
  100. {
  101. if (!IsConnected) return default;
  102. var res = Read(DataType.Memory, 0, address, VarType.Real, count);
  103. if (res != null && res is float[] ReturnValue) return ReturnValue;
  104. return default;
  105. }
  106. private object Read(DataType dataType, int db, int address, VarType varType, int count)
  107. {
  108. try
  109. {
  110. if (!IsConnected) return default;
  111. return myPlc?.Read(dataType, db, address, varType, count);
  112. }
  113. catch (Exception ex)
  114. {
  115. ExceptionDis();
  116. return default;
  117. }
  118. }
  119. public void Write<TValue>(string address, TValue value)
  120. {
  121. try
  122. {
  123. myPlc?.Write(address, value);
  124. }
  125. catch (Exception ex)
  126. {
  127. ExceptionDis();
  128. }
  129. }
  130. public ReadT ReadStruct<ReadT>(int db, int startAddress = 0)
  131. {
  132. if (!IsConnected) return default;
  133. return (ReadT)myPlc.ReadStruct(typeof(ReadT), db, startAddress);
  134. }
  135. public void WriteStruct(object structValue, int db, int startAddress = 0)
  136. {
  137. myPlc?.WriteStruct(structValue, db, startAddress);
  138. }
  139. public int ReadClass(object sourceClass, int db, int startAddress = 0)
  140. {
  141. if (!IsConnected) return -1;
  142. return myPlc.ReadClass(sourceClass, db, startAddress);
  143. }
  144. public TResult ReadClass<TResult>(int db, int startAddress = 0) where TResult : class
  145. {
  146. if (!IsConnected) return default;
  147. return myPlc.ReadClass<TResult>(db, startAddress);
  148. }
  149. public void WriteClass(object sourceClass, int db, int startAddress = 0)
  150. {
  151. myPlc?.WriteClass(sourceClass, db, startAddress);
  152. }
  153. }
  154. }