S7Helper.cs
14.8 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
using HHECS.Bll;
using HHECS.Model;
using S7.Net;
using S7.Net.Types;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HHECS.OPC
{
public class S7Helper
{
/// <summary>
/// 将DataItem中的value强制转换后赋值给value,用于读
/// </summary>
/// <param name="plc"></param>
/// <param name="props"></param>
/// <returns></returns>
public static BllResult ConvertPLCToNet(List<EquipmentProp> props)
{
try
{
foreach (var item in props)
{
switch (item.EquipmentTypePropTemplate.DataType)
{
case "DINT":
item.Value = ((uint)item.DataItem.Value).ConvertToInt().ToString();
break;
case "INT":
item.Value = ((ushort)item.DataItem.Value).ConvertToShort().ToString();
break;
case "BYTE":
case "DWORD":
case "BOOL":
case "WORD": item.Value = item.DataItem.Value.ToString(); break;
case "CHAR": item.Value = item.DataItem.Value.ToString().Trim().Replace("$03","").Replace("\u0003", "").Replace("\0", ""); break;
default:
item.Value = item.DataItem.Value.ToString();
break;
}
}
return BllResultFactory.Sucess("成功");
}
catch (Exception ex)
{
return BllResultFactory.Error($"数据类型转换异常:{ex.Message}");
}
}
/// <summary>
/// 将属性value中的值强制转换赋值给dataitem,用于向PLC写入
/// </summary>
/// <param name="plc"></param>
/// <param name="props"></param>
/// <param name="templates"></param>
/// <returns></returns>
public static BllResult ConvertNetToPLC(List<EquipmentProp> props)
{
try
{
foreach (var item in props)
{
switch (item.EquipmentTypePropTemplate.DataType)
{
case "INT":
item.DataItem.Value = Convert.ToInt16(item.Value);
break;
case "DINT":
item.DataItem.Value = Convert.ToInt32(item.Value);
break;
case "WORD":
item.DataItem.Value = Convert.ToUInt16(item.Value);
break;
case "DWORD":
item.DataItem.Value = Convert.ToUInt32(item.Value);
break;
case "CHAR":
item.DataItem.Value = item.Value.ToString();
break;
case "BOOL":
item.DataItem.Value = Convert.ToBoolean(item.Value);
break;
default:
item.DataItem.Value = item.Value.ToString();
break;
}
}
return BllResultFactory.Sucess("成功");
}
catch (Exception ex)
{
return BllResultFactory.Error($"数据类型转换异常:{ex.Message}");
}
}
/// <summary>
/// 从PLC读取值,读取的值赋值给value属性
/// </summary>
/// <param name="plc"></param>
/// <param name="props"></param>
/// <param name="splitCount"></param>
/// <param name="templates"></param>
/// <returns></returns>
public static BllResult PlcSplitRead(Plc plc, List<EquipmentProp> props, int splitCount)
{
string tamp = "";
if (props.Count == 0)
{
return BllResultFactory.Error("待读集合无数据");
}
try
{
var tempList = props.Select(t => t.DataItem).ToList();
int preIndex = 0;
int currentIndex = splitCount;
plc.Open();
while (preIndex < tempList.Count)
{
List<DataItem> temp1 = null;
if (preIndex < tempList.Count)
{
temp1 = tempList.Skip(preIndex).Take(currentIndex).ToList();
preIndex += currentIndex;
}
else
{
temp1 = tempList.Skip(preIndex).ToList();
}
//tamp = props[preIndex].EquipmentTypePropTemplateCode;
plc.ReadMultipleVars(temp1);
}
//赋值属性
var result = ConvertPLCToNet(props);
if (result.Success)
{
return BllResultFactory.Sucess("去读成功.");
}
else
{
return BllResultFactory.Error($"读取失败:{result.Msg}");
}
}
catch (Exception ex)
{
return BllResultFactory.Error($"读取PLC值出错:{ex.Message}");
}
}
/// <summary>
/// 写入PLC值,待写的值为value属性
/// </summary>
/// <param name="plc"></param>
/// <param name="props"></param>
/// <param name="splitCount"></param>
/// <param name="templates"></param>
/// <returns></returns>
public static BllResult PlcSplitWrite(Plc plc, List<EquipmentProp> props, int splitCount)
{
if (props.Count == 0)
{
return BllResultFactory.Error("待写集合无数据");
}
if (splitCount > 20)
{
return BllResultFactory.Error("分批读写个数不能超过20");
}
//先转换
var result = ConvertNetToPLC(props);
if (result.Success)
{
try
{
var tempList = props.Select(t => t.DataItem).ToList();
int preIndex = 0;
int currentIndex = splitCount;
plc.Open();
while (preIndex < tempList.Count)
{
List<DataItem> temp1 = null;
if (preIndex < tempList.Count)
{
temp1 = tempList.Skip(preIndex).Take(currentIndex).ToList();
preIndex += currentIndex;
}
else
{
temp1 = tempList.Skip(preIndex).ToList();
}
plc.Write(temp1.ToArray());
}
return BllResultFactory.Sucess("写入成功");
}
catch (Exception ex)
{
return BllResultFactory.Error($"写入出现异常:{ex.Message}");
}
}
else
{
return BllResultFactory.Error($"写入失败,数据转换异常:{result.Msg}");
}
}
/// <summary>
/// 创建dataitem,这里解决了原本库中bit位bug,支持char,标准格式DB1000.DBC20.30,表示读取DB1000,起始地址20,以char的形式读取,读取30位;
/// char读写时,超出部分会被截断
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public static DataItem FromAddress(string address)
{
Parse(address, out var dataType, out var dbNumber, out var varType, out var startByte,
out var bitNumber, out var count);
return new DataItem
{
DataType = dataType,
DB = dbNumber,
VarType = varType,
StartByteAdr = startByte,
BitAdr = (byte)(bitNumber == -1 ? 0 : bitNumber),
Count = count
};
}
public static void Parse(string input, out DataType dataType, out int dbNumber, out VarType varType, out int address, out int bitNumber, out int count)
{
count = 1;
bitNumber = -1;
dbNumber = 0;
switch (input.Substring(0, 2))
{
case "DB":
string[] strings = input.Split(new char[] { '.' });
if (strings.Length < 2)
throw new BllException("To few periods for DB address");
dataType = DataType.DataBlock;
dbNumber = int.Parse(strings[0].Substring(2));
address = int.Parse(strings[1].Substring(3));
string dbType = strings[1].Substring(0, 3);
switch (dbType)
{
case "DBB":
varType = VarType.Byte;
return;
case "DBW":
varType = VarType.Word;
return;
case "DBD":
varType = VarType.DWord;
return;
case "DBX":
bitNumber = int.Parse(strings[2]);
if (bitNumber > 7)
throw new BllException("Bit can only be 0-7");
varType = VarType.Bit;
return;
case "DBC":
varType = VarType.String;
count = int.Parse(strings[2]);
return;
default:
throw new BllException();
}
case "EB":
// Input byte
dataType = DataType.Input;
dbNumber = 0;
address = int.Parse(input.Substring(2));
varType = VarType.Byte;
return;
case "EW":
// Input word
dataType = DataType.Input;
dbNumber = 0;
address = int.Parse(input.Substring(2));
varType = VarType.Word;
return;
case "ED":
// Input double-word
dataType = DataType.Input;
dbNumber = 0;
address = int.Parse(input.Substring(2));
varType = VarType.DWord;
return;
case "AB":
// Output byte
dataType = DataType.Output;
dbNumber = 0;
address = int.Parse(input.Substring(2));
varType = VarType.Byte;
return;
case "AW":
// Output word
dataType = DataType.Output;
dbNumber = 0;
address = int.Parse(input.Substring(2));
varType = VarType.Word;
return;
case "AD":
// Output double-word
dataType = DataType.Output;
dbNumber = 0;
address = int.Parse(input.Substring(2));
varType = VarType.DWord;
return;
case "MB":
// Memory byte
dataType = DataType.Memory;
dbNumber = 0;
address = int.Parse(input.Substring(2));
varType = VarType.Byte;
return;
case "MW":
// Memory word
dataType = DataType.Memory;
dbNumber = 0;
address = int.Parse(input.Substring(2));
varType = VarType.Word;
return;
case "MD":
// Memory double-word
dataType = DataType.Memory;
dbNumber = 0;
address = int.Parse(input.Substring(2));
varType = VarType.DWord;
return;
default:
switch (input.Substring(0, 1))
{
case "E":
case "I":
// Input
dataType = DataType.Input;
varType = VarType.Bit;
break;
case "A":
case "O":
// Output
dataType = DataType.Output;
varType = VarType.Bit;
break;
case "M":
// Memory
dataType = DataType.Memory;
varType = VarType.Byte;
break;
case "T":
// Timer
dataType = DataType.Timer;
dbNumber = 0;
address = int.Parse(input.Substring(1));
varType = VarType.Timer;
return;
case "Z":
case "C":
// Counter
dataType = DataType.Timer;
dbNumber = 0;
address = int.Parse(input.Substring(1));
varType = VarType.Counter;
return;
default:
throw new BllException(string.Format("{0} is not a valid address", input.Substring(0, 1)));
}
string txt2 = input.Substring(1);
if (txt2.IndexOf(".") == -1)
throw new BllException("To few periods for DB address");
address = int.Parse(txt2.Substring(0, txt2.IndexOf(".")));
bitNumber = int.Parse(txt2.Substring(txt2.IndexOf(".") + 1));
if (bitNumber > 7)
throw new BllException("Bit can only be 0-7");
return;
}
}
}
}