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;

// 2022.07.02
// 맨위의 창에서 "Any CPU" 우측에 빌드 타겟을 AdvancedHMIcs 로 변경
// Ctr+Shift+B : 프로젝트 모두 빌드하면 왼쪽 도구항목에 AdvancedHMI 컴포넌트 보임
// 그리고 c# 참조에서 추가할 것 : 자동으로 추가가 안됨 ...!!!!!
// "MfgControl.AdvancedHMI.Controls.dll" + "MfgControl.AdvancedHMI.Drivers.dll"
// 시작 Base Address = 1 : 따라서 읽을 주소에 1 추가할 것
namespace AdvancedHMICS
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            btnDisconnect.Enabled = false;
            tbModbusAddess.Text = modbusTCPCom1.IPAddress.ToString();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            modbusTCPCom1.IPAddress = tboxIP.Text;
            tbModbusAddess.Text = modbusTCPCom1.IPAddress.ToString();
            btnConnect.BackColor = Color.Aqua;
            btnConnect.Enabled = false;
            btnDisconnect.Enabled = true;
        }

        private void btnDisconnect_Click(object sender, EventArgs e)
        {
            btnConnect.Enabled = true;
            btnDisconnect.Enabled = false;
        }

        private void btnCoilRead_Click(object sender, EventArgs e)
        {
            tbCoilValue.Text = modbusTCPCom1.Read(tbCoilAddess.Text);
        }

        private void btnDinputRead_Click(object sender, EventArgs e)
        {
            tbDinputValue.Text = modbusTCPCom1.Read(tbInputAddess.Text);
        }

        private void btnAinputRead_Click(object sender, EventArgs e)
        {
            tbAinputValue.Text = modbusTCPCom1.Read(tbAnalogueAddess.Text);
        }

        private void btnHregRead_Click(object sender, EventArgs e)
        {
            tbHregValue.Text = modbusTCPCom1.Read(tnhRegAddess.Text);
        }

        private void btnCoilWrite_Click(object sender, EventArgs e)
        {
            modbusTCPCom1.Write(tbCoilAddess.Text, tbCoilValue.Text);
        }

        private void btnDinputWrite_Click(object sender, EventArgs e)
        {
            modbusTCPCom1.Write(tbInputAddess.Text, tbDinputValue.Text);
        }

        private void btnAinputWrite_Click(object sender, EventArgs e)
        {
            modbusTCPCom1.Write(tbAnalogueAddess.Text, tbAinputValue.Text);
        }

        private void btnHregWrite_Click(object sender, EventArgs e)
        {
            modbusTCPCom1.Write(tnhRegAddess.Text, tbHregValue.Text);
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            tbCoilValue.Text = modbusTCPCom1.Read("00101");
        }
    }
}
