포스트

나만의 Misubishi PLC Class 작성

Mitsubishi PLC Class

나만의 클래스를 작성해 놓으면 언제든지 원하는 데로 수정하고 유용하게 사용 가능하다.

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
public class MitsubishiPLC
{
    private ActUtlType mx;

    /// <summary>
    /// PLC 연결 여부 상태
    /// </summary>
    public bool IsConnected { get; set; }

    private int logicalNumber = 1;

    /// <summary>
    /// PLC 로지컬 스테이션 번호 설정
    /// </summary>
    public int LogicalNumber
    {
        get { return logicalNumber; }
        set
        {
            if (mx != null)
            {
                logicalNumber = value;
                mx.ActLogicalStationNumber = logicalNumber;
            }
        }
    }

    public MitsubishiPLC() { }

    ~MitsubishiPLC()
    {
        if (mx != null)
        {
            mx.Close();
        }
    }

    /// <summary>
    /// MX Component 객체 초기화
    /// </summary>
    public bool Initialize()
    {
        try
        {
            mx = new ActUtlType();
            return true;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
        return false;
    }

    /// <summary>
    /// PLC 연결 시도
    /// </summary>
    public bool Open()
    {
        try
        {
            int resultCode = mx.Open();
            if (resultCode == 0)
            {
                Console.WriteLine($"Mitsubishi PLC open. (Logical Number: {LogicalNumber})");
                IsConnected = true;
                return true;
            }
            else
            {
                WriteErrorCodeLog(resultCode);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
        IsConnected = false;
        return false;
    }

    /// <summary>
    /// PLC 연결 종료
    /// </summary>
    public bool Close()
    {
        try
        {
            int resultCode = mx.Close();
            if (resultCode == 0)
            {
                Console.WriteLine($"Mitsubishi PLC close. (Logical Number: {LogicalNumber})");
                IsConnected = false;
                return true;
            }
            else
            {
                WriteErrorCodeLog(resultCode);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
        return false;
    }

    /// <summary>
    /// PLC 온라인 상태 확인 (SM400 디바이스 이용)
    /// </summary>
    public bool IsOnline()
    {
        try
        {
            int resultCode = mx.GetDevice("SM400", out int iData);
            if (resultCode == 0 && iData == 1)
            {
                IsConnected = true;
                return true;
            }
            else
            {
                WriteErrorCodeLog(resultCode);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
        IsConnected = false;
        return false;
    }

    /// <summary>
    /// 단일 디바이스(short형 데이터) 쓰기
    /// </summary>
    public bool SetDevice2(string deviceCode, int address, short data)
    {
        try
        {
            if (IsConnected)
            {
                string deviceAddress = deviceCode + address.ToString();
                int resultCode = mx.SetDevice2(deviceAddress, data);
                if (resultCode == 0)
                {
                    Console.WriteLine($"SetDevice2 :: Address: {deviceAddress}, Data: {data}");
                    return true;
                }
                else
                {
                    WriteErrorCodeLog(resultCode);
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
        return false;
    }

    /// <summary>
    /// 연속 디바이스 블록(short 배열) 쓰기
    /// </summary>
    public bool WriteDeviceBlock2(string deviceCode, int headAddress, short[] data)
    {
        try
        {
            if (IsConnected && data.Length > 0)
            {
                string deviceAddress = deviceCode + headAddress.ToString();
                int resultCode = mx.WriteDeviceBlock2(deviceAddress, data.Length, ref data[0]);
                if (resultCode == 0)
                {
                    Console.WriteLine($"WriteDeviceBlock2 :: Start Address: {deviceAddress}, Size: {data.Length}");
                    return true;
                }
                else
                {
                    WriteErrorCodeLog(resultCode);
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
        return false;
    }

    /// <summary>
    /// 복수 디바이스 지정하여 랜덤 쓰기
    /// </summary>
    public bool WriteDeviceRandom2(string deviceList, short[] data)
    {
        try
        {
            if (IsConnected && deviceList.Contains('\n'))
            {
                string[] arrayDeviceList = deviceList.Split('\n');
                if (arrayDeviceList.Length == data.Length && data.Length > 0)
                {
                    int resultCode = mx.WriteDeviceRandom2(deviceList, data.Length, ref data[0]);
                    if (resultCode == 0)
                    {
                        Console.WriteLine($"WriteDeviceRandom2 :: Start Address: {arrayDeviceList[0]}, Size: {data.Length}");
                        return true;
                    }
                    else
                    {
                        WriteErrorCodeLog(resultCode);
                    }
                }
                else
                {
                    Console.WriteLine($"WriteDeviceRandom2 :: Size mismatch (Device List: {arrayDeviceList.Length}, Data: {data.Length})");
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
        return false;
    }

    /// <summary>
    /// 단일 디바이스(short) 읽기
    /// </summary>
    public short GetDeivce2(string deviceCode, int address)
    {
        try
        {
            if (IsConnected)
            {
                string deviceAddress = deviceCode + address.ToString();
                int resultCode = mx.GetDevice2(deviceAddress, out short data);
                if (resultCode == 0)
                {
                    Console.WriteLine($"GetDevice2 :: Address: {deviceAddress}, Data: {data}");
                    return data;
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
        return 0;
    }

    /// <summary>
    /// 연속 디바이스 블록 읽기
    /// </summary>
    public short[] ReadDeviceBlock2(string deviceAddress, int size)
    {
        short[] data = new short[size];
        try
        {
            if (IsConnected)
            {
                int resultCode = mx.ReadDeviceBlock2(deviceAddress, size, out data[0]);
                if (resultCode == 0)
                {
                    Console.WriteLine($"Read Device Block2 :: Address: {deviceAddress}, Size: {size}");
                    return data;
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
        return data;
    }

    /// <summary>
    /// 복수 디바이스 지정하여 랜덤 읽기
    /// </summary>
    public short[] ReadDeviceRandom2(string deviceList, int size)
    {
        short[] data = new short[size];
        try
        {
            if (IsConnected && deviceList.Contains('\n'))
            {
                string[] arrayDeviceList = deviceList.Split('\n');
                if (arrayDeviceList.Length == size && size > 0)
                {
                    int resultCode = mx.ReadDeviceRandom2(deviceList, size, out data[0]);
                    if (resultCode == 0)
                    {
                        Console.WriteLine($"ReadDeviceRandom2 :: Start Address: {arrayDeviceList[0]}, Size: {size}");
                        return data;
                    }
                }
                else
                {
                    Console.WriteLine($"ReadDeviceRandom2 :: Size mismatch (Device List: {arrayDeviceList.Length}, Data: {size})");
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
        return data;
    }

    /// <summary>
    /// 에러 코드 로깅 (16진수 포맷)
    /// </summary>
    private void WriteErrorCodeLog(int errorCode)
    {
        string errorMessage = string.Format("0x{0:x8} [HEX]", errorCode);
        Console.WriteLine(errorMessage);
    }
}

바로 실무에 사용 가능하다.

이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.