본문 바로가기

PLC

[C#]PC로 미쯔비시 PLC 제어하기-디바이스값 읽기/쓰기-MX component

 

Read from PLC

Wrtie to PLC

 

텍스트 박스 원하는 디바이스 값을 넣고 읽기/쓰기 버튼을 클릭하면 해당하는 값을 읽거나 쓸수 있다.

 

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using ActUtlTypeLib;


namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {
        public ActUtlType plc = new ActUtlType();
        ActUtlTypeLib.ActUtlType ActUtlType;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            plc.ActLogicalStationNumber = 1;
            int nRtn = plc.Open();
            if(nRtn == 0)
            {
                lbConnstatus.Text = "연결성공";
                timer1.Interval = 100;
                timer1.Enabled = true;
            }
            else 
            {
                MessageBox.Show("연결 실패");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
         
            int nRtn = plc.Close();
            if (nRtn == 0)
            {
                lbConnstatus.Text = "연결종료";
                timer1.Enabled = false;
            }
        
        }

        private void button3_Click(object sender, EventArgs e) // 실린더 후진
        {
            plc.SetDevice("y21", 0);
            plc.SetDevice("y22", 1);
        }

        private void button4_Click(object sender, EventArgs e) // 실린더 전진
        {
            plc.SetDevice("y21", 1);
            plc.SetDevice("y22", 2);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void ACydBackSnsr_Click(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            plc.GetDevice("x1", out int x01);
            plc.GetDevice("x2", out int x02);
            if (x01 == 1) ACydBackSnsr.BackColor = System.Drawing.Color.Green;
            if (x01 == 0) ACydBackSnsr.BackColor = System.Drawing.Color.Gray;
            if (x02 == 1) ACydForSnsr.BackColor = System.Drawing.Color.Green;
            if (x02 == 0) ACydForSnsr.BackColor = System.Drawing.Color.Gray;

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void BCydForSnsr_Click(object sender, EventArgs e)
        {

        }

        private void button9_Click(object sender, EventArgs e)
        {

        }

        private void button14_Click(object sender, EventArgs e)
        {
            if(txtAddressR.Text !="")
            {
                int Read_Result;
                plc.GetDevice(txtAddressR.Text, out Read_Result);
                ValueR.Text = Read_Result.ToString();

            }
            

        }

        private void button15_Click(object sender, EventArgs e)
        {
            if(txtAddressW.Text !="")
            {
                
                plc.SetDevice(txtAddressW.Text, Convert.ToInt16(ValueW.Text));
            }

        }
    }
}

 

디바이스값읽기쓰기.zip
0.06MB