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 System.Runtime.InteropServices;

namespace GcDevicePc
{
    public partial class PID_Form : Form
    {
        public PID_Form()
        {
            InitializeComponent();
        }

        private void changemodbus_Click(object sender, EventArgs e)
        {
        
            int ret;
            ushort[] buf = new ushort[18];

            buf[0] = Convert.ToUInt16(jy1text1.Text);

            buf[1] = Convert.ToUInt16(jy1text2.Text);
            buf[2] = Convert.ToUInt16(jy1text3.Text);

            buf[3] = Convert.ToUInt16(jy2text1.Text);
            buf[4] = Convert.ToUInt16(jy2text2.Text);
            buf[5] = Convert.ToUInt16(jy2text3.Text);

            buf[6] = Convert.ToUInt16(jc1text1.Text);
            buf[7] = Convert.ToUInt16(jc1text2.Text);
            buf[8] = Convert.ToUInt16(jc1text3.Text);

            buf[9] = Convert.ToUInt16(jc2text1.Text);
            buf[10] = Convert.ToUInt16(jc2text2.Text);
            buf[11] = Convert.ToUInt16(jc2text3.Text);

            buf[12] = Convert.ToUInt16(zx1text1.Text);
            buf[13] = Convert.ToUInt16(zx1text2.Text);
            buf[14] = Convert.ToUInt16(zx1text3.Text);

            buf[15] = Convert.ToUInt16(zx1text1.Text);
            buf[16] = Convert.ToUInt16(zx1text2.Text);
            buf[17] = Convert.ToUInt16(zx1text3.Text);          

            if (globaldata.connection_ip != null){
                ret = globaldata.m_modbus.WriteMultipleRegs(70, 18, globaldata.connection_ip, globaldata.datamodbusport, ref buf);
            }                   
        }

        private void InitTextBox_Value()
        {
            int ret;
            ushort[] buf = new ushort[18];
            if(globaldata.connection_ip != null)
            {
                ret = globaldata.m_modbus.ReadHoldingReg(70, 18, globaldata.connection_ip, globaldata.datamodbusport, ref buf);
            }
            jy1text1.Text = buf[0].ToString();
            jy1text2.Text = buf[1].ToString();
            jy1text3.Text = buf[2].ToString();
            jy2text1.Text = buf[3].ToString();
            jy2text2.Text = buf[4].ToString();
            jy2text3.Text = buf[5].ToString();
            jc1text1.Text = buf[6].ToString();
            jc1text2.Text = buf[7].ToString();
            jc1text3.Text = buf[8].ToString();
            jc2text1.Text = buf[9].ToString();
            jc2text2.Text = buf[10].ToString();
            jc2text3.Text = buf[11].ToString();
            zx1text1.Text = buf[12].ToString();
            zx1text2.Text = buf[13].ToString();
            zx1text3.Text = buf[14].ToString();
            zx1text1.Text = buf[15].ToString();
            zx1text2.Text = buf[16].ToString();
            zx1text3.Text = buf[17].ToString();
        }

        private void resetbtn_Click(object sender, EventArgs e)
        {
            InitTextBox_Value();
        }

        private void PID_Form_Load(object sender, EventArgs e)
        {
            InitTextBox_Value();
        }
    }
}
