using CommonUtils.Tcp.TcpSocket; using S7.Net; using S7NetModule.Model; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; using System.Windows.Media.Media3D; namespace S7NetModule.S7 { public class S7Plc { [JsonIgnore] public Plc plc; public S7PlcModel model; public bool isState=false; public event OnRemoteCloseHanlder RemoteClose = null; /// /// 打开plc连接 /// public bool Open() { plc = new Plc(model.CpuType, model.IpAddress, model.Port, ( short ) model.Rack, ( short ) model.Slot); // IP, Rack, Slot try { plc.Open(); if ( plc.IsConnected ) { isState = true; Console.WriteLine("Connected to PLC"); return true; } return false; } catch ( Exception ex ) { Console.WriteLine($"Connection failed: {ex.Message}"); return false; } } public void Close() { if ( plc != null ) { if ( plc.IsConnected ) { isState = false; plc.Close(); this.RemoteClose(plc.IP + ":" + plc.Port); } } } // 读取 Bool (DB1.DBX0.0) public bool ReadBool(S7ReadModel model) { bool boolValue=false; try { if ( plc != null && plc.IsConnected ) { if ( model.VarCount > 1 ) { BitArray bitArray= ( BitArray ) plc.Read(DataType.DataBlock, model.Db, model.StartByteAdr, VarType.Bit, model.VarCount, 0); boolValue = bitArray[0]; } else { boolValue=( bool ) plc.Read(DataType.DataBlock, model.Db, model.StartByteAdr, VarType.Bit, model.VarCount, model.BitAdr); } } } catch ( PlcException ex ) { isState = false; } return boolValue; } public short ReadInt(S7ReadModel model) { short shortValue=0; try { if ( plc != null && plc.IsConnected ) // 读取 Int (DB1.DBW2) shortValue = ( short ) plc.Read(DataType.DataBlock, model.Db, model.StartByteAdr, VarType.Int, 1); } catch ( PlcException ex ) { isState = false; } return shortValue; } public int ReadDInt(S7ReadModel model) { int intValue=0; try { if ( plc != null && plc.IsConnected ) // 读取 DInt (DB1.DBD4) intValue = ( int ) plc.Read(DataType.DataBlock, model.Db, model.StartByteAdr, VarType.DInt, 1); } catch ( PlcException ex ) { isState = false; } return intValue; } public float ReadReal(S7ReadModel model) { float floatValue=0; try { if ( plc != null && plc.IsConnected ) // 读取 Real (DB1.DBD8) floatValue = ( float ) plc.Read(DataType.DataBlock, 1, 8, VarType.Real, 1); } catch ( PlcException ex ) { isState = false; } return floatValue; } public string ReadString(S7ReadModel model) { string stringValue=string.Empty; try { if ( plc != null && plc.IsConnected ) // 读取 String (DB1.DBB12,长度20字节) stringValue = ( string ) plc.Read(DataType.DataBlock, model.Db, model.StartByteAdr, VarType.String, model.VarCount); } catch ( PlcException ex ) { isState = false; } return stringValue; } public void WriteBool(S7WriteModel model) { // 写入 Bool (DB1.DBX1.0) plc.Write(DataType.DataBlock, model.Db, model.StartByteAdr, ( bool ) model.Value, model.BitAdr); } public void WriteShort(S7WriteModel model) { // 写入 Int (DB1.DBW2) plc.Write(DataType.DataBlock, model.Db, model.StartByteAdr, Convert.ToInt16(model.Value)); } public void WriteDint(S7WriteModel model) { // 写入 DInt (DB1.DBD4) plc.Write(DataType.DataBlock, model.Db, model.StartByteAdr, Convert.ToInt32(model.Value)); } public void WriteReal(S7WriteModel model) { // 写入 Real (DB1.DBD8) plc.Write(DataType.DataBlock, model.Db, model.StartByteAdr, Convert.ToSingle(model.Value)); } public void WriteString(S7WriteModel model) { // 写入 String (DB1.DBB12) plc.Write(DataType.DataBlock, model.Db, model.StartByteAdr, model.Value.ToString()); } } }