终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

135 lines
4.5 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. private 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)
  55. {
  56. if (IsConnected)
  57. {
  58. myPlc?.Write(address, value);
  59. return $"成功,地址:{address},值:{value}";
  60. }
  61. else
  62. {
  63. return $"失败,地址:{address},值:{value},断开连接";
  64. }
  65. }
  66. public TResult ReadClass<TResult>(int db, int startByteAdr = 0) where TResult : class, new()
  67. {
  68. TResult sourceClass = new TResult();
  69. int num = (int)EntityClassResolution.GetClassSize(sourceClass);
  70. if (num <= 0) return sourceClass;
  71. byte[] array = myPlc.ReadBytes(DataType.DataBlock, db, startByteAdr, num);
  72. EntityClassResolution.FromBytes(sourceClass, array);
  73. return sourceClass;
  74. }
  75. public void WriteClass<TWriteModel>(TWriteModel sourceClass, int db, int startAddress = 0) where TWriteModel : class, new()
  76. {
  77. byte[] array = new byte[(int)EntityClassResolution.GetClassSize(sourceClass)];
  78. EntityClassResolution.ToBytes(sourceClass, array);
  79. myPlc.WriteBytes(DataType.DataBlock, db, startAddress, array);
  80. }
  81. //public ushort[] ReadMW(int address, int count)
  82. //{
  83. // if (!IsConnected) return default;
  84. // var res = Read(DataType.Memory, 0, address, VarType.Word, count);
  85. // if (res != null && res is ushort[] ReturnValue) return ReturnValue;
  86. // return default;
  87. //}
  88. //public float[] ReadMD(int address, int count)
  89. //{
  90. // if (!IsConnected) return default;
  91. // var res = Read(DataType.Memory, 0, address, VarType.Real, count);
  92. // if (res != null && res is float[] ReturnValue) return ReturnValue;
  93. // return default;
  94. //}
  95. //public ReadT ReadStruct<ReadT>(int db, int startAddress = 0)
  96. //{
  97. // if (!IsConnected) return default;
  98. // return (ReadT)myPlc.ReadStruct(typeof(ReadT), db, startAddress);
  99. //}
  100. //public void WriteStruct(object structValue, int db, int startAddress = 0)
  101. //{
  102. // myPlc?.WriteStruct(structValue, db, startAddress);
  103. //}
  104. //public int ReadClass(object sourceClass, int db, int startAddress = 0)
  105. //{
  106. // if (!IsConnected) return -1;
  107. // return myPlc.ReadClass(sourceClass, db, startAddress);
  108. //}
  109. //public void WriteClass(object sourceClass, int db, int startAddress = 0)
  110. //{
  111. // myPlc?.WriteClass(sourceClass, db, startAddress);
  112. //}
  113. }
  114. }