alielie
2025-11-25 006c98e5650b530e5220f279c1e803fefe69d40d
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
110
111
112
113
114
using HslCommunication;
using HslCommunication.Core.Pipe;
using HslCommunication.Profinet.Siemens;
 
namespace Plugin.CurrentSensorControl.Entity
{
    public class S7Context
    {
        private static Dictionary<string, SiemensS7Net> cache = new Dictionary<string, SiemensS7Net>();
        public static Dictionary<string, PLCTagData> dataCache = new Dictionary<string, PLCTagData>();
 
        private static string Ip;
        private static int Port;
 
        public static void Init(string ip, int port)
        {
            Ip = ip;
            Port = port;
        }
 
 
        public static SiemensS7Net Get(string name)
        {
            if (!cache.ContainsKey(name))
            {
                var client = new SiemensS7Net(SiemensPLCS.S1500);
                cache[name] = client;
                PipeTcpNet pipeTcpNet = new PipeTcpNet(Ip, Port);
                pipeTcpNet.ConnectTimeOut = 5000;
                pipeTcpNet.ReceiveTimeOut = 10000;
                client.CommunicationPipe = pipeTcpNet;
                client.ConnectServer();
            }
            return cache[name];
        }
 
        public static bool Connect(string name)
        {
 
            return false;
        }
 
        public static bool GetData(SiemensS7Net api, string name, out object val)
        {
            val = null;
            if (dataCache.ContainsKey(name))
            {
                var data = dataCache[name];
                string address = data.Address;
                if (data.Type == PLCTagType.BOOL)
                {
                    var res = api.ReadBool(address);
                    val = res.Content;
                    return res.IsSuccess;
                }
                else if (data.Type == PLCTagType.SHORT)
                {
                    var res = api.ReadInt16(address);
                    val = res.Content;
                    return res.IsSuccess;
                }
                else if (data.Type == PLCTagType.INT)
                {
                    var res = api.ReadInt32(address);
                    val = res.Content;
                    return res.IsSuccess;
                }
                else if (data.Type == PLCTagType.FLOAT)
                {
                    var res = api.ReadFloat(address);
                    val = res.Content;
                    return res.IsSuccess;
                }
                return false;
            }
            else
            {
                return false;
            }
        }
 
        public static bool WriteData(SiemensS7Net api, string name, object val)
        {
            if (dataCache.ContainsKey(name))
            {
                var data = dataCache[name];
                string address = data.Address;
                OperateResult res;
                switch (data.Type)
                {
                    case PLCTagType.BOOL:
                        res = api.Write(address, (bool)val);
                        break;
                    case PLCTagType.SHORT:
                        res = api.Write(address, (short)val);
                        break;
                    case PLCTagType.INT:
                        res = api.Write(address, (int)val);
                        break;
                    case PLCTagType.FLOAT:
                        res = api.Write(address, (float)val);
                        break;
                    default:
                        return true;
                }
                return res.IsSuccess;
            }
            else
            {
                return false;
            }
        }
    }
}