1
czw
8 天以前 b520c35d65a71fcbee438670b33dc47eacc95a15
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using EasyModbus;
using GZ.Device.PLC;
using GZ.Modular.Redis;
using ServiceStack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;
using System.Runtime.CompilerServices;
using ServiceStack.Text;
using System.Threading;
using System.Windows.Interop;
using Newtonsoft.Json.Linq;
using System.Windows.Input;
using System.Xml.Linq;
 
namespace GZ.Projects.mttest.代码
{
    internal static class MBTCP
    {
        static Dictionary<string, ModbusClient> mcs = new Dictionary<string, ModbusClient>();
 
        public static ModbusClient initTCP(PlcBase plc)
        {
            if (plc == null)
                return null;
            else if (plc.State == PlcRunState.Runing)
            {
                plc.Stop();
                return null;
            }
            else if (plc.State == PlcRunState.WaitStop)
            {
                return null;
            }
            if (!mcs.TryGetValue(plc.Config.IP, out ModbusClient client))
            {
                var serverUrl = plc.Config.IP;
                string[] array = serverUrl.Split(':');
                int port = int.Parse(array[1]);
                client = new ModbusClient(array[0], port);
                client.Connect();
                mcs.Add(plc.Config.IP, client);
            }
 
            if (!client.Connected)
            {
                Console.WriteLine("连接成功");
            }
            //return client;
 
 
            GandWRedis(plc, client);
            return client;
        }
 
        private static void GandWRedis(PlcBase plc, ModbusClient client)
        {
            if (client != null)
            {
                List<PlcParam> list = (plc.Config.ParamList) ?? new List<PlcParam>();
 
                foreach (var p in list.GroupBy(x => x.AddrType))
                {
                    int max = p.ToList().Max(x => int.Parse(x.Address));
                    int min = p.ToList().Min(x => int.Parse(x.Address));
 
                    try
                    {
                        int adr = min;
                        int len = max - min + 1;
 
                        //int[] list21= client.ReadHoldingRegisters(41201, 3); // 从地址0开始读取10个寄存器
                        int[] list2 = client.ReadHoldingRegisters(min, len);
                        if (list2 != null && list2.Length > 0)
                        {
                            foreach (PlcParam item2 in p)
                            {
                                object value = PlcHelper.ChangeType(list2[int.Parse(item2.Address) - min], item2.DataType);
                                RedisHelper.Add(item2.FullName ?? "", value, out var msg2);
                                string key = item2.FullName + "_LAST";
                                if (item2.Trigger && !RedisHelper.Exists(key, out msg2))
                                {
                                    RedisHelper.Add(key, (object)0, out msg2);
                                }
                            }
                        }
                        Thread.Sleep(1000);
                        foreach (var item in p.ToList())
                        {
                            int obj = RedisHelper.Get<int>(item.FullName, out string msg);
 
                            Console.WriteLine(item.FullName + ":::" + obj);
                        }
                        client.WriteSingleRegister(41201, DateTime.Now.Second); // 向地址0写入值1234
                    }
                    catch (Exception ex)
                    {
                        client.Connect();
                        Console.WriteLine(ex.Message + "" + p.Key + ">>" + ex.StackTrace);
                    }
                }
 
            }
        }
 
        public static bool GetinRedis(this PlcBase plc)
        {
            var client = initTCP(plc);
            if (client != null)
            {
                List<PlcParam> list = (plc.Config.ParamList) ?? new List<PlcParam>();
 
                foreach (var p in list.GroupBy(x => x.AddrType))
                {
                    int max = p.ToList().Max(x => int.Parse(x.Address));
                    int min = p.ToList().Min(x => int.Parse(x.Address));
 
                    try
                    {
                        int adr = min;
                        int len = max - min + 1;
                        int[] list2 = client.ReadHoldingRegisters(min, len); // 从地址0开始读取10个寄存器
 
                        if (list2 != null && list2.Length > 0)
                        {
                            foreach (PlcParam item2 in p)
                            {
                                object value = PlcHelper.ChangeType(list2[int.Parse(item2.Address) - min], item2.DataType);
                                RedisHelper.Add(item2.FullName ?? "", value, out var msg2);
                                string key = item2.FullName + "_LAST";
                                if (item2.Trigger && !RedisHelper.Exists(key, out msg2))
                                {
                                    RedisHelper.Add(key, (object)0, out msg2);
                                }
                            }
                        }
                        foreach (PlcParam item2 in p)
                        {
                            var key = item2.FullName + "_Write";
                            if (RedisHelper.Exists(key, out string msg2))
                            {
                                var val = RedisHelper.Get<int>(key, out msg2);
 
                                client.WriteSingleRegister(int.Parse(item2.Address), val);
                                RedisHelper.Remove(key, out msg2);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        client.Connect();
                        Console.WriteLine(ex.Message + "" + p.Key + ">>" + ex.StackTrace);
                    }
                }
                return true;
            }
            return false;
        }
 
        public static bool SetRedis(string key, string value)
        {
            try
            {
 
                key = key + "_Write";
                if (!RedisHelper.Exists(key, out string msg2))
                {
                    RedisHelper.Add(key, value, out msg2);
                }
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + ex.StackTrace);
                return false;
            }
        }
        public static bool SetinPlc(this PlcBase plc, int address, int value)
        {
            var client = initTCP(plc);
            if (client != null)
            {
                try
                {
                    client.WriteSingleRegister(address, value);
                    return true;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message + $">{address}-{value}>" + ex.StackTrace);
                }
            }
            return false;
        }
 
    }
}