Explorar o código

修改PLC连接方法

ltwork hai 1 ano
pai
achega
f6fc008e2e

+ 1 - 1
BlankApp1/BlankApp1/App.config

@@ -5,7 +5,7 @@
 		<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="PLCIp" value="127.0.0.1"/>
 		<add key="PLCPort" value="6000"/>
 		<add key="DelayTime" value="20"/>
 		<add key="LongDelayTime" value="60"/>

+ 2 - 2
BlankApp1/BlankApp1/Common/IPLCConnection.cs

@@ -9,8 +9,8 @@ namespace PLCTool.Common
     public interface IPLCConnection
     {
      
-        bool ConnectPLC();
-        bool IsConnectPLC();
+        Task<bool> ConnectPLC();
+        Task<bool> IsConnectPLC();
         string ReadPlcObject(string address, VarType valueType);
         bool WritePlcObject(string address, VarType valueType, string writeValue);
         bool ResetPLC(string address, VarType valueType, string writeValue);

+ 57 - 31
BlankApp1/BlankApp1/Common/MelsecPLCConnectioncs.cs

@@ -34,28 +34,35 @@ namespace PLCTool.Common
                 return instance;
             }
         }
-        public bool ConnectPLC()
+        public async Task<bool> ConnectPLC()
         {
             try
             {
+                OperateResult operResult=null;
                 melsec_net = new MelsecMcNet(plcIp, Convert.ToInt32(plcPort));
-                Func<OperateResult> func = (() =>
-                {
-                    return  melsec_net.ConnectServer();
-                });
-                var connect = Task.Run(() => func.Invoke());
-             
-                if (connect.Result.IsSuccess)
-                {
-                    isConnect = true;
-                    return true;
-                }
-                else
-                {
-                    
-                    isConnect = false;
-                    return false;
-                }
+
+
+                var result = await Task.Run(() =>
+
+                    melsec_net.ConnectServer()
+                // 耗时操作
+                ) ; 
+              
+              
+                
+                    if (result.IsSuccess)
+                    {
+                        isConnect = true;
+                        return true;
+                    }
+                    else
+                    {
+
+                        isConnect = false;
+                        return false;
+                    }
+                
+                
 
             }
             catch (Exception e)
@@ -66,27 +73,46 @@ namespace PLCTool.Common
         }
 
       
-        public bool IsConnectPLC()
+        public async Task<bool> IsConnectPLC()
         {
-            short rb;
-            short reSuc = -1;
-            Func<OperateResult<bool>> func = () =>
-            {
-                return melsec_net.ReadBool("X100");
-            };
-            var workTask = Task.Run(() => func.Invoke());
-
-
-            //读取成功
-            if (workTask.Result.IsSuccess)
+            //short rb;
+            //short reSuc = -1;
+            //Func<OperateResult<bool>> func = () =>
+            //{
+            //    return melsec_net.ReadBool("X100");
+            //};
+            //var workTask = Task.Run(() => func.Invoke());
+
+
+            ////读取成功
+            //if (workTask.Result.IsSuccess)
+            //{
+            //    return true;
+            //}
+            //else
+            //{
+            //    return false;
+            //}
+
+            var result = await Task.Run(() =>
+
+                melsec_net.ReadBool("X100")
+             // 耗时操作
+             );
+
+            if (result.IsSuccess)
             {
+                
                 return true;
             }
             else
             {
+
+              
                 return false;
             }
-           
+
+       
         }
 
         public string ReadPlcObject(string address, VarType valueType)

+ 12 - 5
BlankApp1/BlankApp1/Common/PLCCom.cs

@@ -8,6 +8,7 @@ using System.Net;
 using System.Text;
 using System.Threading;
 using System.Threading.Tasks;
+using TFT_MelsecMcNet;
 
 namespace PLCTool.Common
 {
@@ -36,9 +37,9 @@ namespace PLCTool.Common
                 return instance;
             }
         }
-     
 
-        public bool IsConnectPLC()
+
+        public async Task<bool> IsConnectPLC()
         {
          
             //return ENT.IsConnect;
@@ -58,14 +59,20 @@ namespace PLCTool.Common
                 return true;
             }
             return false;
+
+           
         }
 
-        public bool ConnectPLC()
+        public async Task<bool> ConnectPLC()
         {
             try
             {
-            
-                short re = ENT.Link(plcIp, short.Parse(plcPort), 500);
+                short re = -1;
+                await Task.Run(() =>
+                {
+                    re = ENT.Link(plcIp, short.Parse(plcPort), 500);
+                });
+          
                 if (re == 0)
                 {
                     isConnect = true;

+ 3 - 4
BlankApp1/BlankApp1/ViewModels/MainWindowViewModel.cs

@@ -123,7 +123,7 @@ namespace BlankApp1.ViewModels
         /// <summary>
         /// 连接PLC
         /// </summary>
-        private void JudgePLC()
+        private  void JudgePLC()
         {
             
                 Task.Run(async () =>
@@ -132,10 +132,9 @@ namespace BlankApp1.ViewModels
                     {
                        
                         //更新界面上的值
-                        Application.Current.Dispatcher.Invoke(() =>
+                        Application.Current.Dispatcher.Invoke(async () =>
                         {
-                            PLCIsConnect = PLCConnectionFactory.CreatePLCCont().IsConnectPLC();
-
+                            PLCIsConnect =await PLCConnectionFactory.CreatePLCCont().IsConnectPLC();
 
                         });
 

+ 3 - 2
BlankApp1/BlankApp1/ViewModels/MonitorManageViewModel/PLCReadViewModel.cs

@@ -66,12 +66,13 @@ namespace PLCTool.ViewModels.MonitorManageViewModel
                 while (true)
                 {
                     //更新界面上的值
-                    Application.Current.Dispatcher.Invoke(() =>
+                    Application.Current.Dispatcher.Invoke(async () =>
                     {
                         foreach (var item in PLCItemList)
                         {
+                            bool isConnect = await PLCConnectionFactory.CreatePLCCont().IsConnectPLC();
                             //没有连接 plc,直接退出
-                            if (!PLCConnectionFactory.CreatePLCCont().IsConnectPLC())
+                            if (!isConnect)
                             {
                                 break;
                             }

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

@@ -41,9 +41,9 @@ namespace PLCTool.ViewModels.MonitorManageViewModel
         {
             GetPLCConfig();
         }
-        private void WriteToPlc(object obj)
+        private async void WriteToPlc(object obj)
         {//没有连接 plc,直接退出
-            if (!PLCConnectionFactory.CreatePLCCont().IsConnectPLC())
+            if (!await PLCConnectionFactory.CreatePLCCont().IsConnectPLC())
             {
                 MessageBox.Show("PLC未连接!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                 return;