FrmMain.cs 3.4 KB
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 Asa;

namespace AIOBOX_Debug
{
    public partial class FrmMain : Form
    {
        private InputDisplay input;
        private OutputDisplay output;

        public FrmMain()
        {
            InitializeComponent();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            input = new InputDisplay(pictureBox1);
            input.SetCount(16);
            output = new OutputDisplay(pictureBox2);
            output.SetCount(16);
            output.DO_Click += Output_DO_Click;

            Common.box = new Asa.IOModule.AIOBOX();
            Common.box.DI_Changed_Event += DI_Changed;
            Common.box.DO_Changed_Event += DO_Changed;

            Common.localIP = Common.box.GetLocalIP();
            Common.localIndex = 0;
            Common.inputCount = 16;
            Common.outputCount = 16;
            Common.inputSleep = 100;
            Common.outputSleep = 100;
            Common.autoUpload = false;
            Common.autoRead = false;
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            Common.box.Close();
        }

        private void DI_Changed(Asa.IOModule.AIOBOX box, Asa.IOModule.Box_Sta[] sta)
        {
            
            this.Invoke(new Action(() => { input.SetState(sta); }));
        }

        private void DO_Changed(Asa.IOModule.AIOBOX box, Asa.IOModule.Box_Sta[] sta)
        {
            this.Invoke(new Action(() => { output.SetState(sta); }));
        }

        private void Output_DO_Click(int index)
        {
            Asa.IOModule.Box_Sta sta = Common.box.ReadDO(index);
            sta = Common.box.ReverseStatus(sta);
            Common.box.WriteDO(index, sta);
        }

        private void BtnIP_Click(object sender, EventArgs e)
        {
            using (FrmIP frm = new FrmIP { Text = BtnIP.Text })
            {
                frm.ShowDialog();
            }
        }

        private void BtnIO_Click(object sender, EventArgs e)
        {
            using (FrmIO frm = new FrmIO { Text = BtnIO.Text })
            {
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    input.SetCount(Common.inputCount);
                    output.SetCount(Common.outputCount);
                }
            }
        }

        private void BtnCon_Click(object sender, EventArgs e)
        {
            if (Common.box.IsConn)
            {
                Common.box.Close();
                BtnCon.Text = "Connect";
                input.IsRun = false;
                output.IsRun = false;
            }
            else
            {
                if (ChkLog.Checked)
                    Common.box.LogPath(Application.StartupPath + "\\", Asa.IOModule.LogType.All);
                else
                    Common.box.LogPath(null, Asa.IOModule.LogType.All);
                Common.box.Connect();
                if (Common.box.IsConn)
                {
                    BtnCon.Text = "Disconnect";
                    input.IsRun = true;
                    output.IsRun = true;
                }
                else
                {
                    MessageBox.Show(Common.box.ErrInfo);
                }
            }
        }


    }
}