From a7b168385463b6fe2734d753f631a692f69eacdf Mon Sep 17 00:00:00 2001
From: jinxin <1013386558@qq.com>
Date: 星期一, 23 六月 2025 14:51:21 +0800
Subject: [PATCH] 11

---
 C#/HH.WCS.Mobox3.KeKou/process/DeviceProcess.cs |   64 ++++++++++++++++++++++++++++++--
 1 files changed, 60 insertions(+), 4 deletions(-)

diff --git "a/C\043/HH.WCS.Mobox3.KeKou/process/DeviceProcess.cs" "b/C\043/HH.WCS.Mobox3.KeKou/process/DeviceProcess.cs"
index 0fbd675..472bbdf 100644
--- "a/C\043/HH.WCS.Mobox3.KeKou/process/DeviceProcess.cs"
+++ "b/C\043/HH.WCS.Mobox3.KeKou/process/DeviceProcess.cs"
@@ -6,6 +6,7 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Net.Sockets;
 using System.Threading;
 
 namespace HH.WCS.Mobox3.KeKou.process
@@ -69,18 +70,73 @@
         {
             if (State == 1023)
             {
-                PlcHelper.SendHex("172.31.94.107", "00 00 00 00 00 13 01 10 00 00 00 06 0C 00 01 00 00 00 00 00 00 00 00 00 00");
-                PlcHelper.SendHex("172.31.94.108", "00 00 00 00 00 13 01 10 00 00 00 06 0C 00 01 00 00 00 00 00 00 00 00 00 00");
+                SendHexOnce("172.31.94.107", 4001, "00 00 00 00 00 13 01 10 00 00 00 06 0C 00 01 00 00 00 00 00 00 00 00 00 00");
+                SendHexOnce("172.31.94.108", 4001, "00 00 00 00 00 13 01 10 00 00 00 06 0C 00 01 00 00 00 00 00 00 00 00 00 00");
+                //PlcHelper.SendHex("172.31.94.107", "00 00 00 00 00 13 01 10 00 00 00 06 0C 00 01 00 00 00 00 00 00 00 00 00 00");
+                //PlcHelper.SendHex("172.31.94.108", "00 00 00 00 00 13 01 10 00 00 00 06 0C 00 01 00 00 00 00 00 00 00 00 00 00");
+
             }
 
             if (State == 1025)
             {
-                PlcHelper.SendHex("172.31.94.107", "00 00 00 00 00 13 01 10 00 00 00 06 0C 00 00 00 01 00 00 00 00 00 00 00 00");
-                PlcHelper.SendHex("172.31.94.108", "00 00 00 00 00 13 01 10 00 00 00 06 0C 00 00 00 01 00 00 00 00 00 00 00 00");
+                SendHexOnce("172.31.94.107", 4001, "00 00 00 00 00 13 01 10 00 00 00 06 0C 00 00 00 01 00 00 00 00 00 00 00 00");
+                SendHexOnce("172.31.94.108", 4001, "00 00 00 00 00 13 01 10 00 00 00 06 0C 00 00 00 01 00 00 00 00 00 00 00 00");
+                //PlcHelper.SendHex("172.31.94.107", "00 00 00 00 00 13 01 10 00 00 00 06 0C 00 00 00 01 00 00 00 00 00 00 00 00");
+                //PlcHelper.SendHex("172.31.94.108", "00 00 00 00 00 13 01 10 00 00 00 06 0C 00 00 00 01 00 00 00 00 00 00 00 00");
             }
         }
 
 
+        private static string SendHexOnce(string ip, int port, string hex)
+        {
+            var res = string.Empty;
+            try
+            {
+                Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
+                //client.Connect(ip, port);
+                IAsyncResult connectResult = client.BeginConnect(ip, port, null, null);
+                if (!connectResult.AsyncWaitHandle.WaitOne(2000))
+                {
+                    client.Close();
+                    return "";
+                }
+                client.ReceiveTimeout = 2000;
+                if (client.Connected)
+                {
+                    client.Send(Hex2Bytes(hex));
+                    byte[] buffer = new byte[1024];
+
+                    var length = client.Receive(buffer, SocketFlags.None);
+                    byte[] data = new byte[length];
+                    Array.Copy(buffer, data, length);
+                    res = BitConverter.ToString(data).Replace("-", "");
+
+
+                    client.Disconnect(true);
+                    client.Dispose();
+                }
+                client = null;
+            }
+            catch (Exception ex)
+            {
+                LogHelper.Error(ex.Message, ex);
+            }
+            return res;
+        }
+
+
+        internal static byte[] Hex2Bytes(string hexString)
+        {
+            hexString = hexString.Replace(" ", "");
+            if ((hexString.Length % 2) != 0)
+                hexString += " ";
+            byte[] returnBytes = new byte[hexString.Length / 2];
+            for (int i = 0; i < returnBytes.Length; i++)
+                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
+            return returnBytes;
+        }
+
+
         /// <summary>
         /// 鎺ユ敹PLC娉ㄥ鏈轰俊鍙�         /// </summary>

--
Gitblit v1.9.1