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 AR;
using Modbus;
using Modbus.Device;
namespace DigitalIndicator
{
    public partial class Form1 : Form
    {
        ModbusSerialMaster modbus;
        System.IO.Ports.SerialPort dev;
        public Form1()
        {
            InitializeComponent();
            dev = new System.IO.Ports.SerialPort();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (tbport.Text.isEmpty())
            {
                UTIL.MsgE("포트번호를 입력하세요");
                return;
            }
            if (int.TryParse(tbbaud.Text, out int port) == false)
            {
                UTIL.MsgE("baud rate 값을 입력하세요\n(기본:9600)");
                return;
            }

            if (dev.IsOpen)
            {
                if (modbus != null) modbus.Dispose();
                dev.Close();
                UTIL.MsgI("연결이 종료되었습니다");

            }
            else
            {
                dev.PortName = this.tbport.Text;
                dev.DataBits = 8;    // 데이터 비트
                dev.Parity = System.IO.Ports.Parity.None;
                dev.StopBits = System.IO.Ports.StopBits.One;
                try
                {
                    dev.Open();
                }
                catch (Exception ex)
                {
                    UTIL.MsgE(ex.Message);
                    return;
                }

                // Modbus 마스터 생성
                modbus = ModbusSerialMaster.CreateRtu(dev);
            }




        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.button1.Text = (this.dev?.IsOpen ?? false) ? "연결끊기" : "연결";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (Int32.TryParse(this.textBox1.Text, out int value) == false)
            {
                UTIL.MsgE("숫자로 입력하세요");
                return;
            }
            if (this.textBox1.TextLength > 5)
            {
                UTIL.MsgE("5자리를 초과할 수 없습니다");
                return;
            }

            var valuebuffer = splitI32(value);
            var slave = (byte)numericUpDown1.Value;
            modbus.WriteMultipleRegisters(slave, 0, valuebuffer); //ADDRESS 0=40001

        }


        UInt16[] splitI32(Int32 value)
        {
            var hValue = (UInt16)(value >> 16);
            var lValue = (UInt16)(value & 0xFFFF);
            return new ushort[] { hValue, lValue };
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (Int32.TryParse(this.textBox1.Text, out int value) == false)
            {
                UTIL.MsgE("숫자로 입력하세요");
                return;
            }


            var valuebuffer = new ushort[20];
            valuebuffer[0] = 100;
            for (int i = 1; i < valuebuffer.Length; i++) valuebuffer[i] = 0;
            var slave = (byte)numericUpDown1.Value;
            modbus.WriteMultipleRegisters(slave, 0, valuebuffer); //ADDRESS 0=40001
        }
    }
}
