Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

122 рядки
3.8 KiB

  1. using S7.Net;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace HKControl
  9. {
  10. internal class Siemens : CommunicationBase
  11. {
  12. Plc myPlc;
  13. public bool IsConnected => myPlc is null ? false : myPlc.IsConnected;
  14. /// <summary>
  15. /// 打开连接
  16. /// </summary>
  17. /// <param name="cpuType">PLC CPU 类型</param>
  18. /// <param name="ip">plc ip 地址</param>
  19. /// <param name="port">plc 端口号</param>
  20. /// <param name="rack">PLC 机架号</param>
  21. /// <param name="solt"> PLC 插槽号</param>
  22. public void Connect(CpuType cpuType, string ip, int port = 102, short rack = 0, short solt = 0)
  23. {
  24. try
  25. {
  26. myPlc = new Plc(cpuType, ip, port, rack, solt);
  27. myPlc.Open();
  28. if (myPlc.IsConnected)
  29. {
  30. ConnectOk?.Invoke();
  31. Debug.WriteLine($"设备{ip}连接成功");
  32. }
  33. }
  34. catch (Exception ex)
  35. {
  36. Debug.WriteLine($"设备连接出错:{ip}");
  37. //Debug.WriteLine(ex.ToString());
  38. }
  39. }
  40. /// <summary>
  41. /// 断开和PLC的连接
  42. /// </summary>
  43. public void Disconnect()
  44. {
  45. myPlc?.Close();
  46. }
  47. public TResult Read<TResult>(string address)
  48. {
  49. if (!IsConnected) return default;
  50. return (TResult)myPlc?.Read(address);
  51. }
  52. public bool[] ReadBools(int address, int count)
  53. {
  54. if (!IsConnected) return default;
  55. var res = Read(DataType.Memory, 0, address, VarType.Bit, count);
  56. if (res != null && res is bool[] bools) return bools;
  57. return default;
  58. }
  59. public ushort[] ReadMW(int address, int count)
  60. {
  61. if (!IsConnected) return default;
  62. var res = Read(DataType.Memory, 0, address, VarType.Word, count);
  63. if (res != null && res is ushort[] ReturnValue) return ReturnValue;
  64. return default;
  65. }
  66. public float[] ReadMD(int address, int count)
  67. {
  68. if (!IsConnected) return default;
  69. var res = Read(DataType.Memory, 0, address, VarType.Real, count);
  70. if (res != null && res is float[] ReturnValue) return ReturnValue;
  71. return default;
  72. }
  73. private object Read(DataType dataType, int db, int address, VarType varType, int count)
  74. {
  75. if (!IsConnected) return default;
  76. return myPlc?.Read(dataType, db, address, varType, count);
  77. }
  78. public void Write<TValue>(string address, TValue value)
  79. {
  80. myPlc?.Write(address, value);
  81. }
  82. public ReadT ReadStruct<ReadT>(int db, int startAddress = 0)
  83. {
  84. if (!IsConnected) return default;
  85. return (ReadT)myPlc.ReadStruct(typeof(ReadT), db, startAddress);
  86. }
  87. public void WriteStruct(object structValue, int db, int startAddress = 0)
  88. {
  89. myPlc?.WriteStruct(structValue, db, startAddress);
  90. }
  91. public int ReadClass(object sourceClass, int db, int startAddress = 0)
  92. {
  93. if (!IsConnected) return -1;
  94. return myPlc.ReadClass(sourceClass, db, startAddress);
  95. }
  96. public TResult ReadClass<TResult>(int db, int startAddress = 0) where TResult : class
  97. {
  98. if (!IsConnected) return default;
  99. return myPlc.ReadClass<TResult>(db, startAddress);
  100. }
  101. public void WriteClass(object sourceClass, int db, int startAddress = 0)
  102. {
  103. myPlc?.WriteClass(sourceClass, db, startAddress);
  104. }
  105. }
  106. }