瀏覽代碼

修改配置PLC

ltwork 1 年之前
父節點
當前提交
8a0b0eddb2

+ 3 - 0
BlankApp1/BlankApp1/App.config

@@ -3,9 +3,12 @@
 	<appSettings>
 		<!--连接字符串 SQL Server-->
 		<add key="MySql" value="Data Source=localhost;Database=plc_point_db2;User Id='root';Password='521125';port=3306;charset=utf8mb4;"/>
+	    <!--1表示欧姆龙PLC,2表示为三菱PLC-->
+		<add key="PLCType" value="2"/>
 		<add key="PLCIp" value="192.168.3.39"/>
 		<add key="PLCPort" value="3000"/>
 		<add key="DelayTime" value="20"/>
 		<add key="LongDelayTime" value="60"/>
+	
 	</appSettings>
 </configuration>

+ 176 - 0
BlankApp1/BlankApp1/Common/MelsecPLCConnectioncs.cs

@@ -0,0 +1,176 @@
+using OmronFinsTCP.Net;
+using System;
+using System.Collections.Generic;
+using System.Configuration;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using TFT_MelsecMcNet;
+
+namespace PLCTool.Common
+{
+    public  class MelsecPLCConnectioncs : IPLCConnection
+    {
+        private static string plcIp = ConfigurationManager.AppSettings["PLCIp"];
+        private static string plcPort = ConfigurationManager.AppSettings["PLCPort"];
+        private static MelsecMcNet melsec_net;
+        private static MelsecPLCConnectioncs instance;
+        private static bool isConnect = false;
+        public MelsecPLCConnectioncs()
+        {
+            ConnectPLC();
+        }
+        public static MelsecPLCConnectioncs Instance
+        {
+            get
+            {
+                if ((instance==null)||(!melsec_net.IsConnect()))
+                {
+                    instance = new MelsecPLCConnectioncs();
+                }
+                return instance;
+            }
+        }
+        public bool ConnectPLC()
+        {
+            try
+            {
+                melsec_net = new MelsecMcNet(plcIp, Convert.ToInt32(plcPort));
+                OperateResult connect = melsec_net.ConnectServer();
+                if (connect.IsSuccess)
+                {
+                    isConnect = true;
+                    return true;
+                }
+                else
+                {
+                    
+                    isConnect = false;
+                    return false;
+                }
+
+            }
+            catch (Exception e)
+            {
+                melsec_net.ConnectClose();
+                return false;
+            }
+        }
+
+        public bool IsConnectPLC()
+        {
+            return melsec_net.IsConnect();
+        }
+
+        public string ReadPlcObject(string address, VarType valueType)
+        {
+            string value = string.Empty; 
+            short rb;
+            short reSuc = -1;
+            if (isConnect == false)
+            {
+                return string.Empty;
+            }
+
+            switch (valueType)
+            {
+                case VarType.Bit:
+                    OperateResult<bool> isBool = melsec_net.ReadBool(address);
+                    if (isBool.IsSuccess)
+                    {
+                        if (isBool.Content)
+                        {
+                            return "1";
+                        }
+                        else
+                        {
+                            return "0";
+                        }
+                    }
+
+                    break;
+                case VarType.Byte:
+                    break;
+   
+                case VarType.DWord:
+                    break;
+                case VarType.Word:
+                case VarType.Int:
+                    OperateResult<int> intResult = melsec_net.ReadInt32(address);
+                    if (intResult.IsSuccess)
+                    {
+                        return intResult.Content.ToString();
+                    }
+                    break;
+                case VarType.DInt:
+                    break;
+                case VarType.Real:
+                    break;
+
+                default:
+                    return null;
+            }
+            return value;
+        }
+
+        public bool ResetPLC(string address, VarType valueType, string writeValue)
+        {
+            return false;
+        }
+
+        public bool WritePlcObject(string address, VarType valueType, string writeValue)
+        {
+            bool isSuccess = false;
+
+            if (isConnect == false)
+            {
+                return false;
+            }
+            try
+            {
+                switch (valueType)
+                {
+                    case VarType.Bit:
+                        bool writeValueBool=writeValue=="1" ? true:false;
+                        OperateResult writeResut=melsec_net.Write(address, writeValueBool);
+                    
+                        //写成功
+                        if (writeResut.IsSuccess)
+                        {
+                            isSuccess = true;
+                        }
+                        break;
+                    case VarType.Byte:
+                        break;
+                  
+                      
+                    case VarType.DWord:
+                        break;
+                    case VarType.Word:
+                    case VarType.Int:
+                        int wValue = Convert.ToInt32(writeValue);
+                        OperateResult writeResutInt = melsec_net.Write(address, wValue);
+
+                        //写成功
+                        if (writeResutInt.IsSuccess)
+                        {
+                            isSuccess = true;
+                        }
+                        break;
+                    
+                    case VarType.DInt:
+                        break;
+                    case VarType.Real:
+                        break;
+
+                }
+            }
+            catch (Exception ex)
+            {
+
+            }
+
+            return isSuccess;
+        }
+    }
+}

+ 21 - 9
BlankApp1/BlankApp1/Common/PLCCom.cs

@@ -2,14 +2,16 @@
 using System;
 using System.Collections.Generic;
 using System.Configuration;
+using System.Diagnostics;
 using System.Linq;
 using System.Net;
 using System.Text;
+using System.Threading;
 using System.Threading.Tasks;
 
 namespace PLCTool.Common
 {
-    public class PLCCom
+    public class PLCCom:IPLCConnection
     {
         private static string plcIp = ConfigurationManager.AppSettings["PLCIp"];
         private static string plcPort = ConfigurationManager.AppSettings["PLCPort"];
@@ -23,23 +25,33 @@ namespace PLCTool.Common
             ConnectPLC();
         }
 
-
-        public static PLCCom GetInstance()
+        public static PLCCom Instance
         {
-            if ((!ENT.IsConnect))
+            get
             {
-               instance = new PLCCom();
+                if ((!ENT.IsConnect))
+                {
+                    instance = new PLCCom();
+                }
+                return instance;
             }
-            return instance;
-
         }
+     
 
         public bool IsConnectPLC()
         {
+         
             //return ENT.IsConnect;
             short rb;
+            short reSuc=-1;
+            Func<short> func = () =>
+            {
+                return ENT.GetBitState(PlcMemory.CIO, "3321.10", out rb);
+            };
+            var workTask = Task.Run(() => func.Invoke());
             //读取CIO区判断PLC连接情况
-            short reSuc = ENT.GetBitState(PlcMemory.CIO, "3321.10", out rb);
+         
+            reSuc = workTask.Result;
             //读取成功
             if (reSuc == 0)
             {
@@ -48,7 +60,7 @@ namespace PLCTool.Common
             return false;
         }
 
-        private bool ConnectPLC()
+        public bool ConnectPLC()
         {
             try
             {

+ 26 - 0
BlankApp1/BlankApp1/Common/PLCConnectionFactory.cs

@@ -0,0 +1,26 @@
+using System;
+using System.Collections.Generic;
+using System.Configuration;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace PLCTool.Common
+{
+    public  class PLCConnectionFactory
+    {
+        private static string plcType = ConfigurationManager.AppSettings["PLCType"];
+        public static IPLCConnection CreatePLCCont()
+        {
+            switch (plcType)
+            {
+                case "1":
+                    return PLCCom.Instance;
+                case "2":
+                    return MelsecPLCConnectioncs.Instance;
+                default:
+                    throw new ArgumentException("Unsupported PLC type");
+            }
+        }
+    }
+}

+ 7 - 7
BlankApp1/BlankApp1/ViewModels/BusinessManageViewModel/AutoTestViewModel.cs

@@ -816,7 +816,7 @@ namespace PLCTool.ViewModels.BusinessManageViewModel
                 switch (plcAddType)
                 {
                     case "bool":
-                        string readResult = PLCCom.GetInstance().ReadPlcObject(plcAddress, VarType.Bit);
+                        string readResult = PLCConnectionFactory.CreatePLCCont().ReadPlcObject(plcAddress, VarType.Bit);
                         ///实时值记录
 
                         item.RealValue = readResult;
@@ -947,7 +947,7 @@ namespace PLCTool.ViewModels.BusinessManageViewModel
                 {
                     case "bool":
 
-                        bool writeResult = PLCCom.GetInstance().WritePlcObject(plcAddress, VarType.Bit, plcValue);
+                        bool writeResult = PLCConnectionFactory.CreatePLCCont().WritePlcObject(plcAddress, VarType.Bit, plcValue);
                         ///实时值记录
                         System.Windows.Application.Current.Dispatcher.Invoke((delegate
                         {
@@ -970,7 +970,7 @@ namespace PLCTool.ViewModels.BusinessManageViewModel
                         break;
                     case "word":
                        
-                        bool writeWrordResult = PLCCom.GetInstance().WritePlcObject(plcAddress, VarType.Word, plcValue);
+                        bool writeWrordResult = PLCConnectionFactory.CreatePLCCont().WritePlcObject(plcAddress, VarType.Word, plcValue);
                         ///实时值记录
                         ///
                         System.Windows.Application.Current.Dispatcher.Invoke((delegate
@@ -1147,7 +1147,7 @@ namespace PLCTool.ViewModels.BusinessManageViewModel
                                 }));
                                 //先延时,在读取,避免读取传感器刚开始的数据
                                 await Task.Delay(1000);
-                                readResult = PLCCom.GetInstance().ReadPlcObject(plcAddress, VarType.Bit);
+                                readResult = PLCConnectionFactory.CreatePLCCont().ReadPlcObject(plcAddress, VarType.Bit);
                                 if (readResult == item.PlcValue)
                                 {
                                     break;
@@ -1156,7 +1156,7 @@ namespace PLCTool.ViewModels.BusinessManageViewModel
                             }
                         });
                         //可能有多个条件判定的,第二次时间到了直接跳出延时,直接读取,不用在延时20s
-                        readResult = PLCCom.GetInstance().ReadPlcObject(plcAddress, VarType.Bit);
+                        readResult = PLCConnectionFactory.CreatePLCCont().ReadPlcObject(plcAddress, VarType.Bit);
                         System.Windows.Application.Current.Dispatcher.Invoke((delegate
                         {
                             ProVisibility = Visibility.Hidden;
@@ -1195,7 +1195,7 @@ namespace PLCTool.ViewModels.BusinessManageViewModel
                                     ProVisibility = Visibility.Visible;
                                 }));
 
-                                readResult2 = PLCCom.GetInstance().ReadPlcObject(plcAddress, VarType.Word);
+                                readResult2 = PLCConnectionFactory.CreatePLCCont().ReadPlcObject(plcAddress, VarType.Word);
                                 if (readResult2 == item.PlcValue)
                                 {
                                     break;
@@ -1204,7 +1204,7 @@ namespace PLCTool.ViewModels.BusinessManageViewModel
                             }
                         });
                         //可能有多个条件判定的,第二次时间到了直接跳出延时,直接读取,不用在延时20s
-                        readResult2 = PLCCom.GetInstance().ReadPlcObject(plcAddress, VarType.Word);
+                        readResult2 = PLCConnectionFactory.CreatePLCCont().ReadPlcObject(plcAddress, VarType.Word);
                         ProVisibility = Visibility.Hidden;
                         ///实时值记录
                         item.RealValue = readResult2;

+ 10 - 10
BlankApp1/BlankApp1/ViewModels/BusinessManageViewModel/ManualTestViewModel.cs

@@ -96,11 +96,11 @@ namespace PLCTool.ViewModels.BusinessManageViewModel
         /// </summary>
         private async void Reset()
         {
-            PLCCom.GetInstance().ResetPLC("1.00", VarType.Bit, "0");
+            PLCConnectionFactory.CreatePLCCont().ResetPLC("1.00", VarType.Bit, "0");
             await Task.Delay(1000);
-            PLCCom.GetInstance().ResetPLC("1.00", VarType.Bit, "1");
+            PLCConnectionFactory.CreatePLCCont().ResetPLC("1.00", VarType.Bit, "1");
             await Task.Delay(1000);
-            bool isResult=PLCCom.GetInstance().ResetPLC("1.00", VarType.Bit, "0");
+            bool isResult=PLCConnectionFactory.CreatePLCCont().ResetPLC("1.00", VarType.Bit, "0");
             if(isResult)
             {
                 MessageBox.Show("复位成功!", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
@@ -793,7 +793,7 @@ namespace PLCTool.ViewModels.BusinessManageViewModel
                 switch (plcAddType)
                 {
                     case "bool":
-                        string readResult = PLCCom.GetInstance().ReadPlcObject(plcAddress, VarType.Bit);
+                        string readResult = PLCConnectionFactory.CreatePLCCont().ReadPlcObject(plcAddress, VarType.Bit);
                         ///实时值记录
 
                         item.RealValue = readResult;
@@ -939,7 +939,7 @@ namespace PLCTool.ViewModels.BusinessManageViewModel
                             if (callback.Result == ButtonResult.OK)
                             {
                                 string plcRealValue = callback.Parameters.GetValue<string>("ReturnValue");
-                                bool writeResult = PLCCom.GetInstance().WritePlcObject(plcAddress, VarType.Bit, plcRealValue);
+                                bool writeResult = PLCConnectionFactory.CreatePLCCont().WritePlcObject(plcAddress, VarType.Bit, plcRealValue);
                                 ///实时值记录
                                 item.RealValue = plcRealValue;
                                 item.TestTime = DateTime.Now;
@@ -971,7 +971,7 @@ namespace PLCTool.ViewModels.BusinessManageViewModel
                             if (callback.Result == ButtonResult.OK)
                             {
                                 string plcRealValue = callback.Parameters.GetValue<string>("ReturnValue");
-                                bool writeResult = PLCCom.GetInstance().WritePlcObject(plcAddress, VarType.Word, plcRealValue);
+                                bool writeResult = PLCConnectionFactory.CreatePLCCont().WritePlcObject(plcAddress, VarType.Word, plcRealValue);
                                 ///实时值记录
                                 item.RealValue = plcRealValue;
                                 item.TestTime = DateTime.Now;
@@ -1116,7 +1116,7 @@ namespace PLCTool.ViewModels.BusinessManageViewModel
                                 }));
                                 //先延时,在读取,避免读取传感器刚开始的数据
                                 await Task.Delay(1000);
-                                readResult = PLCCom.GetInstance().ReadPlcObject(plcAddress, VarType.Bit);
+                                readResult = PLCConnectionFactory.CreatePLCCont().ReadPlcObject(plcAddress, VarType.Bit);
                                 if (readResult == item.PlcValue)
                                 {
                                     break;
@@ -1125,7 +1125,7 @@ namespace PLCTool.ViewModels.BusinessManageViewModel
                             }
                         });
                         //可能有多个条件判定的,第二次时间到了直接跳出延时,直接读取,不用在延时20s
-                        readResult = PLCCom.GetInstance().ReadPlcObject(plcAddress, VarType.Bit);
+                        readResult = PLCConnectionFactory.CreatePLCCont().ReadPlcObject(plcAddress, VarType.Bit);
                         ProVisibility = Visibility.Hidden;
                         ///实时值记录
                         item.RealValue = readResult;
@@ -1155,7 +1155,7 @@ namespace PLCTool.ViewModels.BusinessManageViewModel
                                     ProVisibility = Visibility.Visible;
                                 }));
 
-                                readResult2 = PLCCom.GetInstance().ReadPlcObject(plcAddress, VarType.Word);
+                                readResult2 = PLCConnectionFactory.CreatePLCCont().ReadPlcObject(plcAddress, VarType.Word);
                                 if (readResult2 == item.PlcValue)
                                 {
                                     break;
@@ -1164,7 +1164,7 @@ namespace PLCTool.ViewModels.BusinessManageViewModel
                             }
                         });
                         //可能有多个条件判定的,第二次直接跳出延时,直接读取
-                        readResult2 = PLCCom.GetInstance().ReadPlcObject(plcAddress, VarType.Word);
+                        readResult2 = PLCConnectionFactory.CreatePLCCont().ReadPlcObject(plcAddress, VarType.Word);
                         ProVisibility = Visibility.Hidden;
                         ///实时值记录
                         item.RealValue = readResult2;

+ 7 - 7
BlankApp1/BlankApp1/ViewModels/BusinessManageViewModel/RetryTestViewModel.cs

@@ -783,7 +783,7 @@ namespace PLCTool.ViewModels.BusinessManageViewModel
                 switch (plcAddType)
                 {
                     case "bool":
-                        string readResult = PLCCom.GetInstance().ReadPlcObject(plcAddress, VarType.Bit);
+                        string readResult = PLCConnectionFactory.CreatePLCCont().ReadPlcObject(plcAddress, VarType.Bit);
                         ///实时值记录
 
                         item.RealValue = readResult;
@@ -929,7 +929,7 @@ namespace PLCTool.ViewModels.BusinessManageViewModel
                             if (callback.Result == ButtonResult.OK)
                             {
                                 string plcRealValue = callback.Parameters.GetValue<string>("ReturnValue");
-                                bool writeResult = PLCCom.GetInstance().WritePlcObject(plcAddress, VarType.Bit, plcRealValue);
+                                bool writeResult = PLCConnectionFactory.CreatePLCCont().WritePlcObject(plcAddress, VarType.Bit, plcRealValue);
                                 ///实时值记录
                                 item.RealValue = plcRealValue;
                                 item.TestTime = DateTime.Now;
@@ -961,7 +961,7 @@ namespace PLCTool.ViewModels.BusinessManageViewModel
                             if (callback.Result == ButtonResult.OK)
                             {
                                 string plcRealValue = callback.Parameters.GetValue<string>("ReturnValue");
-                                bool writeResult = PLCCom.GetInstance().WritePlcObject(plcAddress, VarType.Word, plcRealValue);
+                                bool writeResult = PLCConnectionFactory.CreatePLCCont().WritePlcObject(plcAddress, VarType.Word, plcRealValue);
                                 ///实时值记录
                                 item.RealValue = plcRealValue;
                                 item.TestTime = DateTime.Now;
@@ -1106,7 +1106,7 @@ namespace PLCTool.ViewModels.BusinessManageViewModel
                                 }));
                                 //先延时,在读取,避免读取传感器刚开始的数据
                                 await Task.Delay(1000);
-                                readResult = PLCCom.GetInstance().ReadPlcObject(plcAddress, VarType.Bit);
+                                readResult = PLCConnectionFactory.CreatePLCCont().ReadPlcObject(plcAddress, VarType.Bit);
                                 if (readResult == item.PlcValue)
                                 {
                                     break;
@@ -1115,7 +1115,7 @@ namespace PLCTool.ViewModels.BusinessManageViewModel
                             }
                         });
                         //可能有多个条件判定的,第二次时间到了直接跳出延时,直接读取,不用在延时20s
-                        readResult = PLCCom.GetInstance().ReadPlcObject(plcAddress, VarType.Bit);
+                        readResult = PLCConnectionFactory.CreatePLCCont().ReadPlcObject(plcAddress, VarType.Bit);
                         ProVisibility = Visibility.Hidden;
                         ///实时值记录
                         item.RealValue = readResult;
@@ -1145,7 +1145,7 @@ namespace PLCTool.ViewModels.BusinessManageViewModel
                                     ProVisibility = Visibility.Visible;
                                 }));
 
-                                readResult2 = PLCCom.GetInstance().ReadPlcObject(plcAddress, VarType.Word);
+                                readResult2 = PLCConnectionFactory.CreatePLCCont().ReadPlcObject(plcAddress, VarType.Word);
                                 if (readResult2 == item.PlcValue)
                                 {
                                     break;
@@ -1154,7 +1154,7 @@ namespace PLCTool.ViewModels.BusinessManageViewModel
                             }
                         });
                         //可能有多个条件判定的,第二次直接跳出延时,直接读取
-                        readResult2 = PLCCom.GetInstance().ReadPlcObject(plcAddress, VarType.Word);
+                        readResult2 = PLCConnectionFactory.CreatePLCCont().ReadPlcObject(plcAddress, VarType.Word);
                         ProVisibility = Visibility.Hidden;
                         ///实时值记录
                         item.RealValue = readResult2;

+ 1 - 1
BlankApp1/BlankApp1/ViewModels/MainWindowViewModel.cs

@@ -134,7 +134,7 @@ namespace BlankApp1.ViewModels
                         //更新界面上的值
                         Application.Current.Dispatcher.Invoke(() =>
                         {
-                            PLCIsConnect = PLCCom.GetInstance().IsConnectPLC();
+                            PLCIsConnect = PLCConnectionFactory.CreatePLCCont().IsConnectPLC();
 
 
                         });

+ 5 - 5
BlankApp1/BlankApp1/ViewModels/MonitorManageViewModel/PLCReadViewModel.cs

@@ -71,7 +71,7 @@ namespace PLCTool.ViewModels.MonitorManageViewModel
                         foreach (var item in PLCItemList)
                         {
                             //没有连接 plc,直接退出
-                            if (!PLCCom.GetInstance().IsConnectPLC())
+                            if (!PLCConnectionFactory.CreatePLCCont().IsConnectPLC())
                             {
                                 break;
                             }
@@ -80,16 +80,16 @@ namespace PLCTool.ViewModels.MonitorManageViewModel
                             switch (plcAddType)
                             {
                                 case "bool":
-                                    item.RealValue = PLCCom.GetInstance().ReadPlcObject(plcAddr, VarType.Bit);
+                                    item.RealValue = PLCConnectionFactory.CreatePLCCont().ReadPlcObject(plcAddr, VarType.Bit);
                                     break;
                                 case "word":
-                                    item.RealValue = PLCCom.GetInstance().ReadPlcObject(plcAddr, VarType.Word);
+                                    item.RealValue = PLCConnectionFactory.CreatePLCCont().ReadPlcObject(plcAddr, VarType.Word);
                                     break;
                                 case "dword":
-                                    item.RealValue = PLCCom.GetInstance().ReadPlcObject(plcAddr, VarType.DWord);
+                                    item.RealValue = PLCConnectionFactory.CreatePLCCont().ReadPlcObject(plcAddr, VarType.DWord);
                                     break;
                                 case "real":
-                                    item.RealValue = PLCCom.GetInstance().ReadPlcObject(plcAddr, VarType.Real);
+                                    item.RealValue = PLCConnectionFactory.CreatePLCCont().ReadPlcObject(plcAddr, VarType.Real);
                                     break;
                             }
 

+ 5 - 5
BlankApp1/BlankApp1/ViewModels/MonitorManageViewModel/PLCWriteViewModel.cs

@@ -43,7 +43,7 @@ namespace PLCTool.ViewModels.MonitorManageViewModel
         }
         private void WriteToPlc(object obj)
         {//没有连接 plc,直接退出
-            if (!PLCCom.GetInstance().IsConnectPLC())
+            if (!PLCConnectionFactory.CreatePLCCont().IsConnectPLC())
             {
                 MessageBox.Show("PLC未连接!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                 return;
@@ -69,7 +69,7 @@ namespace PLCTool.ViewModels.MonitorManageViewModel
                 switch (plcAddType)
                 {
                     case "bool":
-                        bool isResult = PLCCom.GetInstance().WritePlcObject(plcAddr, VarType.Bit, plcValue);
+                        bool isResult = PLCConnectionFactory.CreatePLCCont().WritePlcObject(plcAddr, VarType.Bit, plcValue);
                         if(isResult)
                         {
                             _logger.LogInformation($"向PLC写入值。plc地址{plcAddr},数据值{plcValue}");
@@ -77,7 +77,7 @@ namespace PLCTool.ViewModels.MonitorManageViewModel
                         }
                         break;
                     case "word":
-                        bool isResult2 = PLCCom.GetInstance().WritePlcObject(plcAddr, VarType.Word, plcValue);
+                        bool isResult2 = PLCConnectionFactory.CreatePLCCont().WritePlcObject(plcAddr, VarType.Word, plcValue);
                         if (isResult2)
                         {
                             _logger.LogInformation($"向PLC写入值。plc地址{plcAddr},数据值{plcValue}");
@@ -85,7 +85,7 @@ namespace PLCTool.ViewModels.MonitorManageViewModel
                         }
                         break;
                     case "dword":
-                        bool isResult3 = PLCCom.GetInstance().WritePlcObject(plcAddr, VarType.DWord, plcValue);
+                        bool isResult3 = PLCConnectionFactory.CreatePLCCont().WritePlcObject(plcAddr, VarType.DWord, plcValue);
                         if (isResult3)
                         {
                             _logger.LogInformation($"向PLC写入值。plc地址{plcAddr},数据值{plcValue}");
@@ -94,7 +94,7 @@ namespace PLCTool.ViewModels.MonitorManageViewModel
                         break;
                     case "real":
                        
-                        bool isResult4 = PLCCom.GetInstance().WritePlcObject(plcAddr, VarType.Real, plcValue);
+                        bool isResult4 = PLCConnectionFactory.CreatePLCCont().WritePlcObject(plcAddr, VarType.Real, plcValue);
                         if (isResult4)
                         {
                             _logger.LogInformation($"向PLC写入值。plc地址{plcAddr},数据值{plcValue}");

+ 1 - 1
BlankApp1/BlankApp1/Views/MainWindow.xaml

@@ -163,7 +163,7 @@
                         </EllipseGeometry>
                     </Path.Data>
                 </Path>
-                <TextBlock Text="V2.0" Style="{StaticResource txtHeadStyle}" Width="60" Margin="30,0,12,0"/>
+                <TextBlock Text="V2.1" Style="{StaticResource txtHeadStyle}" Width="60" Margin="30,0,12,0"/>
             </StackPanel>
 
         </Border>

+ 10 - 1
BlankApp1/TFT-MelsecMcNet/MelsecMcNet.cs

@@ -130,6 +130,7 @@ namespace TFT_MelsecMcNet
         /// 是否使用同步的网络通讯
         /// </summary>
         public bool UseSynchronousNet { get; set; } = false;
+        OperateResult<TcpClient> rSocket;
         #endregion Global Member
 
         #region Constructor
@@ -164,7 +165,7 @@ namespace TFT_MelsecMcNet
             tcpClient?.Close();
             tcpClient?.Dispose();
 
-            OperateResult<TcpClient> rSocket = CreateSocketAndInitialication();
+             rSocket = CreateSocketAndInitialication();
 
             if (!rSocket.IsSuccess)
             {
@@ -181,6 +182,14 @@ namespace TFT_MelsecMcNet
 
             return result;
         }
+        public bool IsConnect()
+        {
+            if ((rSocket != null)&&(rSocket.Content!=null))
+            {
+                return rSocket.Content.Connected;
+            }
+            return false;
+        }
         /// <summary>
         /// 连接并初始化网络套接字
         /// </summary>