终端一体化运控平台
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

193 行
6.6 KiB

  1. using S7.Net;
  2. using System.Net;
  3. using System.Text;
  4. using BPASmartClient.S7Net;
  5. namespace BPASmartClient.S7Net
  6. {
  7. public class SiemensHelper
  8. {
  9. Plc myPlc;
  10. public bool IsConnected => myPlc is null ? false : myPlc.IsConnected;
  11. /// <summary>
  12. /// 打开连接
  13. /// </summary>
  14. /// <param name="cpuType">PLC CPU 类型</param>
  15. /// <param name="ip">plc ip 地址</param>
  16. /// <param name="port">plc 端口号</param>
  17. /// <param name="rack">PLC 机架号</param>
  18. /// <param name="solt"> PLC 插槽号</param>
  19. public void Connect(CpuType cpuType, string ip, int port = 102, short rack = 0, short solt = 0)
  20. {
  21. myPlc = new Plc(cpuType, ip, port, rack, solt);
  22. myPlc.Open();
  23. }
  24. /// <summary>
  25. /// 断开和PLC的连接
  26. /// </summary>
  27. public void Disconnect()
  28. {
  29. myPlc?.Close();
  30. }
  31. public TResult Read<TResult>(string address)
  32. {
  33. if (!IsConnected) return default;
  34. var value = myPlc?.Read(address);
  35. if (typeof(TResult).Name == "Single")
  36. {
  37. var obj = ((uint)value).ConvertToFloat();
  38. return (TResult)Convert.ChangeType(obj, typeof(TResult));
  39. }
  40. else
  41. {
  42. return (TResult)Convert.ChangeType(value, typeof(TResult));
  43. }
  44. }
  45. public bool[] ReadBools(int address, int count)
  46. {
  47. if (!IsConnected) return default;
  48. var res = Read(DataType.Memory, 0, address, VarType.Bit, count);
  49. if (res != null && res is bool[] bools) return bools;
  50. return default;
  51. }
  52. public object Read(DataType dataType, int db, int address, VarType varType, int count)
  53. {
  54. if (!IsConnected) return default;
  55. return myPlc?.Read(dataType, db, address, varType, count);
  56. }
  57. public void WriteString(DataType dataType, int db, int startByteAdr, string value)
  58. {
  59. if (!IsConnected) return;
  60. var res = value.ToArrays();
  61. if (res != null)
  62. {
  63. List<byte> bytes = new List<byte>();
  64. bytes.Add((byte)res.Length);
  65. bytes.Add((byte)res.Length);
  66. bytes.AddRange(res);
  67. myPlc?.WriteBytes(dataType, db, startByteAdr, bytes.ToArray());
  68. }
  69. }
  70. public string Write<TValue>(string address, TValue value, int Retries = 1)
  71. {
  72. if (IsConnected)
  73. {
  74. int count = 0;
  75. if (Retries == 1 || Retries == 0)
  76. {
  77. myPlc?.Write(address, value);
  78. return $"成功,地址:{address},值:{value}";
  79. }
  80. else
  81. {
  82. while (count < Retries)
  83. {
  84. count++;
  85. myPlc?.Write(address, value);
  86. var res = myPlc?.Read(address);
  87. if (res != null && res.ToString() == value.ToString())
  88. {
  89. break;
  90. }
  91. }
  92. return $"成功,发送了{count}次,地址:{address},值:{value}";
  93. }
  94. }
  95. else
  96. {
  97. return $"失败,地址:{address},值:{value},断开连接";
  98. }
  99. }
  100. /// <summary>
  101. ///
  102. /// </summary>
  103. /// <param name="db">DB号</param>
  104. /// <param name="txt">字符串</param>
  105. /// <param name="startAddress">字节偏移地址</param>
  106. public void WriteString(int db, string txt, int startAddress = 0)
  107. {
  108. var temp = Encoding.ASCII.GetBytes(txt);
  109. var bytes = S7.Net.Types.S7String.ToByteArray(txt, temp.Length);
  110. myPlc.WriteBytes(DataType.DataBlock, db, startAddress, bytes);
  111. }
  112. public void WriteInt16(int db, short txt, int startAddress = 0)
  113. {
  114. var bytes = BitConverter.GetBytes(txt);
  115. myPlc.WriteBytes(DataType.DataBlock, db, startAddress, bytes);
  116. }
  117. public TResult ReadClass<TResult>(int db, int startByteAdr = 0) where TResult : class, new()
  118. {
  119. TResult sourceClass = new TResult();
  120. int num = (int)EntityClassResolution.GetClassSize(sourceClass);
  121. if (num <= 0) return sourceClass;
  122. if (IsConnected)
  123. {
  124. byte[] array = myPlc.ReadBytes(DataType.DataBlock, db, startByteAdr, num);
  125. EntityClassResolution.FromBytes(sourceClass, array);
  126. }
  127. return sourceClass;
  128. }
  129. public void WriteClass<TWriteModel>(TWriteModel sourceClass, int db, int startAddress = 0) where TWriteModel : class, new()
  130. {
  131. if (IsConnected)
  132. {
  133. byte[] array = new byte[(int)EntityClassResolution.GetClassSize(sourceClass)];
  134. EntityClassResolution.ToBytes(sourceClass, array);
  135. myPlc.WriteBytes(DataType.DataBlock, db, startAddress, array);
  136. }
  137. }
  138. //public ushort[] ReadMW(int address, int count)
  139. //{
  140. // if (!IsConnected) return default;
  141. // var res = Read(DataType.Memory, 0, address, VarType.Word, count);
  142. // if (res != null && res is ushort[] ReturnValue) return ReturnValue;
  143. // return default;
  144. //}
  145. //public float[] ReadMD(int address, int count)
  146. //{
  147. // if (!IsConnected) return default;
  148. // var res = Read(DataType.Memory, 0, address, VarType.Real, count);
  149. // if (res != null && res is float[] ReturnValue) return ReturnValue;
  150. // return default;
  151. //}
  152. //public ReadT ReadStruct<ReadT>(int db, int startAddress = 0)
  153. //{
  154. // if (!IsConnected) return default;
  155. // return (ReadT)myPlc.ReadStruct(typeof(ReadT), db, startAddress);
  156. //}
  157. //public void WriteStruct(object structValue, int db, int startAddress = 0)
  158. //{
  159. // myPlc?.WriteStruct(structValue, db, startAddress);
  160. //}
  161. //public int ReadClass(object sourceClass, int db, int startAddress = 0)
  162. //{
  163. // if (!IsConnected) return -1;
  164. // return myPlc.ReadClass(sourceClass, db, startAddress);
  165. //}
  166. //public void WriteClass(object sourceClass, int db, int startAddress = 0)
  167. //{
  168. // myPlc?.WriteClass(sourceClass, db, startAddress);
  169. //}
  170. }
  171. }