Ver código fonte

自动连接PLC

user_lt 1 ano atrás
pai
commit
42b730d107

+ 4 - 3
BlankApp1/BlankApp1/Common/PLCCom.cs

@@ -13,7 +13,7 @@ namespace PLCTool.Common
     {
         private static string plcIp = ConfigurationManager.AppSettings["PLCIp"];
         private static string plcPort = ConfigurationManager.AppSettings["PLCPort"];
-        private EtherNetPLC ENT;//plc连接
+        private static  EtherNetPLC ENT = new EtherNetPLC();
         private static PLCCom instance;
         private static bool isConnect=false;
        
@@ -26,7 +26,7 @@ namespace PLCTool.Common
 
         public static PLCCom GetInstance()
         {
-            if (instance == null||(!isConnect))
+            if ((!ENT.IsConnect))
             {
                instance = new PLCCom();
             }
@@ -43,7 +43,7 @@ namespace PLCTool.Common
         {
             try
             {
-                ENT = new EtherNetPLC();
+            
                 short re = ENT.Link(plcIp, short.Parse(plcPort), 500);
                 if (re == 0)
                 {
@@ -52,6 +52,7 @@ namespace PLCTool.Common
                 }
                 else
                 {
+                   
                     isConnect = false;
                     return false;
                 }

+ 4 - 0
BlankApp1/BlankApp1/PLCTool.csproj

@@ -4,6 +4,7 @@
     <TargetFramework>net6.0-windows</TargetFramework>
     <UseWPF>true</UseWPF>
     <UseWindowsForms>True</UseWindowsForms>
+    <ApplicationIcon>bin\Debug\net6.0-windows\Images\TB0228.ico</ApplicationIcon>
   </PropertyGroup>
   <ItemGroup>
     <None Remove="Assets\Fonts\iconfont.ttf" />
@@ -13,6 +14,9 @@
     <None Remove="Assets\Images\电量蓝色.png" />
     <None Remove="Views\BusinessManageView\AutoTestView.xaml~RF1f86dd9.TMP" />
   </ItemGroup>
+  <ItemGroup>
+    <Content Include="bin\Debug\net6.0-windows\Images\TB0228.ico" />
+  </ItemGroup>
   <ItemGroup>
     <PackageReference Include="AutoMapper" Version="12.0.1" />
     <PackageReference Include="HandyControl" Version="3.4.0" />

+ 11 - 3
BlankApp1/OmronFinsTCP.Net/EtherNetPLC.cs

@@ -21,7 +21,10 @@ namespace OmronFinsTCP.Net
         {
             get { return BasicClass.pcNode.ToString(); }
         }
-
+        public bool IsConnect
+        {
+            get { return BasicClass.Client.Connected; }
+        }
         /// <summary>
         /// 实例化PLC操作对象
         /// </summary>
@@ -41,6 +44,10 @@ namespace OmronFinsTCP.Net
         {
             if(BasicClass.PingCheck(rIP,timeOut))
             {
+                if(BasicClass.Client.Client==null)
+                {
+                    BasicClass.Client = new TcpClient();
+                }
                 BasicClass.Client.Connect(rIP, (int)rPort);
                 BasicClass.Stream = BasicClass.Client.GetStream();
                 Thread.Sleep(10);
@@ -72,6 +79,7 @@ namespace OmronFinsTCP.Net
             }
             else
             {
+                BasicClass.Client?.Close();
                 //连接超时
                 return -1;
             }
@@ -85,8 +93,8 @@ namespace OmronFinsTCP.Net
         {
             try
             {
-                BasicClass.Stream.Close();
-                BasicClass.Client.Close();
+                BasicClass.Stream?.Close();
+                BasicClass.Client?.Close();
                 return 0;
             }
             catch