SiemensHelper.cs
13.6 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
using Hh.Mes.Common.Json;
using Hh.Mes.POJO.BllModel;
using Hh.Mes.POJO.Entity;
using HslCommunication.Core;
using System;
using System.Text;
namespace HHECS.Model.PLCHelper.PLCComponent.HslComponent
{
/// <summary>
/// 西门子PLC帮助类
/// </summary>
public class SiemensHelper
{
/// <summary>
/// 转换类
/// </summary>
private static ReverseBytesTransform reverseBytesTransform = new ReverseBytesTransform();
public static BllResult<HslSiemensDataEntity> ParseAddress(base_equipment_prop prop)
{
HslSiemensDataEntity entity = new HslSiemensDataEntity();
entity.OPCAddressId = prop.id;
string address = prop.address;
try
{
if (address[0] == 'I')
{
entity.Area = SiemensArea.I;
return BllResultFactory<HslSiemensDataEntity>.Error("暂时不支持I区访问");
}
else if (address[0] == 'Q')
{
entity.Area = SiemensArea.Q;
return BllResultFactory<HslSiemensDataEntity>.Error("暂时不支持Q区访问");
}
else if (address[0] == 'M')
{
entity.Area = SiemensArea.M;
entity.BlockIndex = 0;
entity.DataAmount = 1;
string temp = address.Substring(1);
if (temp.StartsWith("DINT"))
{
temp = temp.Substring(4);
entity.DataOffset = Convert.ToInt32(temp);
entity.DataType = PLCDataType.DINT;
}
else if (temp.StartsWith("INT"))
{
temp = temp.Substring(3);
entity.DataOffset = Convert.ToInt32(temp);
entity.DataType = PLCDataType.INT;
}
else if (temp.StartsWith("D"))
{
temp = temp.Substring(1);
entity.DataOffset = Convert.ToInt32(temp);
entity.DataType = PLCDataType.DWORD;
}
else if (temp.StartsWith("W"))
{
temp = temp.Substring(1);
entity.DataOffset = Convert.ToInt32(temp);
entity.DataType = PLCDataType.WORD;
}
else if (temp.StartsWith("X"))
{
temp = temp.Substring(1);
var adss = temp.Split('.');
entity.DataOffset = Convert.ToInt32(adss[0]);
entity.BitOffset = Convert.ToInt32(adss[1]);
entity.DataType = PLCDataType.BOOL;
}
else if (temp.StartsWith("CHAR"))
{
temp = temp.Substring(4);
var adss = temp.Split(',');
entity.DataOffset = Convert.ToInt32(adss[0]);
entity.DataAmount = Convert.ToInt32(adss[1]);
entity.DataType = PLCDataType.CHAR;
}
else
{
return BllResultFactory<HslSiemensDataEntity>.Error("解析错误,无效数据类型");
}
return BllResultFactory<HslSiemensDataEntity>.Success(entity);
}
else if (address[0] == 'D' || address.Substring(0, 2) == "DB")
{
if (address[1] == 'B')
{
entity.Area = SiemensArea.DB;
string temp = address.Substring(2);
if (temp.Contains("W"))
{
var ads = temp.Split('W');
entity.BlockIndex = Convert.ToInt32(ads[0]);
entity.DataOffset = Convert.ToInt32(ads[1]);
entity.DataAmount = 1;
entity.DataType = PLCDataType.WORD;
}
else if (temp.Contains("DINT"))
{
var ads = temp.Split(new char[] { 'D', 'I', 'N', 'T' }, StringSplitOptions.RemoveEmptyEntries);
entity.BlockIndex = Convert.ToInt32(ads[0]);
entity.DataOffset = Convert.ToInt32(ads[1]);
entity.DataAmount = 1;
entity.DataType = PLCDataType.DINT;
}
else if (temp.Contains("INT"))
{
var ads = temp.Split(new char[] { 'I', 'N', 'T' }, StringSplitOptions.RemoveEmptyEntries);
entity.BlockIndex = Convert.ToInt32(ads[0]);
entity.DataOffset = Convert.ToInt32(ads[1]);
entity.DataAmount = 1;
entity.DataType = PLCDataType.INT;
}
else if (temp.Contains("D"))
{
var ads = temp.Split('D');
entity.BlockIndex = Convert.ToInt32(ads[0]);
entity.DataOffset = Convert.ToInt32(ads[1]);
entity.DataAmount = 1;
entity.DataType = PLCDataType.DWORD;
}
else if (temp.Contains("B"))
{
var ads = temp.Split('B');
entity.BlockIndex = Convert.ToInt32(ads[0]);
entity.DataOffset = Convert.ToInt32(ads[1]);
entity.DataAmount = 1;
entity.DataType = PLCDataType.BYTE;
}
else if (temp.Contains("X"))
{
var ads = temp.Split('X');
entity.BlockIndex = Convert.ToInt32(ads[0]);
var adss = ads[1].Split('.');
entity.DataOffset = Convert.ToInt32(adss[0]);
entity.BitOffset = Convert.ToInt32(adss[1]);
entity.DataAmount = 1;
entity.DataType = PLCDataType.BOOL;
}
else if (temp.Contains("CHAR"))
{
var ads = temp.Split(new char[] { 'C', 'H', 'A', 'R' }, StringSplitOptions.RemoveEmptyEntries);
entity.BlockIndex = Convert.ToInt32(ads[0]);
var adss = ads[1].Split(',');
entity.DataOffset = Convert.ToInt32(adss[0]);
entity.DataAmount = Convert.ToInt32(adss[1]);
entity.DataType = PLCDataType.CHAR;
}
else
{
return BllResultFactory<HslSiemensDataEntity>.Error("解析错误,无效数据类型");
}
}
else
{
return BllResultFactory<HslSiemensDataEntity>.Error("暂时只支持DB块访问");
}
return BllResultFactory<HslSiemensDataEntity>.Success(entity);
}
else if (address[0] == 'T')
{
entity.Area = SiemensArea.T;
return BllResultFactory<HslSiemensDataEntity>.Error("暂时不支持T区访问");
}
else if (address[0] == 'C')
{
entity.Area = SiemensArea.C;
return BllResultFactory<HslSiemensDataEntity>.Error("暂时不支持C区访问");
}
else if (address[0] == 'V')
{
entity.Area = SiemensArea.V;
return BllResultFactory<HslSiemensDataEntity>.Error("暂时不支持V区访问");
//result.Content1 = 0x84;
//result.Content3 = 1;
//result.Content2 = CalculateAddressStarted(address.Substring(1));
}
else
{
return BllResultFactory<HslSiemensDataEntity>.Error("未知的地址开头");
}
}
catch (Exception ex)
{
return BllResultFactory<HslSiemensDataEntity>.Error(ex.Message);
}
}
/// <summary>
/// 根据数据类型获取其对应的字节长度,bool型为1个字节
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static ushort GetByteSize(PLCDataType type)
{
switch (type)
{
case PLCDataType.BYTE:
return 1;
case PLCDataType.BOOL:
return 1;
case PLCDataType.DWORD:
return 4;
case PLCDataType.WORD:
return 2;
case PLCDataType.INT:
return 2;
case PLCDataType.DINT:
return 4;
case PLCDataType.CHAR:
return 1;
default:
return 1;
}
}
/// <summary>
/// 数据转换,byte[]到string
/// </summary>
/// <param name="dataType"></param>
/// <param name="buffer"></param>
/// <returns></returns>
public static BllResult<string> TransferBufferToString(HslSiemensDataEntity data)
{
try
{
switch (data.DataType)
{
case PLCDataType.BYTE:
//字节转成int处理
return BllResultFactory<string>.Success(((int)data.Buffer[0]).ToString(), null);
case PLCDataType.BOOL:
//对于bool类型,我们需要判断此字节中的指定位是否为0
return BllResultFactory<string>.Success(ConverHelper.GetBit(data.Buffer[0], data.BitOffset) == 1 ? true.ToString() : false.ToString(), null);
case PLCDataType.DWORD:
return BllResultFactory<string>.Success(reverseBytesTransform.TransUInt32(data.Buffer, 0).ToString(), null);
case PLCDataType.WORD:
return BllResultFactory<string>.Success(reverseBytesTransform.TransUInt16(data.Buffer, 0).ToString(), null);
case PLCDataType.INT:
return BllResultFactory<string>.Success(reverseBytesTransform.TransInt16(data.Buffer, 0).ToString(), null);
case PLCDataType.DINT:
return BllResultFactory<string>.Success(reverseBytesTransform.TransInt32(data.Buffer, 0).ToString(), null);
case PLCDataType.CHAR:
return BllResultFactory<string>.Success(reverseBytesTransform.TransString(data.Buffer, 0, data.Buffer.Length, Encoding.ASCII).Replace("\u0003", "").Trim(), null);
default:
return BllResultFactory<string>.Error("未识别");
}
}
catch (Exception ex)
{
return BllResultFactory<string>.Error($"转换出错:{ex.Message}");
}
}
/// <summary>
/// 数据转换,string到byte[]
/// </summary>
/// <param name="dataType"></param>
/// <param name="value"></param>
/// <returns></returns>
public static BllResult<byte[]> TransferStringToBuffer(PLCDataType dataType, string value)
{
try
{
switch (dataType)
{
case PLCDataType.BYTE:
return BllResultFactory<byte[]>.Success(reverseBytesTransform.TransByte(Convert.ToByte(value)));
case PLCDataType.BOOL:
return BllResultFactory<byte[]>.Success(reverseBytesTransform.TransByte(Convert.ToBoolean(value)));
case PLCDataType.DWORD:
return BllResultFactory<byte[]>.Success(reverseBytesTransform.TransByte(Convert.ToUInt32(value)));
case PLCDataType.WORD:
return BllResultFactory<byte[]>.Success(reverseBytesTransform.TransByte(Convert.ToUInt16(value)));
case PLCDataType.INT:
return BllResultFactory<byte[]>.Success(reverseBytesTransform.TransByte(Convert.ToInt16(value)));
case PLCDataType.DINT:
return BllResultFactory<byte[]>.Success(reverseBytesTransform.TransByte(Convert.ToInt32(value)));
case PLCDataType.CHAR:
return BllResultFactory<byte[]>.Success(reverseBytesTransform.TransByte((value ?? "").PadRight(20, ' '), Encoding.ASCII));
default:
return BllResultFactory<byte[]>.Error("未识别的数据类型");
}
}
catch (Exception ex)
{
return BllResultFactory<byte[]>.Error("转换出现问题:" + ex.Message);
}
}
}
}