From 9d1660270272180cd71147d6ddd53d1382643d2e Mon Sep 17 00:00:00 2001
From: wenxiangchao <wenxiangchao@ihengxin.com.cn>
Date: 星期一, 02 十一月 2026 18:00:14 +0800
Subject: [PATCH] merge: 并入远程 Initial commit 作为历史根节点

---
 OPCClient/OPCClient.cs |  664 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 664 insertions(+), 0 deletions(-)

diff --git a/OPCClient/OPCClient.cs b/OPCClient/OPCClient.cs
new file mode 100644
index 0000000..b135858
--- /dev/null
+++ b/OPCClient/OPCClient.cs
@@ -0,0 +1,664 @@
+锘縰sing ConnectionUtil;
+using LT.Utils.Json;
+using Newtonsoft.Json;
+using SuperSocketServer.Common;
+using System;
+using System.Collections.Generic;
+using System.Net;
+using System.Net.Sockets;
+using System.Text;
+using System.Threading;
+
+namespace OPCConnection
+{
+    public class OPCClient : SCADAApi
+    {
+        private Socket ClientSocket = null;
+
+        /// <summary>
+        /// 杩炴帴瓒呮椂浜嬩欢ms
+        /// </summary>
+        public int _ConnectTimeout = 20000;
+        private int Size = 0x989680;
+        private Dictionary<int, string> paraMPairs = new Dictionary<int, string>();
+
+
+        /// <summary>
+        /// IP杩炴帴Scada鏈嶅姟鍣�
+        /// </summary>
+        /// <param name="IP">MES鏈嶅姟鍣↖P</param>
+        /// <param name="Port"> MES鏈嶅姟鍣≒ort鍙�</param>
+        /// <param name="LocalIP">鏈湴杩炴帴IP</param>
+        /// <param name="LocalPort">鏈湴杩炴帴绔彛</param>
+        /// <returns></returns>
+        public bool Connect(string IP, int Port, string LocalIP, int LocalPort)
+        {
+            try
+            {
+                this.ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
+                IPAddress address = IPAddress.Parse(IP);
+                IPEndPoint point = new IPEndPoint(IPAddress.Parse(LocalIP), LocalPort);
+                IPEndPoint remoteEP = new IPEndPoint(address, Port);
+                this.ClientSocket.Connect(remoteEP);
+                return true;
+            }
+            catch (Exception)
+            {
+                return false;
+            }
+        }
+
+        /// <summary>
+        /// 杩炴帴Scada绠$悊鏈嶅姟鍣�
+        /// </summary>
+        /// <param name="IP"> Scada鏈嶅姟鍣↖P</param>
+        /// <param name="Port">ScadPort鍙�</param>
+        /// <param name="LocalPort">鏈湴杩炴帴绔彛</param>
+        /// <param name="errMsg"></param>
+        /// <param name="socket"></param>
+        /// <returns></returns>
+        public bool Connect2(string IP, int Port, int LocalPort, out string errMsg, Socket socket = null)
+        {
+            try
+            {
+                errMsg = string.Empty;
+                if (!this.TcpPing(IP, Port, out errMsg))
+                {
+                    throw new Exception(errMsg);
+                }
+                if (socket != null)
+                {
+                    this.ClientSocket = socket;
+                }
+                else
+                {
+                    this.ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
+                }
+                IPAddress address = IPAddress.Parse(IP);
+                this.ClientSocket.Connect(address, Port);
+                if (!this.DoLogin("admin", "LT123456", out string str))
+                {
+                    throw new Exception(str);
+                }
+                return true;
+            }
+            catch (Exception exception)
+            {
+                errMsg = "杩炴帴SCADA鏈嶅姟澶辫触:" + exception.Message;
+                return false;
+            }
+        }
+
+        /// <summary>
+        ///  杩炴帴Scada绠$悊鏈嶅姟鍣�
+        /// </summary>
+        /// <param name="IP">ScadaManagerIP</param>
+        /// <param name="Port">ScadaManagerPort鍙�</param>
+        /// <param name="LocalPort">鏈湴杩炴帴绔彛</param>
+        /// <param name="errMsg"></param>
+        /// <returns></returns>
+        public bool ConnectManager(string IP, int Port, int LocalPort, out string errMsg)
+        {
+            try
+            {
+                errMsg = string.Empty;
+                if (!this.TcpPing(IP, Port, out errMsg))
+                {
+                    throw new Exception(errMsg);
+                }
+                this.ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
+                IPAddress address = IPAddress.Parse(IP);
+                this.ClientSocket.Connect(address, Port);
+                return true;
+            }
+            catch (Exception exception)
+            {
+                errMsg = "杩炴帴SCADA鏈嶅姟澶辫触:" + exception.Message;
+                return false;
+            }
+        }
+
+        /// <summary>
+        /// 鏂紑鏈嶅姟鍣ㄨ繛鎺�
+        /// </summary>
+        public void DisConnect()
+        {
+            try
+            {
+                if (this.ClientSocket != null)
+                {
+                    //this.ClientSocket.Shutdown(SocketShutdown.Both);
+                    this.ClientSocket.Close();
+                }
+            }
+            catch (Exception exception)
+            {
+                throw new Exception("鏂紑SCADA鏈嶅姟鍣ㄥけ璐�:" + exception.Message);
+            }
+        }
+
+        /// <summary>
+        /// 閲婃斁璧勬簮
+        /// </summary>
+        public void Dispose()
+        {
+            if (this.ClientSocket != null)
+            {
+                this.ClientSocket.Dispose();
+            }
+        }
+
+        /// <summary>
+        /// 鎵归噺鍐欏鍔犲弬鏁�
+        /// </summary>
+        /// <param name="id"></param>
+        /// <param name="par"></param>
+        /// <returns></returns>
+        public bool DoAddParWriteMultiple(int id, string par)
+        {
+            if (this.paraMPairs.ContainsKey(id))
+            {
+                return false;
+            }
+            this.paraMPairs.Add(id, par);
+            return true;
+        }
+
+        /// <summary>
+        /// 鎹㈠瀷
+        /// </summary>
+        /// <param name="_ProductLineName"></param>
+        /// <param name="_ProductName"></param>
+        /// <param name="errMsg"></param>
+        /// <param name="_WorkStationName"></param>
+        /// <returns></returns>
+        public bool DoDoChangeModel(string _ProductLineName, string _ProductName, out string errMsg, string _WorkStationName = null)
+        {
+            try
+            {
+                var type = new
+                {
+                    command = 7,
+                    ProductLineName = _ProductLineName,
+                    ProductName = _ProductName,
+                    WorkStationName = _WorkStationName
+                };
+                string resquestStr = this.SendSocketMessage(JsonExtensions.ToJson(type, false, false, false));
+                return this.ResquestResult(resquestStr, out _, out errMsg);
+            }
+            catch (Exception exception)
+            {
+                errMsg = exception.Message;
+                return false;
+            }
+        }
+
+        /// <summary>
+        /// 鎵归噺璇诲��
+        /// </summary>
+        /// <param name="_Id"></param>
+        /// <returns></returns>
+        public string[,] DoFctReadMultiple(int[] _Id)
+        {
+            string[,] strArray2;
+            try
+            {
+                var type = new
+                {
+                    command = 2,
+                    comefrom = 1,
+                    paraM = _Id
+                };
+                string resquestStr = this.SendSocketMessage(JsonExtensions.ToJson(type, false, false, false));
+                if (this.ResquestResult(resquestStr, out Result result, out _))
+                {
+                    Dictionary<string, string> dictionary = JsonUtil.ToObject<Dictionary<string, string>>(result.result.ToString());
+                    string[,] strArray = new string[dictionary.Count, 2];
+                    int num = 0;
+                    foreach (KeyValuePair<string, string> pair in dictionary)
+                    {
+                        strArray[num, 0] = pair.Key;
+                        strArray[num, 1] = pair.Value;
+                        num++;
+                    }
+                    return strArray;
+                }
+                strArray2 = null;
+            }
+            catch (Exception exception)
+            {
+                throw new Exception(exception.Message);
+            }
+            return strArray2;
+        }
+
+        /// <summary>
+        /// 鍗曚竴璇诲��
+        /// </summary>
+        /// <param name="_name"></param>
+        /// <param name="result"></param>
+        /// <param name="errMsg"></param>
+        /// <returns></returns>
+        public bool DoFctReadSingle(string _name, out string result, out string errMsg)
+        {
+            result = string.Empty;
+            try
+            {
+                var type = new
+                {
+                    command = 0x31,
+                    id = _name,
+                    comefrom = 1
+                };
+                string resquestStr = this.SendSocketMessage(JsonExtensions.ToJson(type, false, false, false));
+                if (this.ResquestResult(resquestStr, out Result result2, out errMsg))
+                {
+                    result = result2.result?.ToString();
+                    return true;
+                }
+                return false;
+            }
+            catch (Exception exception)
+            {
+                errMsg = exception.Message;
+                return false;
+            }
+            finally
+            {
+                GC.Collect();
+            }
+        }
+
+        /// <summary>
+        /// 鍗曚竴鍐欏��
+        /// </summary>
+        /// <param name="errMsg"></param>
+        /// <returns></returns>
+        public bool DoFctWriteMultiple(out string errMsg)
+        {
+            bool flag2;
+            errMsg = string.Empty;
+            try
+            {
+                if (this.ToFctWriteMultiple(this.paraMPairs, out errMsg))
+                {
+                    return true;
+                }
+                flag2 = false;
+            }
+            catch (Exception exception)
+            {
+                errMsg = exception.Message;
+                flag2 = false;
+            }
+            finally
+            {
+                this.InitParWriteMultiple();
+            }
+            return flag2;
+        }
+
+        /// <summary>
+        /// 鍐欏崟涓�鍊�
+        /// </summary>
+        /// <param name="_name"></param>
+        /// <param name="_value"></param>
+        /// <param name="errMsg"></param>
+        /// <returns></returns>
+        public bool DoFctWriteSingle(string _name, string _value, out string errMsg)
+        {
+            try
+            {
+                var type = new
+                {
+                    command = 5,
+                    id = _name,
+                    value = _value
+                };
+                string resquestStr = this.SendSocketMessage(JsonExtensions.ToJson(type, false, false, false));
+                return this.ResquestResult(resquestStr, out _, out errMsg);
+            }
+            catch (Exception exception)
+            {
+                errMsg = exception.Message;
+                return false;
+            }
+        }
+
+        /// <summary>
+        /// mqtt鍙戝竷娑堟伅
+        /// </summary>
+        /// <param name="topic">涓婚</param>
+        /// <param name="_value">鍊�</param>
+        /// <param name="errMsg">閿欒淇℃伅></param>
+        /// <returns></returns>
+        public bool DoMqttPublish(string topic, string _value, out string errMsg)
+        {
+            try
+            {
+                var type = new
+                {
+                    command = 12,
+                    Tag = topic,
+                    value = _value
+                };
+                string resquestStr = this.SendSocketMessage(JsonExtensions.ToJson(type, false, false, false));
+                return this.ResquestResult(resquestStr, out _, out errMsg);
+            }
+            catch (Exception exception)
+            {
+                errMsg = exception.Message;
+                return false;
+            }
+        }
+
+        /// <summary>
+        /// 
+        /// </summary>
+        /// <param name="_user"></param>
+        /// <param name="_password"></param>
+        /// <param name="errMsg"></param>
+        /// <returns></returns>
+        public bool DoLogin(string _user, string _password, out string errMsg)
+        {
+            try
+            {
+                var type = new
+                {
+                    command = 11,
+                    user = _user,
+                    password = _password
+                };
+                string resquestStr = this.SendSocketMessage(JsonExtensions.ToJson(type, false, false, false));
+                return this.ResquestResult(resquestStr, out _, out errMsg);
+            }
+            catch (Exception exception)
+            {
+                errMsg = exception.Message;
+                return false;
+            }
+        }
+
+        /// <summary>
+        /// 閲嶅惎DataServer鏈嶅姟鍣�
+        /// </summary>
+        /// <param name="errMsg"></param>
+        /// <returns></returns>
+        public bool DoReSETDataServer(out string errMsg)
+        {
+            try
+            {
+                var type = new
+                {
+                    command = 10
+                };
+                string resquestStr = this.SendSocketMessage(JsonExtensions.ToJson(type, false, false, false));
+                return this.ResquestResult(resquestStr, out _, out errMsg);
+            }
+            catch (Exception exception)
+            {
+                errMsg = exception.Message;
+                return false;
+            }
+        }
+
+        /// <summary>
+        ///  鑾峰彇鍔ㄦ�両P
+        /// </summary>
+        /// <returns></returns>
+        public string getLocalIP()
+        {
+            string str2 = null;
+            IPHostEntry hostByName = Dns.GetHostByName(Dns.GetHostName());
+            int length = hostByName.AddressList.Length;
+            for (int i = 0; i < length; i++)
+            {
+                IPAddress address = hostByName.AddressList[i];
+                str2 = Convert.ToString(address);
+                if (str2.Substring(0, 5) == "10.10")
+                {
+                    return str2;
+                }
+            }
+            return str2;
+        }
+
+        /// <summary>
+        /// 
+        /// </summary>
+        /// <param name="_paraMPairs"></param>
+        /// <param name="errMsg"></param>
+        /// <returns></returns>
+        public short[,] GetLowerComputerStatus(short[] _paraMPairs, out string errMsg)
+        {
+            try
+            {
+                var type = new
+                {
+                    command = 9,
+                    paraM = _paraMPairs
+                };
+                string resquestStr = this.SendSocketMessage(JsonExtensions.ToJson(type, false, false, false));
+                if (this.ResquestResult(resquestStr, out Result result, out errMsg))
+                {
+                    Dictionary<short, short> dictionary = JsonUtil.ToObject<Dictionary<short, short>>(result.result.ToString());
+                    short[,] numArray = new short[dictionary.Count, 2];
+                    int num = 0;
+                    foreach (KeyValuePair<short, short> pair in dictionary)
+                    {
+                        numArray[num, 0] = pair.Key;
+                        numArray[num, 1] = pair.Value;
+                        num++;
+                    }
+                    return numArray;
+                }
+                return null;
+            }
+            catch (Exception exception)
+            {
+                errMsg = exception.Message;
+                return null;
+            }
+        }
+
+        /// <summary>
+        /// 鍒濆鍖栨壒閲忓啓鍙傛暟
+        /// </summary>
+        public void InitParWriteMultiple()
+        {
+            this.paraMPairs.Clear();
+        }
+
+        /// <summary>
+        /// 妫�鏌ヤ笌LTSCADA鏄惁鏂紑杩炴帴浜�
+        /// </summary>
+        /// <param name="errMsg"></param>
+        /// <returns></returns>
+        public bool IsConnected(out string errMsg)
+        {
+            try
+            {
+                var type = new
+                {
+                    command = 6
+                };
+                string resquestStr = this.SendSocketMessage(JsonExtensions.ToJson(type, false, false, false));
+                return this.ResquestResult(resquestStr, out _, out errMsg);
+            }
+            catch (Exception exception)
+            {
+                errMsg = exception.Message;
+                return false;
+            }
+        }
+
+        /// <summary>
+        /// 
+        /// </summary>
+        /// <typeparam name="T"></typeparam>
+        /// <param name="Pars"></param>
+        /// <returns></returns>
+        private static string ParsToString<T>(T Pars) =>
+            null;
+
+        /// <summary>
+        /// 杩斿洖璇锋眰淇℃伅
+        /// </summary>
+        /// <param name="ResquestStr"></param>
+        /// <param name="JResult"></param>
+        /// <param name="errMsg"></param>
+        /// <returns></returns>
+        private bool ResquestResult(string ResquestStr, out Result JResult, out string errMsg)
+        {
+            errMsg = string.Empty;
+            JResult = null;
+            try
+            {
+                JResult = JsonConvert.DeserializeObject<Result>(ResquestStr);
+                if (!JResult.success)
+                {
+                    errMsg = "璇锋眰澶辫触:" + JResult.message;
+                    return false;
+                }
+                return true;
+            }
+            catch (Exception exception)
+            {
+                errMsg = JResult?.message + " --- " + exception.Message;
+                return false;
+            }
+        }
+
+
+        /// <summary>
+        /// 鎺ユ敹鏁版嵁瓒呮椂鏃堕棿
+        /// </summary>
+        public uint TimeRecOut { set; get; } = 20000;
+
+        /// <summary>
+        /// 绾跨▼閿�
+        /// </summary>
+        private readonly object objLock = new object();
+
+        /// <summary>
+        /// 鍙戦�佽姹備俊鎭�
+        /// </summary>
+        /// <param name="Cmd"></param>
+        /// <returns></returns>
+        private string SendSocketMessage(string Cmd)
+        {
+            string str2;
+            try
+            {
+                string str = "";
+                byte[] bytes = Encoding.UTF8.GetBytes(Cmd + "\r\n");
+                lock (objLock)
+                {
+                    int available = this.ClientSocket.Available;
+                    if (available > 0)
+                    {
+                        byte[] buffer2 = new byte[available];
+                        this.ClientSocket.Receive(buffer2, 0, available, SocketFlags.None);
+                    }
+                    this.ClientSocket.Send(bytes);
+                    bool flag = false;
+                    int tickCount = Environment.TickCount;
+                    int size = this.ClientSocket.Available;
+                    bool flag2 = false;
+                    while (((size < this.Size) && !flag) && !(flag2 = str.Contains("\r\n")))
+                    {
+                        Thread.Sleep(2);
+                        size = this.ClientSocket.Available;
+                        if ((size > this.Size) || (str.Length > this.Size))
+                        {
+                            throw new Exception($"鎺ユ敹鏁版嵁瓒呰繃璁惧畾鍊納this.Size}BYTE锛�");
+                        }
+                        flag = (Environment.TickCount - tickCount) > TimeRecOut;
+                        if (flag)
+                        {
+                            if (size > 0)
+                            {
+                                byte[] buffer3 = new byte[size];
+                                this.ClientSocket.Receive(buffer3, 0, size, SocketFlags.None);
+                            }
+                            throw new Exception("鎺ユ敹鏁版嵁瓒呮椂锛�");
+                        }
+                        if (!flag && (size > 0))
+                        {
+                            byte[] buffer4 = new byte[size];
+                            this.ClientSocket.Receive(buffer4, 0, size, SocketFlags.None);
+                            str = str + Encoding.UTF8.GetString(buffer4);
+                        }
+                    }
+                }
+                str2 = str;
+            }
+            catch (Exception exception)
+            {
+                throw new Exception(exception.Message);
+            }
+            return str2;
+        }
+
+        /// <summary>
+        /// TcpPing
+        /// </summary>
+        /// <param name="Host"></param>
+        /// <param name="Port"></param>
+        /// <param name="errMsg"></param>
+        /// <returns></returns>
+        private bool TcpPing(string Host, int Port, out string errMsg)
+        {
+            bool flag = false;
+            errMsg = string.Empty;
+            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
+            try
+            {
+                bool flag2 = socket.BeginConnect(Host, Port, null, null).AsyncWaitHandle.WaitOne(this._ConnectTimeout, true);
+                if (!flag2)
+                {
+                    errMsg = "errTCPConnectionFailed";
+                }
+                flag = flag2;
+            }
+            catch
+            {
+                errMsg = "errTCPConnectionFailed";
+                flag = false;
+            }
+            socket.Close();
+            return flag;
+        }
+
+        /// <summary>
+        /// 鎵归噺鍐�
+        /// </summary>
+        /// <param name="_paraMPairs">鍙傛暟鍒楄〃</param>
+        /// <param name="errMsg">閿欒淇℃伅</param>
+        /// <returns>澶勭悊缁撴灉</returns>
+        public bool ToFctWriteMultiple(Dictionary<int, string> _paraMPairs, out string errMsg)
+        {
+            try
+            {
+                var type = new
+                {
+                    command = 15,
+                    paraM = _paraMPairs
+                };
+                string resquestStr = this.SendSocketMessage(JsonExtensions.ToJson(type, false, false, false));
+                return this.ResquestResult(resquestStr, out _, out errMsg);
+            }
+            catch (Exception exception)
+            {
+                errMsg = exception.Message;
+                return false;
+            }
+        }
+
+        public bool Connect(object config, out string errMsg)
+        {
+            errMsg = null;
+            return false;
+        }
+    }
+}

--
Gitblit v1.9.1