選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Siemens.cs 3.4 KiB

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