using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Quicker;

namespace wp_CSharp01
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int temp;
            temp =Convert.ToInt32(ComboBox1.SelectedIndex + 1);
            // write long val (32-bit sined integer) to Modbus No.=21
            UserShare.UserSetReg_Long(Convert.ToUInt16(21),out temp);
        }

        private void OFF_04_Click(object sender, EventArgs e)
        {
            // set False to Modbus address 1
            Quicker.UserShare.UserSetCoil(Convert.ToUInt16(4), 0);
        }

        private void OFF_03_Click(object sender, EventArgs e)
        {
            // set False to Modbus address 1
            Quicker.UserShare.UserSetCoil(Convert.ToUInt16(3), 0);
        }

        private void OFF_02_Click(object sender, EventArgs e)
        {
            // set False to Modbus address 1
            Quicker.UserShare.UserSetCoil(Convert.ToUInt16(2), 0);
        }

        private void OFF_01_Click(object sender, EventArgs e)
        {
            // set False to Modbus address 1
            Quicker.UserShare.UserSetCoil(Convert.ToUInt16(1), 0);
        }

        private void ON_04_Click(object sender, EventArgs e)
        {
            // set true to Modbus address 2
            Quicker.UserShare.UserSetCoil(Convert.ToUInt16(4), 1);
        }

        private void ON_03_Click(object sender, EventArgs e)
        {
            // set true to Modbus address 2
            Quicker.UserShare.UserSetCoil(Convert.ToUInt16(3), 1);
        }

        private void ON_02_Click(object sender, EventArgs e)
        {
            // set true to Modbus address 2
            Quicker.UserShare.UserSetCoil(Convert.ToUInt16(2), 1);
        }

        private void ON_01_Click(object sender, EventArgs e)
        {
            // set true to Modbus address 1
            Quicker.UserShare.UserSetCoil(Convert.ToUInt16(1), 1);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int current_mode;

            // enable Timer1
            timer1.Interval = 250;  // 250 ms
            timer1.Enabled = true;

            // read current mode at Modbus address 21
            Quicker.UserShare.UserGetReg_Long(Convert.ToUInt16(21),out current_mode);

            // set initial Mode as 1   (Index = mode - 1)
            if (current_mode >= 1 && current_mode <= 3)  // make sure the mode is between 1 and 3
                ComboBox1.SelectedIndex = current_mode - 1;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            // scan DI and DO every Timer1 interval
            ScanDI();
            ScanDO();
        }

        private void ScanDO()
        {
            byte iValue;

            // get boolean at Modbus No.=1
            Quicker.UserShare.UserGetCoil(Convert.ToUInt16(1), out iValue);
            if (iValue == 1)
                PanelDO1.BackColor = Color.LightGreen;
            else
                PanelDO1.BackColor = Color.DarkGreen;

            // get boolean at Modbus No.=2
            Quicker.UserShare.UserGetCoil(Convert.ToUInt16(2), out iValue);
            if (iValue == 1)
                PanelDO2.BackColor = Color.LightGreen;
            else
                PanelDO2.BackColor = Color.DarkGreen;

            Quicker.UserShare.UserGetCoil(Convert.ToUInt16(3), out iValue);
            if (iValue == 1)
                PanelDO3.BackColor = Color.LightGreen;
            else
                PanelDO3.BackColor = Color.DarkGreen;

            Quicker.UserShare.UserGetCoil(Convert.ToUInt16(4), out iValue);
            if (iValue == 1)
                PanelDO4.BackColor = Color.LightGreen;
            else
                PanelDO4.BackColor = Color.DarkGreen;

            Quicker.UserShare.UserGetCoil(Convert.ToUInt16(5), out iValue);
            if (iValue == 1)
                PanelDO5.BackColor = Color.LightGreen;
            else
                PanelDO5.BackColor = Color.DarkGreen;

            Quicker.UserShare.UserGetCoil(Convert.ToUInt16(6), out iValue);
            if (iValue == 1)
                PanelDO6.BackColor = Color.LightGreen;
            else
                PanelDO6.BackColor = Color.DarkGreen;

            Quicker.UserShare.UserGetCoil(Convert.ToUInt16(7), out iValue);
            if (iValue == 1)
                PanelDO7.BackColor = Color.LightGreen;
            else
                PanelDO7.BackColor = Color.DarkGreen;


            Quicker.UserShare.UserGetCoil(Convert.ToUInt16(8), out iValue);
            if (iValue == 1)
                PanelDO8.BackColor = Color.LightGreen;
            else
                PanelDO8.BackColor = Color.DarkGreen;
        }

        private void ScanDI()
        {
            byte iValue;

            // get boolean at Modbus No.=9
            Quicker.UserShare.UserGetCoil(Convert.ToUInt16(9), out iValue);
            if (iValue == 1)
                PanelDI1.BackColor = Color.Red;
            else
                PanelDI1.BackColor = Color.DarkRed;

            // get boolean value at Modbus No.=10
            Quicker.UserShare.UserGetCoil(Convert.ToUInt16(10), out iValue);
            if (iValue == 1)
                PanelDI2.BackColor = Color.Red;
            else
                PanelDI2.BackColor = Color.DarkRed;

            Quicker.UserShare.UserGetCoil(Convert.ToUInt16(11), out iValue);
            if (iValue == 1)
                PanelDI3.BackColor = Color.Red;
            else
                PanelDI3.BackColor = Color.DarkRed;

            Quicker.UserShare.UserGetCoil(Convert.ToUInt16(12), out iValue);
            if (iValue == 1)
                PanelDI4.BackColor = Color.Red;
            else
                PanelDI4.BackColor = Color.DarkRed;

            Quicker.UserShare.UserGetCoil(Convert.ToUInt16(13), out iValue);
            if (iValue == 1)
                PanelDI5.BackColor = Color.Red;
            else
                PanelDI5.BackColor = Color.DarkRed;

            Quicker.UserShare.UserGetCoil(Convert.ToUInt16(14), out iValue);
            if (iValue == 1)
                PanelDI6.BackColor = Color.Red;
            else
                PanelDI6.BackColor = Color.DarkRed;

            Quicker.UserShare.UserGetCoil(Convert.ToUInt16(15), out iValue);
            if (iValue == 1)
                PanelDI7.BackColor = Color.Red;
            else
                PanelDI7.BackColor = Color.DarkRed;

            Quicker.UserShare.UserGetCoil(Convert.ToUInt16(16), out iValue);
            if (iValue == 1)
                PanelDI8.BackColor = Color.Red;
            else
                PanelDI8.BackColor = Color.DarkRed;
        }            
    }
}