S7Helper.cs
7.41 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
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, List<EquipmentTypePropTemplate> templates)
{
try
{
var temp = props.Join(templates, t => t.EquipmentTypePropTemplateId, i => i.Id, (prop, template) => new { prop, template }).ToList();
foreach (var item in temp)
{
switch (item.template.DataType)
{
case "BYTE":
case "DWORD":
case "BOOL":
case "WORD": item.prop.Value = item.prop.DataItem.Value.ToString(); break;
case "CHAR": item.prop.Value = ConverHelper.ASCIIToString((short[])item.prop.DataItem.Value).Trim().Replace("\u0003", ""); break;
default:
item.prop.Value = item.prop.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,List<EquipmentTypePropTemplate> templates)
{
try
{
var temp = props.Join(templates, t => t.EquipmentTypePropTemplateId, i => i.Id, (prop, template) => new { prop, template }).ToList();
foreach (var item in temp)
{
switch (item.template.DataType)
{
case "INT":
item.prop.DataItem.Value = Convert.ToInt16(item.prop.Value);
break;
case "DINT":
item.prop.DataItem.Value = Convert.ToInt32(item.prop.Value);
break;
case "WORD":
item.prop.DataItem.Value = Convert.ToUInt16(item.prop.Value);
break;
case "DWORD":
item.prop.DataItem.Value = Convert.ToUInt32(item.prop.Value);
break;
case "CHAR":
item.prop.DataItem.Value = ConverHelper.StringToASCII(item.prop.Value);
break;
default:
item.prop.DataItem.Value = item.prop.Value.BinStringToByte();
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, List<EquipmentTypePropTemplate> templates)
{
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();
}
plc.ReadMultipleVars(temp1);
}
//赋值属性
var result = ConvertPLCToNet(props, templates);
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, List<EquipmentTypePropTemplate> templates)
{
if (props.Count == 0)
{
return BllResultFactory.Error("待写集合无数据");
}
//先转换
var result = ConvertNetToPLC(props, templates);
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}");
}
}
}
}