S7Plc.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using CommonUtils.Tcp.TcpSocket;
  2. using S7.Net;
  3. using S7NetModule.Model;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using Newtonsoft.Json;
  12. using System.Windows.Media.Media3D;
  13. namespace S7NetModule.S7
  14. {
  15. public class S7Plc
  16. {
  17. [JsonIgnore]
  18. public Plc plc;
  19. public S7PlcModel model;
  20. public bool isState=false;
  21. public event OnRemoteCloseHanlder RemoteClose = null;
  22. /// <summary>
  23. /// 打开plc连接
  24. /// </summary>
  25. public bool Open()
  26. {
  27. plc = new Plc(model.CpuType, model.IpAddress, model.Port, ( short ) model.Rack, ( short ) model.Slot); // IP, Rack, Slot
  28. try
  29. {
  30. plc.Open();
  31. if ( plc.IsConnected )
  32. {
  33. isState = true;
  34. Console.WriteLine("Connected to PLC");
  35. return true;
  36. }
  37. return false;
  38. }
  39. catch ( Exception ex )
  40. {
  41. Console.WriteLine($"Connection failed: {ex.Message}");
  42. return false;
  43. }
  44. }
  45. public void Close()
  46. {
  47. if ( plc != null )
  48. {
  49. if ( plc.IsConnected )
  50. {
  51. isState = false;
  52. plc.Close();
  53. this.RemoteClose(plc.IP + ":" + plc.Port);
  54. }
  55. }
  56. }
  57. // 读取 Bool (DB1.DBX0.0)
  58. public bool ReadBool(S7ReadModel model)
  59. {
  60. bool boolValue=false;
  61. try
  62. {
  63. if ( plc != null && plc.IsConnected )
  64. {
  65. if ( model.VarCount > 1 )
  66. {
  67. BitArray bitArray= ( BitArray ) plc.Read(DataType.DataBlock, model.Db, model.StartByteAdr, VarType.Bit, model.VarCount, 0);
  68. boolValue = bitArray[0];
  69. }
  70. else
  71. {
  72. boolValue=( bool ) plc.Read(DataType.DataBlock, model.Db, model.StartByteAdr, VarType.Bit, model.VarCount, model.BitAdr);
  73. }
  74. }
  75. }
  76. catch ( PlcException ex )
  77. {
  78. isState = false;
  79. }
  80. return boolValue;
  81. }
  82. public short ReadInt(S7ReadModel model)
  83. {
  84. short shortValue=0;
  85. try
  86. {
  87. if ( plc != null && plc.IsConnected )
  88. // 读取 Int (DB1.DBW2)
  89. shortValue = ( short ) plc.Read(DataType.DataBlock, model.Db, model.StartByteAdr, VarType.Int, 1);
  90. }
  91. catch ( PlcException ex )
  92. {
  93. isState = false;
  94. }
  95. return shortValue;
  96. }
  97. public int ReadDInt(S7ReadModel model)
  98. {
  99. int intValue=0;
  100. try
  101. {
  102. if ( plc != null && plc.IsConnected )
  103. // 读取 DInt (DB1.DBD4)
  104. intValue = ( int ) plc.Read(DataType.DataBlock, model.Db, model.StartByteAdr, VarType.DInt, 1);
  105. }
  106. catch ( PlcException ex )
  107. {
  108. isState = false;
  109. }
  110. return intValue;
  111. }
  112. public float ReadReal(S7ReadModel model)
  113. {
  114. float floatValue=0;
  115. try
  116. {
  117. if ( plc != null && plc.IsConnected )
  118. // 读取 Real (DB1.DBD8)
  119. floatValue = ( float ) plc.Read(DataType.DataBlock, 1, 8, VarType.Real, 1);
  120. }
  121. catch ( PlcException ex )
  122. {
  123. isState = false;
  124. }
  125. return floatValue;
  126. }
  127. public string ReadString(S7ReadModel model)
  128. {
  129. string stringValue=string.Empty;
  130. try
  131. {
  132. if ( plc != null && plc.IsConnected )
  133. // 读取 String (DB1.DBB12,长度20字节)
  134. stringValue = ( string ) plc.Read(DataType.DataBlock, model.Db, model.StartByteAdr, VarType.String, model.VarCount);
  135. }
  136. catch ( PlcException ex )
  137. {
  138. isState = false;
  139. }
  140. return stringValue;
  141. }
  142. public void WriteBool(S7WriteModel model)
  143. {
  144. // 写入 Bool (DB1.DBX1.0)
  145. plc.Write(DataType.DataBlock, model.Db, model.StartByteAdr, ( bool ) model.Value, model.BitAdr);
  146. }
  147. public void WriteShort(S7WriteModel model)
  148. {
  149. // 写入 Int (DB1.DBW2)
  150. plc.Write(DataType.DataBlock, model.Db, model.StartByteAdr, Convert.ToInt16(model.Value));
  151. }
  152. public void WriteDint(S7WriteModel model)
  153. {
  154. // 写入 DInt (DB1.DBD4)
  155. plc.Write(DataType.DataBlock, model.Db, model.StartByteAdr, Convert.ToInt32(model.Value));
  156. }
  157. public void WriteReal(S7WriteModel model)
  158. {
  159. // 写入 Real (DB1.DBD8)
  160. plc.Write(DataType.DataBlock, model.Db, model.StartByteAdr, Convert.ToSingle(model.Value));
  161. }
  162. public void WriteString(S7WriteModel model)
  163. {
  164. // 写入 String (DB1.DBB12)
  165. plc.Write(DataType.DataBlock, model.Db, model.StartByteAdr, model.Value.ToString());
  166. }
  167. }
  168. }