11
jinxin
2025-06-23 a7b168385463b6fe2734d753f631a692f69eacdf
C#/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>