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

SiemensHelper.cs 5.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. public TResult ReadClass<TResult>(int db, int startByteAdr = 0) where TResult : class, new()
  101. {
  102. TResult sourceClass = new TResult();
  103. int num = (int)EntityClassResolution.GetClassSize(sourceClass);
  104. if (num <= 0) return sourceClass;
  105. if (IsConnected)
  106. {
  107. byte[] array = myPlc.ReadBytes(DataType.DataBlock, db, startByteAdr, num);
  108. EntityClassResolution.FromBytes(sourceClass, array);
  109. }
  110. return sourceClass;
  111. }
  112. public void WriteClass<TWriteModel>(TWriteModel sourceClass, int db, int startAddress = 0) where TWriteModel : class, new()
  113. {
  114. if (IsConnected)
  115. {
  116. byte[] array = new byte[(int)EntityClassResolution.GetClassSize(sourceClass)];
  117. EntityClassResolution.ToBytes(sourceClass, array);
  118. myPlc.WriteBytes(DataType.DataBlock, db, startAddress, array);
  119. }
  120. }
  121. //public ushort[] ReadMW(int address, int count)
  122. //{
  123. // if (!IsConnected) return default;
  124. // var res = Read(DataType.Memory, 0, address, VarType.Word, count);
  125. // if (res != null && res is ushort[] ReturnValue) return ReturnValue;
  126. // return default;
  127. //}
  128. //public float[] ReadMD(int address, int count)
  129. //{
  130. // if (!IsConnected) return default;
  131. // var res = Read(DataType.Memory, 0, address, VarType.Real, count);
  132. // if (res != null && res is float[] ReturnValue) return ReturnValue;
  133. // return default;
  134. //}
  135. //public ReadT ReadStruct<ReadT>(int db, int startAddress = 0)
  136. //{
  137. // if (!IsConnected) return default;
  138. // return (ReadT)myPlc.ReadStruct(typeof(ReadT), db, startAddress);
  139. //}
  140. //public void WriteStruct(object structValue, int db, int startAddress = 0)
  141. //{
  142. // myPlc?.WriteStruct(structValue, db, startAddress);
  143. //}
  144. //public int ReadClass(object sourceClass, int db, int startAddress = 0)
  145. //{
  146. // if (!IsConnected) return -1;
  147. // return myPlc.ReadClass(sourceClass, db, startAddress);
  148. //}
  149. //public void WriteClass(object sourceClass, int db, int startAddress = 0)
  150. //{
  151. // myPlc?.WriteClass(sourceClass, db, startAddress);
  152. //}
  153. }
  154. }