FrmIOStatus.cs 12.6 KB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.CompilerServices;
using System.Threading;
using System.IO;
using System.Runtime.InteropServices;
using OnlineStore.DeviceLibrary;
using log4net;
using System.Reflection;
using UserFromControl;
using OnlineStore.LoadCSVLibrary;
using OnlineStore.Common;


namespace OnlineStore.AutoInOutStore
{
    public partial class FrmIOStatus : FrmBase
    {
        private int StoreId = 1;
        private AC_SA_BoxBean boxBean;
        public static readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        public FrmIOStatus()
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
        }
        public FrmIOStatus(AC_SA_BoxBean store)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();

            this.boxBean = store;
            this.StoreId = store.StoreID;
            LoadIOList();
        }

        Dictionary<string, IOTextControl> DIControlList = new Dictionary<string, IOTextControl>();
        Dictionary<string, IOTextControl> DOControlList = new Dictionary<string, IOTextControl>();
        private void LoadIOList()
        {
            int roleindex = 0;
            this.tableLayoutPanel1.RowStyles.Clear();
            this.tableLayoutPanel1.RowCount = boxBean.Config.StoreDIList.Count;
            foreach (ConfigIO ioValue in boxBean.Config.StoreDIList.Values)
            {
                this.tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 28));
                IOTextControl control = new IOTextControl();
                control.IOName = ioValue.ElectricalDefinition + "_" + ioValue.Explain;
                control.IOValue = 0;
                control.isCanClick = false;
                control.Name = "IO_" + ioValue.ProName;
                control.Size = new System.Drawing.Size(200, 25);
                control.TabIndex = 0;
                this.tableLayoutPanel1.Controls.Add(control, 0, roleindex);
                roleindex++;
                DIControlList.Add(ioValue.ProName, control);
            }

            tableLayoutPanel2.RowStyles.Clear();
            this.tableLayoutPanel2.RowCount = boxBean.Config.StoreDOList.Count;
            roleindex = 0;
            foreach (ConfigIO ioValue in boxBean.Config.StoreDOList.Values)
            {
                this.tableLayoutPanel2.RowStyles.Add(new RowStyle(SizeType.Absolute, 28));
                IOTextControl control = new IOTextControl();
                control.IOName = ioValue.ElectricalDefinition + "_" + ioValue.Explain;
                control.IOValue = 0;
                control.isCanClick = true;
                //control.Location = new System.Drawing.Point(0, 25*roleindex);
                control.Name = "IO_" + ioValue.ProName;
                control.Size = new System.Drawing.Size(200, 25);
                control.TabIndex = 0;
                this.tableLayoutPanel2.Controls.Add(control, 0, roleindex);
                roleindex++;
                DOControlList.Add(ioValue.ProName, control);
            }
            this.SuspendLayout();    //此处为不闪屏,一定要有的! 

            cmbWriteIO.DataSource = new List<ConfigIO>(boxBean.Config.StoreDOList.Values);
            cmbWriteIO.ValueMember = "ProName";
            cmbWriteIO.DisplayMember = "DisplayStr";
            cmbWriteValue.SelectedIndex = 0; 
        }

        private void timer1_Tick(object sender, EventArgs e)
        { 
            ReadIOList();
        }

        private void FrmTest_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (this.timer1.Enabled)
                {
                    this.timer1.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, "Exception(异常)", MessageBoxButtons.OK, MessageBoxIcon.Error);
                LogUtil.error(LOGGER, ex.StackTrace);
            }
        }
        private void btnWriteSingleDO_Click(object sender, EventArgs e)
        {
            string deviceName = txtDoName.Text;
            int index = FormUtil.GetIntValue(txtDOIndex);
            IO_VALUE value = (IO_VALUE)cmbWriteValue.SelectedIndex;
            int time = FormUtil.GetIntValue(txtWriteTime);
            int slaveId = FormUtil.GetIntValue(txtSlaveId);
            if (time > 0)
            {
                KNDManager.WriteSingleDO(deviceName, (byte)slaveId, (ushort)index, (IO_VALUE)value, time);
            }
            else
            {
                KNDManager.WriteSingleDO(deviceName, (byte)slaveId, (ushort)index, (IO_VALUE)value);
            }
        }

        private ConfigIO GetSelectDO()
        {
            string text = cmbWriteIO.SelectedValue.ToString();
            if (boxBean.Config.StoreDOList.ContainsKey(text))
            {
                ConfigIO io = boxBean.Config.StoreDOList[text];
                return io;
            }
            return null;
        }
        IOTextControl selectControl = null;
        private void cmbWriteIO_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbWriteIO.SelectedIndex >= 0)
            {
                ConfigIO io = GetSelectDO();
                if (io != null)
                {
                    // txtIp.Text = io.DeviceName;
                    txtDOIndex.Text = io.GetIOAddr().ToString();
                    txtDoName.Text = io.DeviceName;
                    txtSlaveId.Text = io.SlaveID.ToString();
                    IOTextControl newControl = DOControlList[io.ProName];
                    selectControl = newControl;
                }
            }
        }

        private void FrmStoreIOStatus_Load(object sender, EventArgs e)
        {
            btnDisDoorOpen.Visible = StoreManager.HasDisableDoorControl;
            btnDisDoorClose.Visible = StoreManager.HasDisableDoorControl;
        }

        private void cmbWriteIO_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index < 0)
            {
                return;
            }
            e.DrawBackground();
            e.DrawFocusRectangle();
            if (cmbWriteIO.Items.Count > e.Index)
            {
                ConfigIO io = (ConfigIO)cmbWriteIO.Items[e.Index];
                e.Graphics.DrawString(io.DisplayStr, e.Font, new SolidBrush(e.ForeColor), e.Bounds.X, e.Bounds.Y + 3);
            }
        }

        private void cmbWriteValue_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index < 0)
            {
                return;
            }
            e.DrawBackground();
            e.DrawFocusRectangle();
            if (cmbWriteValue.Items.Count > e.Index)
            {
                e.Graphics.DrawString(cmbWriteValue.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds.X, e.Bounds.Y + 3);
            }
        }

        private void btnReadIO_Click(object sender, EventArgs e)
        {
            ReadIOList();
        }

        private void ReadIOList()
        {
            foreach (string key in DIControlList.Keys)
            {
                IOTextControl control = DIControlList[key];
                int iov = (int)KNDManager.GetIOValue(boxBean.Config.StoreDIList[key]);
                if (iov != control.IOValue)
                {
                    control.IOValue = iov;
                    control.ShowData();
                }
            } foreach (string key in this.DOControlList.Keys)
            {
                IOTextControl control = DOControlList[key];
                int iov = (int)KNDManager.GetIOValue(boxBean.Config.StoreDOList[key]);
                if (iov != control.IOValue)
                {
                    control.IOValue = iov;
                    control.ShowData();
                }
            }
             
        }
         
        private void btnReadAllDi_Click(object sender, EventArgs e)
        {
            string deviceName = txtDoName.Text; 
            IO_VALUE value = (IO_VALUE)cmbWriteValue.SelectedIndex;
            int time = FormUtil.GetIntValue(txtWriteTime);
            int slaveId = FormUtil.GetIntValue(txtSlaveId);
            KNDManager.ReadMultipleDI(deviceName, (byte)slaveId, (ushort)KNDManager.DIStartAddress, 16);
        } 
        private void btnReadAllDo_Click(object sender, EventArgs e)
        {
            string deviceName = txtDoName.Text;

            IO_VALUE value = (IO_VALUE)cmbWriteValue.SelectedIndex;
            int time = FormUtil.GetIntValue(txtWriteTime);
            int slaveId = FormUtil.GetIntValue(txtSlaveId);
            KNDManager.ReadMultipleDO(deviceName, (byte)slaveId, (ushort)KNDManager.DoStartAddress, 16);
        } 
        private void btnOpenDoor_Click(object sender, EventArgs e)
        {
            KND.IOMove(IO_Type.Door_Up, IO_VALUE.HIGH);
            KND.IOMove(IO_Type.Door_Down, IO_VALUE.LOW);
        } 
        private void btnCloseDoor_Click(object sender, EventArgs e)
        {
            KND.IOMove(IO_Type.Door_Down, IO_VALUE.HIGH);
            KND.IOMove(IO_Type.Door_Up, IO_VALUE.LOW);
        }

        private void btnLocationUp_Click(object sender, EventArgs e)
        {
            KND.IOMove(IO_Type.LocationCylinder_Down, IO_VALUE.LOW);
            KND.IOMove(IO_Type.LocationCylinder_Up, IO_VALUE.HIGH);
        }

        private void btnLocationDown_Click(object sender, EventArgs e)
        {
            KND.IOMove(IO_Type.LocationCylinder_Down, IO_VALUE.HIGH);
            KND.IOMove(IO_Type.LocationCylinder_Up, IO_VALUE.LOW); 
        }

        private void FrmIOStatus_Shown(object sender, EventArgs e)
        { 
            SetSkin(this);
        }

        private void btnOpenAxisBreak_Click(object sender, EventArgs e)
        {
            KND.IOMove(IO_Type.Axis_Brake, IO_VALUE.HIGH);
        }

        private void btnCloseAxisBreak_Click(object sender, EventArgs e)
        { 
            KND.IOMove(IO_Type.Axis_Brake, IO_VALUE.LOW);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOpenLed_Click(object sender, EventArgs e)
        { 
            KND.IOMove(IO_Type.CameraLight_Power, IO_VALUE.HIGH);
        }

        private void btnCloseLed_Click(object sender, EventArgs e)
        { 
            KND.IOMove(IO_Type.CameraLight_Power, IO_VALUE.LOW);
        }

        private void btnSuckingDiscUp_Click(object sender, EventArgs e)
        {
            KND.IOMove(IO_Type.SuckingDisc_Down, IO_VALUE.LOW);
            KND.IOMove(IO_Type.SuckingDisc_Up, IO_VALUE.HIGH);
        }

        private void btnSuckingDiscDown_Click(object sender, EventArgs e)
        {
            KND.IOMove(IO_Type.SuckingDisc_Up, IO_VALUE.LOW);
            KND.IOMove(IO_Type.SuckingDisc_Down, IO_VALUE.HIGH);
        }

        private void btnDOpen_Click(object sender, EventArgs e)
        {
            if (AutomaticBaiting.CanOpenBatchDoor()|| StoreManager.Store.storeRunStatus.Equals(StoreRunStatus.Wait))
            {
                AutomaticBaiting.BatchDoorOpen(false);
            }
            else
            {
                MessageBox.Show("忙碌中,无法打开门锁");
            }
        }
        private void btnDClose_Click(object sender, EventArgs e)
        {
            AutomaticBaiting.BatchDoorClose(false );
        }

        private void btnSXi_Click(object sender, EventArgs e)
        { 
            KND.IOMove(IO_Type.SuckingDisc_Work, IO_VALUE.HIGH);
        }

        private void btnSXil_Click(object sender, EventArgs e)
        { 
            KND.IOMove(IO_Type.SuckingDisc_Work, IO_VALUE.LOW);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            KND.IOMove(IO_Type.Run_Sign, IO_VALUE.HIGH);
        }

        private void button2_Click(object sender, EventArgs e)
        { 
            KND.IOMove(IO_Type.Run_Sign, IO_VALUE.LOW);
        }

        private void button5_Click(object sender, EventArgs e)
        { 
            KND.IOMove(IO_Type.StartOrStopBlow, IO_VALUE.HIGH);
        }

        private void button4_Click(object sender, EventArgs e)
        { 
            KND.IOMove(IO_Type.StartOrStopBlow, IO_VALUE.LOW);
        }

        private void btnDisDoorOpen_Click(object sender, EventArgs e)
        {
            KND.IOMove(IO_Type.DisableDoorControl, IO_VALUE.HIGH);
        }

        private void btnDisDoorClose_Click(object sender, EventArgs e)
        {
            KND.IOMove(IO_Type.DisableDoorControl, IO_VALUE.LOW);
        }
    }
}