终端一体化运控平台
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.

SiemensHelper.cs 4.3 KiB

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