본문 바로가기

Programming/C#/WPF

MX Component v4을 이용하여 C# 프로그램 만들기.

내가 사용할 Tip 정리_4


Step 1.

MX Component의 Communication Setup Utility를 이용해서 기본 세팅을 한다.

(참조 : http://orangetazo.tistory.com/18 )


GX Works 2를 실행 및 새 프로젝트 생성해서 간단한 Ladder 코드를 작성하고, GX Simulator를 실행한다.

(참조 : http://orangetazo.tistory.com/17 )


Step 2.

Visual Studio 2013 실행해서 프로젝트를 생성한다.

C#이나 WPF나 어차피 C# 코드로 PLC를 접근할 거라 편한쪽으로 하자.

참고로 난 WPF를 배우는 중이니 WPF로 해야지, Project 이름은 PlcRemote 로 하고 확인 버튼~


Step 3.

설치된 MX Component의 설치 경로에서 필요한 파일을 참조로 추가하자.

기본으로 설치했다면 경로는 C:\MELSEC\Act\Control 이다.


ActEther.dll은 이더넷 카드를 통해서 LAN선으로 연결하고자 할 때 사용가능하다. 다만 설정할 것을이 좀 있다.

ActPcUsb.dll은 CPU의 USB Port를 통해서 PLC에 접근하고자 할 때 사용할 수 있다. 설정할 내용이 없다.

ActUtlType.dll은 Step 1.에서 설정한 내용을 기준으로 PLC에 접근이 가능하도록 해준다.

이외 다른 dll도 많지만 일단 난 사용할 PLC의 CPU가 Q시리즈라 이것들만 사용했다.


참고로 참조는 ActUtlType.dll 이것만 해줘도 된다.


Step 4.

일단 Window Form에 아래 그림 처럼 Control을 배치했다. C#이나 WPF나 화면 디자인은 어차피 비슷하니 대충 넘어간다.

이름

Control

설명 

Logical Station number

TextBlock 


텍스트 박스

TextBox 

Communication Setup Utility에서 설정한 PLC Port 번호 

Connect / Disconnect

Button 

PLC 연결 및 연결 해제를 위한 버튼

Connected :

TextBlock 

1초 마다 PLC의 D0의 값을 읽어와서 화면에 표시 

Timer 

Timer 

1초 마다 PLC의 D0값을 읽어올 수 있도록 타이머

사용한 Control (WPF 기준)


Step 5.

이제 실제로 PLC에서 데이터를 읽어오기 위한 코드이다. 물론 테스트를 하기 위해서는 GX Works 2의 GX Simulator2를 기동하길 바란다.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace PlcRemote {     /// <summary>     /// MainWindow.xaml에 대한 상호 작용 논리     /// </summary>     public partial class MainWindow : Window     {         bool bPlcConnection = false;         System.Timers.Timer _Timer;         ActUtlTypeLib.ActUtlType _ActUtlType;
        public MainWindow()         {             InitializeComponent();         }
        //Timer 간격이 경과되면 발생될 내용         void _Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)         {             if (bPlcConnection.Equals(true))             {                 String szDevice = "D0"; //읽어올 PLC Device                 int lSize = 1; //읽어올 PLC Device의 Word 수                 int[] lplData = new int[lSize]; //읽어온 PLC Device Word 값을 저장할 변수
                //_ActUtlType.ReadDeviceBlock 함수의 리턴 값                 int nResult = _ActUtlType.ReadDeviceBlock(szDevice, lSize, out lplData[0]);                 if (nResult.Equals(0)) //정상적으로 리턴받으면 0을 받는다.                 {                     this.Dispatcher.Invoke(new D_Set_Value(_Set_Value), lplData[0]);                 }             }         }
        // PLC로 부터 읽은 데이터를 입력하기 위한 대리자         private delegate void D_Set_Value(int nValue);         private void _Set_Value(int nValue)         {             vConnectedText.Text = String.Format("Connected : {0}", nValue);         }
        //PLC 연결 버튼         private void Button_Click(object sender, RoutedEventArgs e)         {             _Timer = new System.Timers.Timer();             _Timer.Interval = 100;             _Timer.Elapsed += _Timer_Elapsed;             _ActUtlType = new ActUtlTypeLib.ActUtlType();
            //Logical Station number를 입력할 TextBox가 String.Empty이거나, Null이 아니면 진행되도록.             if (String.IsNullOrEmpty(vPortNumber.Text).Equals(false))             {                 _ActUtlType.ActLogicalStationNumber = Convert.ToInt32(vPortNumber.Text);                 if (_ActUtlType.Open().Equals(0))                 {                     bPlcConnection = true;
                    //PLC의 M0를 접점시키는 코드                     if (_ActUtlType.WriteDeviceBlock("M0", 1, 1).Equals(0))                      {                         _Timer.Start();                     }                 }             }         }
        //PLC 연결 해제 버튼         private void Button_Click_1(object sender, RoutedEventArgs e)         {             if (bPlcConnection.Equals(true))             {                 //PLC의 M0를 접점을 해제시키는 코드                 if (_ActUtlType.WriteDeviceBlock("M0", 1, 0).Equals(0))                 {                     _Timer.Stop();                     _Timer.Dispose();                     _ActUtlType.Close();                 }             }         }     } }

PlcRemote.zip


실행해봤더니 아주 잘 동작한다. 얼른 공부해서 HMI 프로그램을 개발해야 하는데....


끝~