浏览代码

修正S7库避免多次打开PLC连接

xuwei 2 年之前
父节点
当前提交
804939158d
共有 3 个文件被更改,包括 166 次插入3 次删除
  1. 1 0
      DK.XuWei.WebMes/DK.XuWei.WebMes.csproj
  2. 3 3
      DK.XuWei.WebMes/Properties/AssemblyInfo.cs
  3. 162 0
      DK.XuWei.WebMes/S7.cs

+ 1 - 0
DK.XuWei.WebMes/DK.XuWei.WebMes.csproj

@@ -88,6 +88,7 @@
   <ItemGroup>
     <Compile Include="Easyui.cs" />
     <Compile Include="Import.cs" />
+    <Compile Include="S7.cs" />
     <Compile Include="SiemensS7.cs" />
     <Compile Include="SoapClient.cs" />
     <Compile Include="XmlClient.cs" />

+ 3 - 3
DK.XuWei.WebMes/Properties/AssemblyInfo.cs

@@ -10,7 +10,7 @@ using System.Runtime.InteropServices;
 [assembly: AssemblyConfiguration("")]
 [assembly: AssemblyCompany("沈阳东科云信软件有限公司")]
 [assembly: AssemblyProduct("DK.XuWei.WebMes")]
-[assembly: AssemblyCopyright("Copyright © 2023")]
+[assembly: AssemblyCopyright("Copyright © 2024")]
 [assembly: AssemblyTrademark("东科软件")]
 [assembly: AssemblyCulture("")]
 
@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
 //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
 //通过使用 "*",如下所示:
 // [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("1.0.1.1")]
-[assembly: AssemblyFileVersion("1.0.1.1")]
+[assembly: AssemblyVersion("20.24.01.23")]
+[assembly: AssemblyFileVersion("20.24.01.23")]

+ 162 - 0
DK.XuWei.WebMes/S7.cs

@@ -0,0 +1,162 @@
+using Curtain.Net.Sockets.PLC;
+using Curtain.Net.Sockets.PLC.Model.Siemens;
+
+using System;
+using System.Collections.Generic;
+using System.Collections.Specialized;
+using System.Linq;
+using System.Text;
+
+namespace DK.XuWei.WebMes
+{
+    public class S7
+    {
+        public SocketClient<SiemensS7_1200Model> PLC = null;
+        public string PLC_IP = "";
+        public int PLC_PORT = 102;
+        public bool IS_OPEN = false;
+
+        public S7()
+        {
+            PLC = new SocketClient<SiemensS7_1200Model>();
+        }
+
+        public S7(string ip, int port = 102)
+        {
+            PLC_IP = ip;
+            PLC_PORT = port;
+            PLC = new SocketClient<SiemensS7_1200Model>();
+            Open(PLC_IP,PLC_PORT);
+        }
+
+        //连接PLC
+        public SocketClient<SiemensS7_1200Model> Open(string ip,int port=102)
+        {
+            if(PLC_IP != ip || PLC_PORT != port)
+            {
+                if (IS_OPEN) PLC.Close();
+                PLC_IP = ip;
+                PLC_PORT = port;
+                PLC.Connect(PLC_IP, PLC_PORT);
+                IS_OPEN = true;
+            }
+            else
+            {
+                if (!IS_OPEN)
+                {
+                    PLC.Connect(PLC_IP, PLC_PORT);
+                    IS_OPEN = true;
+                }
+            }
+            return PLC;
+        }
+
+        //写入PLC
+        public bool Write<T>(string db,object value,string dbname="D")
+        {
+            if (!IS_OPEN)
+            {
+                PLC.Connect(PLC_IP, PLC_PORT);
+                IS_OPEN = true;
+            }
+            if (typeof(T) == typeof(string))
+            {
+                PLCResult resultWrite = PLC.Write<T>(dbname, db, (T)(value),value.ToString().Length);
+                return resultWrite.Successed;
+            }
+            else
+            {
+                PLCResult resultWrite = PLC.Write<T>(dbname, db, (T)(value));
+                return resultWrite.Successed;
+            }
+        }
+
+        //读取PLC
+        public object Read<T>(string db,string dbname="D")
+        {
+            if (!IS_OPEN)
+            {
+                PLC.Connect(PLC_IP, PLC_PORT);
+                IS_OPEN = true;
+            }
+            PLCResult<T> resultRead = PLC.Read<T>(dbname, db);
+            return resultRead.Data;
+        }
+
+        public object Read<T>(string db, int len)
+        {
+            if (!IS_OPEN)
+            {
+                PLC.Connect(PLC_IP, PLC_PORT);
+                IS_OPEN = true;
+            }
+            PLCResult<T> resultRead = PLC.Read<T>("D", db, len);
+            return resultRead.Data;
+        }
+
+        //关闭PLC
+        public void Close()
+        {
+            if(IS_OPEN)
+            {
+                PLC.Close();
+                IS_OPEN = false;
+            }
+        }
+
+        //释放资源
+        public void Dispose()
+        {
+            if (IS_OPEN) PLC.Close();
+            PLC.Dispose();
+        }
+
+        //读取PLC数据默认1200D块
+        public static PLCResult<T> PlcRead<T>(string ip,string db)
+        {
+            SocketClient<SiemensS7_1200Model> plc = null;
+            plc = new SocketClient<SiemensS7_1200Model>();
+            plc.Connect(ip, 102);
+            PLCResult<T> resultRead = plc.Read<T>("D", db);
+            plc.Close();
+            plc.Dispose();
+            return resultRead;
+        }
+
+        //写入PLC数据默认1200D块
+        public static PLCResult PlcWrite<T>(string ip, string db, object value)
+        {
+            SocketClient<SiemensS7_1200Model> plc = null;
+            plc = new SocketClient<SiemensS7_1200Model>();
+            plc.Connect(ip, 102);
+            PLCResult resultWrite = plc.Write<T>("D", db, (T)(value));
+            plc.Close();
+            plc.Dispose();
+            return resultWrite;
+        }
+
+        //读取PLC数据200
+        public static PLCResult<T> PlcRead200<T>(string ip, string code,string number)
+        {
+            SocketClient<SiemensS7_200SmartModel> plc = null;
+            plc = new SocketClient<SiemensS7_200SmartModel>();
+            plc.Connect(ip, 102);
+            PLCResult<T> resultRead = plc.Read<T>(code, number);
+            plc.Close();
+            plc.Dispose();
+            return resultRead;
+        }
+
+        //写入PLC数据200
+        public static PLCResult PlcWrite200<T>(string ip,string code, string number, object value)
+        {
+            SocketClient<SiemensS7_200SmartModel> plc = null;
+            plc = new SocketClient<SiemensS7_200SmartModel>();
+            plc.Connect(ip, 102);
+            PLCResult resultWrite = plc.Write<T>(code, number, (T)(value));
+            plc.Close();
+            plc.Dispose();
+            return resultWrite;
+        }
+    }
+}