FrmIOStatus.cs 18.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
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.AssemblyLine
{
    internal partial class FrmIOStatus : FrmBase
    {  
        //internal static readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        internal FrmIOStatus()
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            LoadIOList();
        } 
        Dictionary<string, IOTextControl> DIControlList = new Dictionary<string, IOTextControl>();
        Dictionary<string, IOTextControl> DOControlList = new Dictionary<string, IOTextControl>();
        private void LoadIOList()
        {
            int count = 18;
            int roleindex = 0;
            this.tableLayoutPanel1.RowStyles.Clear();
            this.tableLayoutPanel1.RowCount = count;
            this.tableLayoutPanel3.RowStyles.Clear();
            if (LineManager.Config.DIList.Count > count)
            {
                this.tableLayoutPanel3.RowCount = LineManager.Config.DIList.Count - count;
            }
            else
            { 
                this.tableLayoutPanel3.RowCount =0  ;
            }
            foreach (ConfigIO ioValue in LineManager.Config.DIList.Values)
            {
                IOTextControl control = new IOTextControl(ioValue.ElectricalDefinition + "_" + ioValue.Explain, ioValue.ProName);
                if (DIControlList.Count >= count)
                {
                   // roleindex = DIControlList.Count - count-1;
                    this.tableLayoutPanel3.RowStyles.Add(new RowStyle(SizeType.Absolute, 28));
                    this.tableLayoutPanel3.Controls.Add(control, 0, roleindex - count);                  
                }
                else
                {
                    this.tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 28)); 
                    this.tableLayoutPanel1.Controls.Add(control, 0, roleindex); 
                }
                roleindex++;
                DIControlList.Add(ioValue.ProName, control);
            }
            count = 19;
            tableLayoutPanel2.RowStyles.Clear();
            this.tableLayoutPanel2.RowCount = count;
            this.tableLayoutPanel4.RowStyles.Clear();
            this.tableLayoutPanel4.RowCount = count;
            roleindex = 0;
            foreach (ConfigIO ioValue in LineManager.Config.DOList.Values)
            { 
                IOTextControl control = new IOTextControl(ioValue.ElectricalDefinition + "_" + ioValue.Explain,ioValue.ProName);
                control.Click += Control_Click;
                if (roleindex >= count)
                {
                    this.tableLayoutPanel4.RowStyles.Add(new RowStyle(SizeType.Absolute, 28));
                    this.tableLayoutPanel4.Controls.Add(control, 0, roleindex - count);
                }
                else
                {
                    this.tableLayoutPanel2.RowStyles.Add(new RowStyle(SizeType.Absolute, 28));
                    this.tableLayoutPanel2.Controls.Add(control, 0, roleindex);
                } 
                roleindex++;
                DOControlList.Add(ioValue.ProName, control);
            }
            this.SuspendLayout();    //此处为不闪屏,一定要有的! 

            cmbWriteIO.DataSource = new List<ConfigIO>(LineManager.Config.DOList.Values);
            cmbWriteIO.ValueMember = "ProName";
            cmbWriteIO.DisplayMember = "DisplayStr";
             
        }
        private void Control_Click(object sender, EventArgs e)
        {
            IOTextControl control = (IOTextControl)sender;
            string name = control.Name.Substring(3, control.Name.Length - 3);
            List<string> keyList = new List<string>(DOControlList.Keys);
            int index = keyList.IndexOf(name);
            if (index >= 0)
            {
                cmbWriteIO.SelectedIndex = index;
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {

            if (chbAutoRead.Checked && this.Visible)
            {
                ReadIOList();
               // lblTrayNum.Text = "托盘编码:" + TrayManager.GetTrayNum(0); 
            }
        }

        private void FrmTest_FormClosing(object sender, FormClosingEventArgs e)
        {
           // KNDAIManager.NeedShow = false;
            try
            {
                if (this.timer1.Enabled)
                {
                    this.timer1.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, "Exception(异常)", MessageBoxButtons.OK, MessageBoxIcon.Error);
                LogUtil.error("",ex);
            }
        }
      

        private ConfigIO GetSelectDO()
        {
            string text = cmbWriteIO.SelectedValue.ToString();
            if (LineManager.Config.DOList.ContainsKey(text))
            {
                ConfigIO io = LineManager.Config.DOList[text];
                return io;
            }
            return null;
        }
        IOTextControl selectControl = null;
        private void WriteDO(IO_VALUE value)
        {
            string deviceName = txtDoName.Text;
            int index = FormUtil.GetIntValue(txtDOIndex); 
            int time = FormUtil.GetIntValue(txtWriteTime);
      
            if (time > 0)
            {
                IOManager.instance.WriteSingleDO(deviceName, (byte)0, (ushort)index, value, time);
            }
            else
            {
                IOManager.instance.WriteSingleDO(deviceName, (byte)0, (ushort)index, value);
            }
        }

        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.IO_IP; 
                    IOTextControl newControl = DOControlList[io.ProName];
                    if (selectControl != null) { selectControl.BackColor = Color.White; }
                    newControl.BackColor = Color.SkyBlue;
                    selectControl = newControl;
                    
                }
            }
        }
        private void btnWriteSingleDO_Click(object sender, EventArgs e)
        {
            WriteDO(IO_VALUE.HIGH);
        }
        private void button1_Click(object sender, EventArgs e)
        { 
            WriteDO(IO_VALUE.LOW);
        }
        private void FrmStoreIOStatus_Load(object sender, EventArgs e)
        {
            // KNDAIManager.NeedShow = true;
            //tabControl1.TabPages.RemoveAt(1);
        }
     
        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 btnReadIO_Click(object sender, EventArgs e)
        {
            ReadIOList();
        }

        private void ReadIOList()
        {
            foreach (string key in DIControlList.Keys)
            {
                IOTextControl control = DIControlList[key];
                int iov = (int)IOManager.IOValue(key, 0);
                if (iov != control.IOValue)
                {
                    control.IOValue = iov;
                    control.ShowData();
                }
            }
            foreach (string key in this.DOControlList.Keys)
            {
                IOTextControl control = DOControlList[key];
                int iov = (int)IOManager.DOValue(key, 0);
                if (iov != control.IOValue)
                {
                    control.IOValue = iov;
                    control.ShowData();
                }
            } 
        }
          
        private void FrmIOStatus_Shown(object sender, EventArgs e)
        {  
            timer1.Start();
        }
        protected void BtnMove(Button btn, string defaultText, string targetText, string ioHighType)
        {
            LogUtil.info(LineManager.Line.Name+ "点击【" + btn.Text + "】 ");
            if (btn.Text.Equals(defaultText))
            {
                LineManager.Line.IOMove(ioHighType, IO_VALUE.HIGH);
                btn.Text = targetText;
                btn.BackColor = Color.Aqua;
            }
            else
            {
                LineManager.Line.IOMove(ioHighType, IO_VALUE.LOW);
                btn.Text = defaultText;
                btn.BackColor = Color.White;
            }
        }

        protected void BtnMove(Button btn, string defaultText, string targetText, string ioLowType, string ioHighType)
        {
            LogUtil.info(LineManager.Line.Name + "点击【" + btn.Text + "】 ");
            if (btn.Text.Equals(defaultText))
            {
                LineManager.Line.CylinderMove(null, ioLowType, ioHighType);
                btn.Text = targetText;
                btn.BackColor = Color.Aqua;
            }
            else
            {
                LineManager.Line.CylinderMove(null, ioHighType, ioLowType);
                btn.Text = defaultText;
                btn.BackColor = Color.SkyBlue;
            }
        }
        private void btnDriveMotor_Run_Click(object sender, EventArgs e)
        {
            if (btnDriveMotor_Run.Text.Equals("驱动电机1234正转"))
            {
                btnDriveMotor_Run.Text = "驱动电机1234停止";
                btnDriveMotor_Run.BackColor = Color.Aqua;
                LineManager.Line.WriteDrivetMotorRun(IO_VALUE.HIGH);
            }
            else
            {
                LineManager.Line.WriteDrivetMotorRun(IO_VALUE.LOW); 
                btnDriveMotor_Run.Text = "驱动电机1234正转";
                btnDriveMotor_Run.BackColor = Color.White;
            }
        }
         
        private void btnSWRun_Click(object sender, EventArgs e)
        {
            BtnMove(btnSW4Run, "环形线横移4电机运转", "环形线横移4电机停止", IO_Type.SW4_MotorRun); 
        } 

        private void btnNGCylinder_Click(object sender, EventArgs e)
        {
            BtnMove(btnNGCylinder, "NG料盘气缸前进", "NG料盘气缸后退", IO_Type.NGCylinder_After, IO_Type.NGCylinder_Before); 
        }

         
        private void btnCloseAll_Click(object sender, EventArgs e)
        {
            IOManager.instance.CloseAllDO();
        }

        private void btnSw1Run_Click(object sender, EventArgs e)
        { 
            BtnMove(btnSw1Run, "环形线横移1电机运转", "环形线横移1电机停止", IO_Type.SW1_MotorRun);
        }

        private void btnSw2Run_Click(object sender, EventArgs e)
        { 
            BtnMove(btnSw2Run, "环形线横移2电机运转", "环形线横移2电机停止", IO_Type.SW2_MotorRun);
        }

        private void btnSw3Run_Click(object sender, EventArgs e)
        { 
            BtnMove(btnSw3Run, "环形线横移3电机运转", "环形线横移3电机停止", IO_Type.SW3_MotorRun);
        }

        private void btnSw1Location_Click(object sender, EventArgs e)
        {
            BtnMove(btnSw1Location, "环形线横移1定位上升", "环形线横移1定位下降", IO_Type.SW1_LocationCylinder_Down, IO_Type.SW1_LocationCylinder_Up);
        } 
        private void btnSw2Location_Click(object sender, EventArgs e)
        {
            BtnMove(btnSw2Location, "环形线横移2定位上升", "环形线横移2定位下降", IO_Type.SW2_LocationCylinder_Down, IO_Type.SW2_LocationCylinder_Up);
        } 
        private void btnSw3Location_Click(object sender, EventArgs e)
        {
            BtnMove(btnSw3Location, "环形线横移3定位上升", "环形线横移3定位下降", IO_Type.SW3_LocationCylinder_Down, IO_Type.SW3_LocationCylinder_Up); 
        } 
        private void btnSw4Location_Click(object sender, EventArgs e)
        { 
            BtnMove(btnSw4Location, "环形线横移4定位上升", "环形线横移4定位下降", IO_Type.SW4_LocationCylinder_Down, IO_Type.SW4_LocationCylinder_Up);
        }

        private void btnSw1Top_Click(object sender, EventArgs e)
        {
            BtnMove(btnSw1Top, "环形线横移1顶升上升", "环形线横移1顶升下降", IO_Type.SW1_TopCylinder_Down, IO_Type.SW1_TopCylinder_Up);
        }

        private void btnSw2Top_Click(object sender, EventArgs e)
        {
            BtnMove(btnSw2Top, "环形线横移2顶升上升", "环形线横移2顶升下降", IO_Type.SW2_TopCylinder_Down, IO_Type.SW2_TopCylinder_Up);
        }

        private void btnSw3Top_Click(object sender, EventArgs e)
        {
            BtnMove(btnSw3Top, "环形线横移3顶升上升", "环形线横移3顶升下降", IO_Type.SW3_TopCylinder_Down, IO_Type.SW3_TopCylinder_Up);
        }
        private void btn34Top_Click(object sender, EventArgs e)
        {
            BtnMove(btnSw4Top, "环形线横移4顶升上升", "环形线横移4顶升下降", IO_Type.SW4_TopCylinder_Down, IO_Type.SW4_TopCylinder_Up);
        }

        private void btnsw2StopDown_Click(object sender, EventArgs e)
        {
            
            BtnMove(btnsw2StopDown, "环形线横移2阻挡下降", "环形线横移2阻挡上升", IO_Type.SW2_StopDown);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            BtnMove(button2, "环形线横移4阻挡下降", "环形线横移4阻挡上升", IO_Type.SW4_StopDown);
        }

        private void btnShuntStop1_Click(object sender, EventArgs e)
        {
            BtnMove(btnShuntStop1, "分流前端阻挡下降", "分流前端阻挡上升", IO_Type.Shunt_StopDown1_Front); 
        }

        private void btnShuntStop2_Click(object sender, EventArgs e)
        {  
            BtnMove(btnShuntStop2, "分流后端阻挡下降", "分流后端阻挡上升", IO_Type.Shunt_StopDown2_Back);
        }

        private void btnShuntupdown1_Click(object sender, EventArgs e)
        {

            BtnMove(btnShuntupdown1 , "分流横移机构1上升", "分流横移机构1下降", IO_Type.Shunt_TopCylinder_Down1, IO_Type.Shunt_TopCylinder_Up1);
        }

        private void btnShuntupdown2_Click(object sender, EventArgs e)
        {

            BtnMove(btnShuntupdown2, "分流横移机构2上升", "分流横移机构2下降", IO_Type.Shunt_TopCylinder_Down2, IO_Type.Shunt_TopCylinder_Up2);
        }

        private void btnShuntRun1_Click(object sender, EventArgs e)
        { 
            BtnMove(btnShuntRun1, "分流横移机构1皮带运行", "分流横移机构1皮带停止", IO_Type.Shunt_MotorRun1);
        }

        private void btnShuntRun2_Click(object sender, EventArgs e)
        { 
            BtnMove(btnShuntRun2, "分流横移机构2皮带运行", "分流横移机构2皮带停止", IO_Type.Shunt_MotorRun2);
        }

        private void btnShuntClose_Click(object sender, EventArgs e)
        {
            IOManager.IOMove(IO_Type.Shunt_StopDown1_Front, IO_VALUE.LOW);
            IOManager.IOMove(IO_Type.Shunt_StopDown2_Back, IO_VALUE.LOW);
            IOManager.IOMove(IO_Type.Shunt_TopCylinder_Down1, IO_VALUE.LOW);
            IOManager.IOMove(IO_Type.Shunt_TopCylinder_Up1, IO_VALUE.LOW);
            IOManager.IOMove(IO_Type.Shunt_TopCylinder_Down2, IO_VALUE.LOW);
            IOManager.IOMove(IO_Type.Shunt_TopCylinder_Up2, IO_VALUE.LOW);
            IOManager.IOMove(IO_Type.Shunt_MotorRun1, IO_VALUE.LOW);
            IOManager.IOMove(IO_Type.Shunt_MotorRun2, IO_VALUE.LOW); 
        }

        private void btnShunt2Stop1_Click(object sender, EventArgs e)
        {
            BtnMove(btnShunt2Stop1, "分流2-前端阻挡下降", "分流2-前端阻挡上升", IO_Type.Shunt2_StopDown1_Front);
        }
        private void btnShunt2Stop2_Click(object sender, EventArgs e)
        {
            BtnMove(btnShunt2Stop2, "分流2-后端阻挡下降", "分流2-后端阻挡上升", IO_Type.Shunt2_StopDown2_Back);
        }
        private void btnShunt2updown1_Click(object sender, EventArgs e)
        {
            BtnMove(btnShunt2updown1, "分流2-机构1上升", "分流2-机构1下降", IO_Type.Shunt2_TopCylinder_Down1, IO_Type.Shunt2_TopCylinder_Up1);
        }
        private void btnShunt2updown2_Click(object sender, EventArgs e)
        {
            BtnMove(btnShunt2updown2, "分流2-机构1上升", "分流2-机构1下降", IO_Type.Shunt2_TopCylinder_Down2, IO_Type.Shunt2_TopCylinder_Up2);
        }
        private void btnShunt2Run1_Click(object sender, EventArgs e)
        {
            BtnMove(btnShunt2Run1, "分流2-机构1皮带运行", "分流2-机构1皮带停止", IO_Type.Shunt2_MotorRun1);
        }
        private void btnShunt2Run2_Click(object sender, EventArgs e)
        {
            BtnMove(btnShunt2Run2, "分流2-机构2皮带运行", "分流2-机构2皮带停止", IO_Type.Shunt2_MotorRun2);
        }
        private void btnShunt2Close_Click(object sender, EventArgs e)
        {
            IOManager.IOMove(IO_Type.Shunt2_StopDown1_Front, IO_VALUE.LOW);
            IOManager.IOMove(IO_Type.Shunt2_StopDown2_Back, IO_VALUE.LOW);
            IOManager.IOMove(IO_Type.Shunt2_TopCylinder_Down1, IO_VALUE.LOW);
            IOManager.IOMove(IO_Type.Shunt2_TopCylinder_Up1, IO_VALUE.LOW);
            IOManager.IOMove(IO_Type.Shunt2_TopCylinder_Down2, IO_VALUE.LOW);
            IOManager.IOMove(IO_Type.Shunt2_TopCylinder_Up2, IO_VALUE.LOW);
            IOManager.IOMove(IO_Type.Shunt2_MotorRun1, IO_VALUE.LOW);
            IOManager.IOMove(IO_Type.Shunt2_MotorRun2, IO_VALUE.LOW);
        }

        private void btmMove5StopDown1_Click(object sender, EventArgs e)
        {

            BtnMove(btnMove5StopDown1, "分流2-进仓5阻挡1下降", "分流2-进仓5阻挡1上升", IO_Type.StopCylinder_Down1);
        }
    }
}