LTT Source에 들어가는 Module Dll
장정호
2020-12-15 dd4a016ef8fc5cae0cbd336a5bdec4651b1d87c1
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
using log4net;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace DIT.Framework.Module
{
    /// <summary>
    /// Model : PMC-2HS-232
    /// Home : www.autonics.com
    /// </summary>
    public class Autonics_PMC_2HS : SerialModule
    {
        #region 파라미터
 
        /// <summary>
        /// Exception Log
        /// </summary>
        private ILog ExceptionLog = LogManager.GetLogger("Autonics_PMC_2HS_Exception");
        
        /// <summary>
        /// Exception Log를 기록하기 위한 메서드
        /// </summary>
        /// <param name="msg">Error Message</param>
        private void WriteExceptionLog(string msg)
        {
            if (UseExceptionLog)
                ExceptionLog.Debug(msg);
        }
 
        /// <summary>
        /// X축의 Status
        /// </summary>
        public MotorStatus Status_X { get { return status_x; } private set { status_x = value; } }
        private MotorStatus status_x;
 
        /// <summary>
        /// Y축의 Status
        /// </summary>
        public MotorStatus Status_Y { get { return status_y; } private set { status_y = value; } }
        private MotorStatus status_y;
 
        /// <summary>
        /// Parallel의 Status
        /// </summary>
        public ParallelStatus Status_Parallel { get { return status_parallel; } private set { status_parallel = value; } }
        private ParallelStatus status_parallel;
 
        /// <summary>
        /// X축 현재위치
        /// </summary>
        public int PosX { get { return posx; } private set { posx = value; } }
        private int posx;
 
        /// <summary>
        /// Y축 현재위치
        /// </summary>
        public int PosY { get { return posy; } private set { posy = value; } }
        private int posy;
 
        /// <summary>
        /// X축 현재 Speed
        /// </summary>
        public int SpeedX { get { return speedx; } private set { speedx = value; } }
        private int speedx;
 
        /// <summary>
        /// Y축 현재 Speed
        /// </summary>
        public int SpeedY { get { return speedy; } private set { speedy = value; } }
        private int speedy;
        #endregion
 
        /// <summary>
        /// Autonics_PMC_2HS의 생성자.
        /// </summary>
        /// <param name="PortName"> Port이름 ex)COM6, COM13 </param>
        /// <param name="BaudRate"> 전송속도 ex)4800, 9600, 19200 </param>
        /// <param name="DataBits"> 바이트 당 데이터 비트의 표준길이 ex)5, 6, 7, 8 </param>
        /// <param name="Parity">패리티 검사 프로토콜 ex)Ports.Parity.None </param>
        /// <param name="StopBits">비트당 정지비트의 표준 개수 ex)Ports.StopBits.One </param>
        /// <param name="Handshake">직렬 전송을 위한 핸드셰이킹 ex)Ports.Handshake.None</param>
        public Autonics_PMC_2HS(string PortName, int BaudRate, int DataBits = 8, Parity Parity = Parity.None, StopBits StopBits = StopBits.One, Handshake Handshake = Handshake.None) 
            : base(PortName, BaudRate, DataBits, Parity, StopBits, Handshake)
        {
            SetEncodeType(Encoding.ASCII);
            Status_X = new MotorStatus();
            Status_Y = new MotorStatus();
            Status_Parallel = new ParallelStatus();
        }
 
        /// <summary>
        /// Serial에서 받은 명령어 처리 메서드
        /// </summary>
        /// <param name="recvData">Recv받은 Data</param>
        private void OnRecvData(byte[] recvData)
        {
            try
            {
                string[] str = Encoding.ASCII.GetString(recvData).Split(' ');
 
                switch (str[0])
                {
                    case "ERD":
                        string[] proc_str;
                        proc_str = str[1].Split('X');
                        int ia = int.Parse(proc_str[1]);
 
                        BitArray bits = new BitArray(new int[] { ia });
                        break;
                    case "INR":
                        // X모터값 
                        if (str[1].Contains("X"))
                        {
                            string x_motor = str[1].Split('X')[1].Split(',')[0];
                            string parallel = str[2].Split('\r')[0];
                            Status_X.StatusChange(x_motor);
                            Status_Parallel.StatusChange(parallel);
                        }
                        // Y모터값
                        else if (str[1].Contains("Y"))
                        {
                            string y_motor = str[1].Split('Y')[1].Split(',')[0];
                            string parallel = str[2].Split('\r')[0];
                            Status_Y.StatusChange(y_motor);
                            Status_Parallel.StatusChange(parallel);
                        }
                        break;
                    case "SPD":
                        string proc_data = Encoding.Default.GetString(recvData);
                        string[] speeds = proc_data.Split('D');
                        string[] speed = speeds[1].Split(',');
                        int spd = 0;
                        if (int.TryParse(speed[0], out spd)) SpeedX = spd;
                        if (int.TryParse(speed[1], out spd)) SpeedY = spd;
                        break;
                    case "POS":
                        string[] poss = str[1].Split(',');
                        PosX = Convert.ToInt32(poss[0], 16);
                        PosY = Convert.ToInt32(poss[1].Split('\r')[0], 16);
                        break;
                }
            }
            catch(Exception ex)
            {
                WriteExceptionLog(ex.Message);
            }
        }
 
        #region 명령어
        /// <summary>
        /// X축 HOME
        /// </summary>
        public void Home_X()
        {
            string cmd = $"HOM X{CR}";
            SendData(Encoding.ASCII.GetBytes(cmd));
        }
 
        /// <summary>
        /// Y축 HOME
        /// </summary>
        public void Home_Y()
        {
            string cmd = $"HOM Y{CR}";
            SendData(Encoding.ASCII.GetBytes(cmd));
        }
 
        /// <summary>
        /// X축 HOME Stop
        /// </summary>
        public void Home_X_Stop()
        {
            string cmd = $"OGE X{CR}";
            SendData(Encoding.ASCII.GetBytes(cmd));
        }
 
        /// <summary>
        /// Y축 HOME Stop
        /// </summary>
        public void Home_Y_Stop()
        {
            string cmd = $"OGE Y{CR}";
            SendData(Encoding.ASCII.GetBytes(cmd));
        }
 
        /// <summary>
        /// X축 실제 위치 클리어
        /// </summary>
        public void X_Pos_Clear()
        {
            string cmd = $"CLR X{CR}";
            SendData(Encoding.ASCII.GetBytes(cmd));
        }
 
        /// <summary>
        /// Y축 실제 위치 클리어
        /// </summary>
        public void Y_Pos_Clear()
        {
            string cmd = $"CLR Y{CR}";
            SendData(Encoding.ASCII.GetBytes(cmd));
        }
 
        /// <summary>
        /// X축 내부 프로그램 지정 번지부터 실행
        /// </summary>
        /// <param name="value">번지</param>
        public void Proc_X_Run(int value)
        {
            string cmd = $"PRG X{value}{CR}";
            SendData(Encoding.ASCII.GetBytes(cmd));
        }
 
        /// <summary>
        /// Y축 내부 프로그램 지정 번지부터 실행
        /// </summary>
        /// <param name="value">번지</param>
        public void Proc_Y_Run(int value)
        {
            string cmd = $"PRG Y{value}{CR}";
            SendData(Encoding.ASCII.GetBytes(cmd));
        }
 
        /// <summary>
        /// X축 내부 프로그램 지정 번지 실행
        /// </summary>
        /// <param name="value">번지</param>
        public void Proc_X_Step(int value)
        {
            string cmd = $"PST X{value}{CR}";
            SendData(Encoding.ASCII.GetBytes(cmd));
        }
 
        /// <summary>
        /// Y축 내부 프로그램 지정 번지 실행
        /// </summary>
        /// <param name="value">번지</param>
        public void Proc_Y_Step(int value)
        {
            string cmd = $"PST Y{value}{CR}";
            SendData(Encoding.ASCII.GetBytes(cmd));
        }
 
        /// <summary>
        ///  X축 내부 프로그램 일시정지
        /// </summary>
        public void Proc_X_Stop()
        {
            string cmd = $"PSP X{CR}";
            SendData(Encoding.ASCII.GetBytes(cmd));
        }
 
        /// <summary>
        ///  Y축 내부 프로그램 일시정지
        /// </summary>
        public void Proc_Y_Stop()
        {
            string cmd = $"PSP Y{CR}";
            SendData(Encoding.ASCII.GetBytes(cmd));
        }
 
        /// <summary>
        /// X축 속도 설정
        /// </summary>
        /// <param name="value">속도</param>
        public void Set_Speed_X(int value)
        {
            string cmd = $"SPD {value}{CR}";
            SendData(Encoding.ASCII.GetBytes(cmd));
        }
 
        /// <summary>
        /// Y축 속도 설정
        /// </summary>
        /// <param name="value">속도</param>
        public void Set_Speed_Y(int value)
        {
            string cmd = $"SPD ,{value}{CR}";
            SendData(Encoding.ASCII.GetBytes(cmd));
        }
 
        /// <summary>
        /// X축 +위치로 이동
        /// </summary>
        public void Move_X_Plus()
        {
            string cmd = $"JOG +X{CR}";
            SendData(Encoding.ASCII.GetBytes(cmd));
        }
 
        /// <summary>
        /// X축 -위치로 이동
        /// </summary>
        public void Move_X_Minus()
        {
            string cmd = $"JOG -X{CR}";
            SendData(Encoding.ASCII.GetBytes(cmd));
        }
 
        /// <summary>
        /// Y축 +위치로 이동
        /// </summary>
        public void Move_Y_Plus()
        {
            string cmd = $"JOG +Y{CR}";
            SendData(Encoding.ASCII.GetBytes(cmd));
        }
 
        /// <summary>
        /// Y축 -위치로 이동
        /// </summary>
        public void Move_Y_Minus()
        {
            string cmd = $"JOG -Y{CR}";
            SendData(Encoding.ASCII.GetBytes(cmd));
        }
 
        /// <summary>
        /// X축 절대이동
        /// </summary>
        /// <param name="value">이동 위치</param>
        public void Move_X_ABS(int value)
        {
            string cmd = $"PAB {value}{CR}";
            SendData(Encoding.ASCII.GetBytes(cmd));
        }
 
        /// <summary>
        /// Y축 절대이동
        /// </summary>
        /// <param name="value">이동 위치</param>
        public void Move_Y_ABS(int value)
        {
            string cmd = $"PAB ,{value}{CR}";
            SendData(Encoding.ASCII.GetBytes(cmd));
        }
 
        /// <summary>
        /// X축 정지
        /// </summary>
        public void Stop_X()
        {
            string cmd = $"PSP X{CR}";
            SendData(Encoding.ASCII.GetBytes(cmd));
        }
 
        /// <summary>
        /// Y축 정지
        /// </summary>
        public void Stop_Y()
        {
            string cmd = $"PSP Y{CR}";
            SendData(Encoding.ASCII.GetBytes(cmd));
        }
 
        /// <summary>
        /// 드라이브 속도 변경
        /// </summary>
        /// <param name="value">1~4</param>
        public void Speed_Setting(int value)
        {
            string cmd = $"SSM X{value}{CR}";
            SendData(Encoding.ASCII.GetBytes(cmd));
        }
 
        /// <summary>
        /// 드라이브 속도 변경
        /// </summary>
        /// <param name="x_value">X 축 속도</param>
        /// <param name="y_value">Y 축 속도</param>
        public void Speed_Setting(int x_value, int y_value)
        {
            string cmd = $"SPD {x_value},{y_value}{CR}";
            SendData(Encoding.ASCII.GetBytes(cmd));
        }
 
        /// <summary>
        /// X축 에러 값 요청
        /// </summary>
        public void Request_Error_X()
        {
            string cmd = $"ERD X{CR}";
            byte[] recvData = SendWaitData(Encoding.ASCII.GetBytes(cmd));
 
            if (recvData == null)
                return;
 
            OnRecvData(recvData);
        }
 
        /// <summary>
        /// Y축 에러 값 요청
        /// </summary>
        public void Request_Error_Y()
        {
            string cmd = $"ERD Y{CR}";
            byte[] recvData = SendWaitData(Encoding.ASCII.GetBytes(cmd));
 
            if (recvData == null)
                return;
 
            OnRecvData(recvData);
        }
 
        /// <summary>
        /// X축 현재 상태 요청
        /// </summary>
        public void Request_State_X()
        {
            string cmd = $"INR X{CR}";
            byte[] recvData = SendWaitData(Encoding.ASCII.GetBytes(cmd));
 
            if (recvData == null)
                return;
 
            OnRecvData(recvData);
        }
 
        /// <summary>
        /// Y축 현재 상태 요청
        /// </summary>
        public void Request_State_Y()
        {
            string cmd = $"INR Y{CR}";
            byte[] recvData = SendWaitData(Encoding.ASCII.GetBytes(cmd));
 
            if (recvData == null)
                return;
 
            OnRecvData(recvData);
        }
 
        /// <summary>
        /// PMC 본체의 버전 정보 요청
        /// </summary>
        public void Request_Ver()
        {
            string cmd = $"VER{CR}";
            byte[] recvData = SendWaitData(Encoding.ASCII.GetBytes(cmd));
 
            if (recvData == null)
                return;
 
            OnRecvData(recvData);
        }
 
        /// <summary>
        /// X축과 Y축의 현재 위치 요청
        /// </summary>
        public void Request_Pos()
        {
            string cmd = $"POS{CR}";
            byte[] recvData = SendWaitData(Encoding.ASCII.GetBytes(cmd));
 
            if (recvData == null)
                return;
 
            OnRecvData(recvData);
        }
 
        /// <summary>
        /// X축과 Y축의 현재 속도 요청
        /// </summary>
        public void Request_Speed()
        {
            string cmd = $"SPD{CR}";
            byte[] recvData = SendWaitData(Encoding.ASCII.GetBytes(cmd));
 
            if (recvData == null)
                return;
 
            OnRecvData(recvData);
        }
 
        #endregion
 
        #region Motor의 상태를 확인하기 위한 Class
        /// <summary>
        /// Motor의 상태 클래스
        /// </summary>
        public class MotorStatus
        {
            #region 프로퍼티
            /// <summary>
            /// 서보 위치 결정 완료
            /// </summary>
            public bool Inpos { get; private set; }
 
            /// <summary>
            /// 서보 알람
            /// </summary>
            public bool Alarm { get; private set; }
 
            /// <summary>
            /// 엔코더 Z상
            /// </summary>
            public bool Stop2 { get; private set; }
 
            /// <summary>
            /// 원점
            /// </summary>
            public bool Stop1 { get; private set; }
 
            /// <summary>
            /// 원점 근접
            /// </summary>
            public bool Stop0 { get; private set; }
 
            /// <summary>
            /// +방향 리미트
            /// </summary>
            public bool Lmt_Plus { get; private set; }
 
            /// <summary>
            /// -방향 리미트
            /// </summary>
            public bool Lmt_Minus { get; private set; }
 
            /// <summary>
            /// 긴급정지
            /// </summary>
            public bool EMG { get; private set; }
 
            /// <summary>
            /// 드라이브 상태(1 : 드라이브 중 0 :종료)
            /// </summary>
            public bool DRV { get; private set; }
 
            /// <summary>
            /// 에러 발생 상태(1 : 에러)
            /// </summary>
            public bool ERR { get; private set; }
            /// <summary>
            /// 자동 원점복귀 상태 (1:동작중)
            /// </summary>
            public bool HOM { get; private set; }
            /// <summary>
            /// 드라이브 동작, 원점출력 동작 등(1:ON)
            /// </summary>
            public bool RUN { get; private set; }
            /// <summary>
            /// 일시 정지중(1 : 일시정지중)
            /// </summary>
            public bool PAUSE { get; private set; }
            /// <summary>
            /// (CN3) 의 nDRIVE 신호(1:ON)
            /// </summary>
            public bool DRIVE { get; private set; }
 
            /// <summary>
            /// (CN3)의 nError 신호(1:ON)
            /// </summary>
            public bool ERROR { get; private set; }
 
            /// <summary>
            /// (CN4, 5)의 nOUT0 신호(1:ON)
            /// </summary>
            public bool OUT0 { get; private set; }
 
            #endregion
 
            /// <summary>
            /// Status 변경
            /// </summary>
            /// <param name="data"></param>
            public void StatusChange(string data)
            {
 
                BitArray bit = new BitArray(new int[] { Convert.ToInt32(data, 16) });
 
                Stop0 = bit[0];
                Stop1 = bit[1];
                Stop2 = bit[2];
                Inpos = bit[3];
                Lmt_Plus = bit[4];
                Lmt_Minus = bit[5];
                Alarm = bit[6];
                EMG = bit[7];
                DRV = bit[8];
                ERR = bit[9];
                HOM = bit[10];
                RUN = bit[11];
                PAUSE = bit[12];
                OUT0 = bit[13];
                DRIVE = bit[14];
                ERROR = bit[15];
 
            }
        }
 
        /// <summary>
        /// Parallel 상태 클래스
        /// </summary>
        public class ParallelStatus
        {
            #region
            /// <summary>
            /// 원점 복귀 시작 명령
            /// </summary>
            public bool Home { get; private set; }
 
            /// <summary>
            /// 드라이브 시작명령
            /// </summary>
            public bool Strobe { get; private set; }
 
            /// <summary>
            /// X축 지정/스캔Y+
            /// </summary>
            public bool X { get; private set; }
 
            /// <summary>
            /// Y축지정/스캔Y-
            /// </summary>
            public bool Y { get; private set; }
 
            /// <summary>
            /// 레지스터 지정0/ 런+/스캔+
            /// </summary>
            public bool RegSL0 { get; private set; }
 
            /// <summary>
            /// 레지스터 지정1/ 런-/스캔+
            /// </summary>
            public bool RegSL1 { get; private set; }
 
            /// <summary>
            /// 레지스터 지정 2/ 드라이브 속도 지정0
            /// </summary>
            public bool RegSL2 { get; private set; }
 
            /// <summary>
            /// 레지스터 지정3/ 드라이브 속도 지정1
            /// </summary>
            public bool RegSL3 { get; private set; }
 
            /// <summary>
            /// 레지스터 지정4/ 스캔 지정
            /// </summary>
            public bool RegSL4 { get; private set; }
 
            /// <summary>
            /// 레지스터 지정5/ 드라이브 정지
            /// </summary>
            public bool RegSL5 { get; private set; }
 
            /// <summary>
            /// 동작 모드 지정0
            /// </summary>
            public bool Mode0 { get; private set; }
 
            /// <summary>
            /// 동작 모드 지정1
            /// </summary>
            public bool Mode1 { get; private set; }
 
            /// <summary>
            /// 0 : 비동작 1:동작
            /// </summary>
            public bool PowerOnHome { get; private set; }
 
            /// <summary>
            /// 0 : 비동작 1:동작
            /// </summary>
            public bool PowerOnProgram { get; private set; }
            #endregion
 
            /// <summary>
            /// Status 변경
            /// </summary>
            /// <param name="data"></param>
            public void StatusChange(string data)
            {
                BitArray bit = new BitArray(new int[] { Convert.ToInt32(data, 16) });
                Home = bit[0];
                Strobe = bit[1];
                X = bit[2];
                Y = bit[3];
                Mode0 = bit[4];
                Mode1 = bit[5];
                RegSL0 = bit[8];
                RegSL1 = bit[9];
                RegSL2 = bit[10];
                RegSL3 = bit[11];
                RegSL4 = bit[12];
                RegSL5 = bit[13];
            }
 
 
        }
        #endregion
    }
}