alielie
2026-04-23 ef52fd6fd07508883dce4eb002acda59f0169e59
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using PLCHelper.Entity;
using System.Collections.Generic;
 
namespace PLCHelper
{
    public class NXI_5010Helper
    {
        private static Dictionary<string, RestTcpClient> cache = new Dictionary<string, RestTcpClient>();
 
        internal static RestTcpClient GetClient(string ip, int port)
        {
            var key = $"{ip}:{port};";
            if (!cache.ContainsKey(key))
            {
                var client = new RestTcpClient(ip, port);
                client.ReceiveTimeout = 500;
                client.ConnectServer();
                if (client.Request("*RST", out string res, out string errMsg) && res == "RST")
                {
                    if (client.Request("*TSS", out res, out errMsg))
                    {
 
                    }
                }
                cache[key] = client;
            }
            return cache[key];
        }
 
        internal static bool ParseConfig(string address, out string ip, out int port, out string slot, out string com, out string chn)
        {
            try
            {
                var arr = address.Split(',');
                ip = arr[0];
                port = int.Parse(arr[1]);
                slot = arr[2];
                com = arr[3];
                chn = arr[4];
                return true;
            }
            catch
            {
                ip = null;
                port = -1;
                slot = null;
                com = null;
                chn = null;
                return false;
            }
        }
 
        internal static bool ReadData(PLCTagData data, string name, out object val)
        {
            val = null;
            string address = data.Address;
            if (!ParseConfig(address, out string ip, out int port, out string slot, out string com, out string chn))
            {
                return false;
            }
            var api = GetClient(ip, port);
            if (data.Type == PLCTagType.BOOL)
            {
                var send = $"MULT::SLOT {slot}:GET G{com}C{chn}";
                var res = api.ReadBool(send);
                val = res.Content;
                return res.IsSuccess;
            }
            return false;
        }
 
        internal static bool WriteData(PLCTagData data, string name, object val)
        {
            val = null;
            string address = data.Address;
            if (!ParseConfig(address, out string ip, out int port, out string slot, out string com, out string chn))
            {
                return false;
            }
            var api = GetClient(ip, port);
            if (data.Type == PLCTagType.BOOL)
            {
                string send = string.Empty;
                if ((bool)val == true)
                {
                    send = $"MULT::SLOT {slot}:SET G{com}C{chn}";
                    var res = api.Write(send, true);
                    return res.IsSuccess;
                }
                else
                {
                    send = $"MULT::SLOT {slot}:RESE";
                    var res = api.Write(send, true);
                    return res.IsSuccess;
                }
            }
            return false;
        }
 
        internal static void Release()
        {
            foreach (var client in cache.Values)
            {
                client.Dispose();
            }
            cache.Clear();
        }
    }
}