|
|
@@ -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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|