본문 바로가기

PLC

[C#]PC로 미쯔비시 PLC 제어하기-실린더제어-MX component

SetDeviec / GetDevice 활용하기

timer를 활용하여 센서의 값을 표시한다.

 

 

 

 

 

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;  // 1000 = 1초 
                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 CydBackSnsr_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) CydBackSnsr.BackColor = System.Drawing.Color.Green;
            if (x01 == 0) CydBackSnsr.BackColor = System.Drawing.Color.Gray;
            if (x02 == 1) CydForSnsr.BackColor = System.Drawing.Color.Green;
            if (x02 == 0) CydForSnsr.BackColor = System.Drawing.Color.Gray;

        }
    }
}

 

실린더제어.zip
0.05MB