终端一体化运控平台
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

118 строки
3.9 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. return (TResult)myPlc?.Read(address);
  32. }
  33. public bool[] ReadBools(int address, int count)
  34. {
  35. if (!IsConnected) return default;
  36. var res = Read(DataType.Memory, 0, address, VarType.Bit, count);
  37. if (res != null && res is bool[] bools) return bools;
  38. return default;
  39. }
  40. private object Read(DataType dataType, int db, int address, VarType varType, int count)
  41. {
  42. if (!IsConnected) return default;
  43. return myPlc?.Read(dataType, db, address, varType, count);
  44. }
  45. public void Write<TValue>(string address, TValue value)
  46. {
  47. if (IsConnected) myPlc?.Write(address, value);
  48. }
  49. public TResult ReadClass<TResult>(int db, int startByteAdr = 0) where TResult : class, new()
  50. {
  51. TResult sourceClass = new TResult();
  52. int num = (int)EntityClassResolution.GetClassSize(sourceClass);
  53. if (num <= 0) return sourceClass;
  54. byte[] array = myPlc.ReadBytes(DataType.DataBlock, db, startByteAdr, num);
  55. EntityClassResolution.FromBytes(sourceClass, array);
  56. return sourceClass;
  57. }
  58. public void WriteClass<TWriteModel>(TWriteModel sourceClass, int db, int startAddress = 0) where TWriteModel : class, new()
  59. {
  60. byte[] array = new byte[(int)EntityClassResolution.GetClassSize(sourceClass)];
  61. EntityClassResolution.ToBytes(sourceClass, array);
  62. myPlc.WriteBytes(DataType.DataBlock, db, startAddress, array);
  63. }
  64. //public ushort[] ReadMW(int address, int count)
  65. //{
  66. // if (!IsConnected) return default;
  67. // var res = Read(DataType.Memory, 0, address, VarType.Word, count);
  68. // if (res != null && res is ushort[] ReturnValue) return ReturnValue;
  69. // return default;
  70. //}
  71. //public float[] ReadMD(int address, int count)
  72. //{
  73. // if (!IsConnected) return default;
  74. // var res = Read(DataType.Memory, 0, address, VarType.Real, count);
  75. // if (res != null && res is float[] ReturnValue) return ReturnValue;
  76. // return default;
  77. //}
  78. //public ReadT ReadStruct<ReadT>(int db, int startAddress = 0)
  79. //{
  80. // if (!IsConnected) return default;
  81. // return (ReadT)myPlc.ReadStruct(typeof(ReadT), db, startAddress);
  82. //}
  83. //public void WriteStruct(object structValue, int db, int startAddress = 0)
  84. //{
  85. // myPlc?.WriteStruct(structValue, db, startAddress);
  86. //}
  87. //public int ReadClass(object sourceClass, int db, int startAddress = 0)
  88. //{
  89. // if (!IsConnected) return -1;
  90. // return myPlc.ReadClass(sourceClass, db, startAddress);
  91. //}
  92. //public void WriteClass(object sourceClass, int db, int startAddress = 0)
  93. //{
  94. // myPlc?.WriteClass(sourceClass, db, startAddress);
  95. //}
  96. }
  97. }