终端一体化运控平台
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

153 Zeilen
5.2 KiB

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