MelsecMcAddressData.cs
24.9 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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
using HslCommunication.Profinet.Melsec;
using System;
namespace HslCommunication.Core.Address
{
/// <summary>
/// 三菱的数据地址表示形式
/// </summary>
public class McAddressData : DeviceAddressDataBase
{
#region Constructor
/// <summary>
/// 实例化一个默认的对象
/// </summary>
public McAddressData()
{
McDataType = MelsecMcDataType.D;
}
#endregion
/// <summary>
/// 三菱的数据地址信息
/// </summary>
public MelsecMcDataType McDataType { get; set; }
/// <summary>
/// 从指定的地址信息解析成真正的设备地址信息,默认是三菱的地址
/// </summary>
/// <param name="address">地址信息</param>
/// <param name="length">数据长度</param>
public override void Parse(string address, ushort length)
{
OperateResult<McAddressData> addressData = ParseMelsecFrom(address, length);
if (addressData.IsSuccess)
{
AddressStart = addressData.Content.AddressStart;
Length = addressData.Content.Length;
McDataType = addressData.Content.McDataType;
}
}
#region Static Method
/// <summary>
/// 从实际三菱的地址里面解析出
/// </summary>
/// <param name="address">三菱的地址数据信息</param>
/// <param name="length">读取的数据长度</param>
/// <returns>是否成功的结果对象</returns>
public static OperateResult<McAddressData> ParseMelsecFrom(string address, ushort length)
{
McAddressData addressData = new McAddressData();
addressData.Length = length;
try
{
switch (address[0])
{
case 'M':
case 'm':
{
addressData.McDataType = MelsecMcDataType.M;
addressData.AddressStart = Convert.ToInt32(address.Substring(1), MelsecMcDataType.M.FromBase);
break;
}
case 'X':
case 'x':
{
addressData.McDataType = MelsecMcDataType.X;
addressData.AddressStart = Convert.ToInt32(address.Substring(1), MelsecMcDataType.X.FromBase);
break;
}
case 'Y':
case 'y':
{
addressData.McDataType = MelsecMcDataType.Y;
addressData.AddressStart = Convert.ToInt32(address.Substring(1), MelsecMcDataType.Y.FromBase);
break;
}
case 'D':
case 'd':
{
addressData.McDataType = MelsecMcDataType.D;
addressData.AddressStart = Convert.ToInt32(address.Substring(1), MelsecMcDataType.D.FromBase);
break;
}
case 'W':
case 'w':
{
addressData.McDataType = MelsecMcDataType.W;
addressData.AddressStart = Convert.ToInt32(address.Substring(1), MelsecMcDataType.W.FromBase);
break;
}
case 'L':
case 'l':
{
addressData.McDataType = MelsecMcDataType.L;
addressData.AddressStart = Convert.ToInt32(address.Substring(1), MelsecMcDataType.L.FromBase);
break;
}
case 'F':
case 'f':
{
addressData.McDataType = MelsecMcDataType.F;
addressData.AddressStart = Convert.ToInt32(address.Substring(1), MelsecMcDataType.F.FromBase);
break;
}
case 'V':
case 'v':
{
addressData.McDataType = MelsecMcDataType.V;
addressData.AddressStart = Convert.ToInt32(address.Substring(1), MelsecMcDataType.V.FromBase);
break;
}
case 'B':
case 'b':
{
addressData.McDataType = MelsecMcDataType.B;
addressData.AddressStart = Convert.ToInt32(address.Substring(1), MelsecMcDataType.B.FromBase);
break;
}
case 'R':
case 'r':
{
addressData.McDataType = MelsecMcDataType.R;
addressData.AddressStart = Convert.ToInt32(address.Substring(1), MelsecMcDataType.R.FromBase);
break;
}
case 'S':
case 's':
{
if (address[1] == 'N' || address[1] == 'n')
{
addressData.McDataType = MelsecMcDataType.SN;
addressData.AddressStart = Convert.ToInt32(address.Substring(2), MelsecMcDataType.SN.FromBase);
break;
}
else if (address[1] == 'S' || address[1] == 's')
{
addressData.McDataType = MelsecMcDataType.SS;
addressData.AddressStart = Convert.ToInt32(address.Substring(2), MelsecMcDataType.SS.FromBase);
break;
}
else if (address[1] == 'C' || address[1] == 'c')
{
addressData.McDataType = MelsecMcDataType.SC;
addressData.AddressStart = Convert.ToInt32(address.Substring(2), MelsecMcDataType.SC.FromBase);
break;
}
else
{
addressData.McDataType = MelsecMcDataType.S;
addressData.AddressStart = Convert.ToInt32(address.Substring(1), MelsecMcDataType.S.FromBase);
break;
}
}
case 'Z':
case 'z':
{
if (address.StartsWith("ZR") || address.StartsWith("zr"))
{
addressData.McDataType = MelsecMcDataType.ZR;
addressData.AddressStart = Convert.ToInt32(address.Substring(2), MelsecMcDataType.ZR.FromBase);
break;
}
else
{
addressData.McDataType = MelsecMcDataType.Z;
addressData.AddressStart = Convert.ToInt32(address.Substring(1), MelsecMcDataType.Z.FromBase);
break;
}
}
case 'T':
case 't':
{
if (address[1] == 'N' || address[1] == 'n')
{
addressData.McDataType = MelsecMcDataType.TN;
addressData.AddressStart = Convert.ToInt32(address.Substring(2), MelsecMcDataType.TN.FromBase);
break;
}
else if (address[1] == 'S' || address[1] == 's')
{
addressData.McDataType = MelsecMcDataType.TS;
addressData.AddressStart = Convert.ToInt32(address.Substring(2), MelsecMcDataType.TS.FromBase);
break;
}
else if (address[1] == 'C' || address[1] == 'c')
{
addressData.McDataType = MelsecMcDataType.TC;
addressData.AddressStart = Convert.ToInt32(address.Substring(2), MelsecMcDataType.TC.FromBase);
break;
}
else
{
throw new Exception(StringResources.Language.NotSupportedDataType);
}
}
case 'C':
case 'c':
{
if (address[1] == 'N' || address[1] == 'n')
{
addressData.McDataType = MelsecMcDataType.CN;
addressData.AddressStart = Convert.ToInt32(address.Substring(2), MelsecMcDataType.CN.FromBase);
break;
}
else if (address[1] == 'S' || address[1] == 's')
{
addressData.McDataType = MelsecMcDataType.CS;
addressData.AddressStart = Convert.ToInt32(address.Substring(2), MelsecMcDataType.CS.FromBase);
break;
}
else if (address[1] == 'C' || address[1] == 'c')
{
addressData.McDataType = MelsecMcDataType.CC;
addressData.AddressStart = Convert.ToInt32(address.Substring(2), MelsecMcDataType.CC.FromBase);
break;
}
else
{
throw new Exception(StringResources.Language.NotSupportedDataType);
}
}
default: throw new Exception(StringResources.Language.NotSupportedDataType);
}
}
catch (Exception ex)
{
return new OperateResult<McAddressData>(ex.Message);
}
return OperateResult.CreateSuccessResult(addressData);
}
/// <summary>
/// 从实际基恩士的地址里面解析出
/// </summary>
/// <param name="address">基恩士的地址数据信息</param>
/// <param name="length">读取的数据长度</param>
/// <returns>是否成功的结果对象</returns>
public static OperateResult<McAddressData> ParseKeyenceFrom(string address, ushort length)
{
McAddressData addressData = new McAddressData();
addressData.Length = length;
try
{
switch (address[0])
{
case 'M':
case 'm':
{
addressData.McDataType = MelsecMcDataType.Keyence_M;
addressData.AddressStart = Convert.ToInt32(address.Substring(1), MelsecMcDataType.Keyence_M.FromBase);
break;
}
case 'X':
case 'x':
{
addressData.McDataType = MelsecMcDataType.Keyence_X;
addressData.AddressStart = Convert.ToInt32(address.Substring(1), MelsecMcDataType.Keyence_X.FromBase);
break;
}
case 'Y':
case 'y':
{
addressData.McDataType = MelsecMcDataType.Keyence_Y;
addressData.AddressStart = Convert.ToInt32(address.Substring(1), MelsecMcDataType.Keyence_Y.FromBase);
break;
}
case 'B':
case 'b':
{
addressData.McDataType = MelsecMcDataType.Keyence_B;
addressData.AddressStart = Convert.ToInt32(address.Substring(1), MelsecMcDataType.Keyence_B.FromBase);
break;
}
case 'L':
case 'l':
{
addressData.McDataType = MelsecMcDataType.Keyence_L;
addressData.AddressStart = Convert.ToInt32(address.Substring(1), MelsecMcDataType.Keyence_L.FromBase);
break;
}
case 'S':
case 's':
{
if (address[1] == 'M' || address[1] == 'm')
{
addressData.McDataType = MelsecMcDataType.Keyence_SM;
addressData.AddressStart = Convert.ToInt32(address.Substring(2), MelsecMcDataType.Keyence_SM.FromBase);
break;
}
else if (address[1] == 'D' || address[1] == 'd')
{
addressData.McDataType = MelsecMcDataType.Keyence_SD;
addressData.AddressStart = Convert.ToInt32(address.Substring(2), MelsecMcDataType.Keyence_SD.FromBase);
break;
}
else
{
throw new Exception(StringResources.Language.NotSupportedDataType);
}
}
case 'D':
case 'd':
{
addressData.McDataType = MelsecMcDataType.Keyence_D;
addressData.AddressStart = Convert.ToInt32(address.Substring(1), MelsecMcDataType.Keyence_D.FromBase);
break;
}
case 'R':
case 'r':
{
addressData.McDataType = MelsecMcDataType.Keyence_R;
addressData.AddressStart = Convert.ToInt32(address.Substring(1), MelsecMcDataType.Keyence_R.FromBase);
break;
}
case 'Z':
case 'z':
{
if (address[1] == 'R' || address[1] == 'r')
{
addressData.McDataType = MelsecMcDataType.Keyence_ZR;
addressData.AddressStart = Convert.ToInt32(address.Substring(2), MelsecMcDataType.Keyence_ZR.FromBase);
break;
}
else
{
throw new Exception(StringResources.Language.NotSupportedDataType);
}
}
case 'W':
case 'w':
{
addressData.McDataType = MelsecMcDataType.Keyence_W;
addressData.AddressStart = Convert.ToInt32(address.Substring(1), MelsecMcDataType.Keyence_W.FromBase);
break;
}
case 'T':
case 't':
{
if (address[1] == 'N' || address[1] == 'n')
{
addressData.McDataType = MelsecMcDataType.Keyence_TN;
addressData.AddressStart = Convert.ToInt32(address.Substring(2), MelsecMcDataType.Keyence_TN.FromBase);
break;
}
else if (address[1] == 'S' || address[1] == 's')
{
addressData.McDataType = MelsecMcDataType.Keyence_TS;
addressData.AddressStart = Convert.ToInt32(address.Substring(2), MelsecMcDataType.Keyence_TS.FromBase);
break;
}
else
{
throw new Exception(StringResources.Language.NotSupportedDataType);
}
}
case 'C':
case 'c':
{
if (address[1] == 'N' || address[1] == 'n')
{
addressData.McDataType = MelsecMcDataType.Keyence_CN;
addressData.AddressStart = Convert.ToInt32(address.Substring(2), MelsecMcDataType.Keyence_CN.FromBase);
break;
}
else if (address[1] == 'S' || address[1] == 's')
{
addressData.McDataType = MelsecMcDataType.Keyence_CS;
addressData.AddressStart = Convert.ToInt32(address.Substring(2), MelsecMcDataType.Keyence_CS.FromBase);
break;
}
else
{
throw new Exception(StringResources.Language.NotSupportedDataType);
}
}
default: throw new Exception(StringResources.Language.NotSupportedDataType);
}
}
catch (Exception ex)
{
return new OperateResult<McAddressData>(ex.Message);
}
return OperateResult.CreateSuccessResult(addressData);
}
/// <summary>
/// 计算松下的MC协议的偏移地址的机制
/// </summary>
/// <param name="address">字符串形式的地址</param>
/// <returns>实际的偏移地址</returns>
public static int GetPanasonicAddress(string address)
{
if (address.IndexOf('.') > 0)
{
string[] values = address.Split('.');
return Convert.ToInt32(values[0]) * 16 + Convert.ToInt32(values[1]);
}
else
{
return Convert.ToInt32(address.Substring(0, address.Length - 1)) * 16 + Convert.ToInt32(address.Substring(address.Length - 1), 16);
}
}
/// <summary>
/// 从实际松下的地址里面解析出
/// </summary>
/// <param name="address">松下的地址数据信息</param>
/// <param name="length">读取的数据长度</param>
/// <returns>是否成功的结果对象</returns>
public static OperateResult<McAddressData> ParsePanasonicFrom(string address, ushort length)
{
McAddressData addressData = new McAddressData();
addressData.Length = length;
try
{
switch (address[0])
{
case 'R':
case 'r':
{
int add = GetPanasonicAddress(address.Substring(1));
if (add < 14400)
{
addressData.McDataType = MelsecMcDataType.Panasonic_R;
addressData.AddressStart = add;
}
else
{
addressData.McDataType = MelsecMcDataType.Panasonic_SM;
addressData.AddressStart = add - 14400;
}
break;
}
case 'X':
case 'x':
{
addressData.McDataType = MelsecMcDataType.Panasonic_X;
addressData.AddressStart = GetPanasonicAddress(address.Substring(1));
break;
}
case 'Y':
case 'y':
{
addressData.McDataType = MelsecMcDataType.Panasonic_Y;
addressData.AddressStart = GetPanasonicAddress(address.Substring(1));
break;
}
case 'L':
case 'l':
{
if (address[1] == 'D' || address[1] == 'd')
{
addressData.McDataType = MelsecMcDataType.Panasonic_LD;
addressData.AddressStart = Convert.ToInt32(address.Substring(2));
break;
}
else
{
addressData.McDataType = MelsecMcDataType.Panasonic_L;
addressData.AddressStart = GetPanasonicAddress(address.Substring(1));
}
break;
}
case 'D':
case 'd':
{
int add = Convert.ToInt32(address.Substring(1));
if (add < 90000)
{
addressData.McDataType = MelsecMcDataType.Panasonic_DT;
addressData.AddressStart = Convert.ToInt32(address.Substring(1));
}
else
{
addressData.McDataType = MelsecMcDataType.Panasonic_SD;
addressData.AddressStart = Convert.ToInt32(address.Substring(1)) - 90000;
}
break;
}
case 'T':
case 't':
{
if (address[1] == 'N' || address[1] == 'n')
{
addressData.McDataType = MelsecMcDataType.Panasonic_TN;
addressData.AddressStart = Convert.ToInt32(address.Substring(2));
break;
}
else if (address[1] == 'S' || address[1] == 's')
{
addressData.McDataType = MelsecMcDataType.Panasonic_TS;
addressData.AddressStart = Convert.ToInt32(address.Substring(2));
break;
}
else
{
throw new Exception(StringResources.Language.NotSupportedDataType);
}
}
case 'C':
case 'c':
{
if (address[1] == 'N' || address[1] == 'n')
{
addressData.McDataType = MelsecMcDataType.Panasonic_CN;
addressData.AddressStart = Convert.ToInt32(address.Substring(2));
break;
}
else if (address[1] == 'S' || address[1] == 's')
{
addressData.McDataType = MelsecMcDataType.Panasonic_CS;
addressData.AddressStart = Convert.ToInt32(address.Substring(2));
break;
}
else
{
throw new Exception(StringResources.Language.NotSupportedDataType);
}
}
default: throw new Exception(StringResources.Language.NotSupportedDataType);
}
}
catch (Exception ex)
{
return new OperateResult<McAddressData>(ex.Message);
}
return OperateResult.CreateSuccessResult(addressData);
}
#endregion
}
}