FrmXRay.cs 18.8 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 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
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;
using static OnlineStore.DeviceLibrary.CountParam;

namespace OnlineStore.AutoCountClient
{
    internal partial class FrmXRay : FrmEquipBase
    {
        private SignType DeviceType = SignType.AUTO;
        private bool IsLoad = false;

        private X_RAY_Equip equipBean;
        internal FrmXRay(X_RAY_Equip moveEquip)
        {
            equipBase = moveEquip;
            this.equipBean = moveEquip;
            Control.CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            LoadIOList();
        }

        private void FrmXRay_Shown(object sender, EventArgs e)
        {
        }

        private void FrmStoreIOStatus_Load(object sender, EventArgs e)
        {
            cmbType.Items.AddRange(new string[] { "默认算法", "算法A", "算法B", "算法C", "算法D", "算法E" });
            cmbType.SelectedIndex = (DeviceType==SignType.AUTO?0: (int)DeviceType);
            chbDebug.Checked = equipBean.IsDebug;

            lblCom.Text = "X射线端口号:" + equipBean.Config.XRay_Port;
            lblDianl.Text = "电流:" + equipBean.Config.XRay_Electricity;
            lblDiany.Text = "电压:" + equipBean.Config.XRay_Voltage;
            //lblDiany.Text = "窗宽:" + equipBean.Config.WindowWidth + ",窗位:" + equipBean.Config.WindowLevel;
            lblData.Text = "最后使用时间:" + ConfigAppSettings.GetValue(Setting_Init.XRay_Data);
            lblLastS.Text = equipBean.LastXRayState;
            lblName.Text = equipBean.Name;
            numTh.Value = equipBean.ThresholdValue;
            IsLoad = true;
        }


        private void LoadIOList()
        {
            int roleindex = 0;
            this.tableLayoutPanel1.RowStyles.Clear();
            this.tableLayoutPanel1.RowCount = equipBean.Config.DIList.Count;
            foreach (ConfigIO ioValue in equipBean.Config.DIList.Values)
            {
                this.tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 28));
                IOTextControl control = new IOTextControl(ioValue.ElectricalDefinition + "_" + ioValue.Explain, ioValue.ProName);
                this.tableLayoutPanel1.Controls.Add(control, 0, roleindex);
                roleindex++;
                DIControlList.Add(ioValue.ProName, control);
            }

            tableLayoutPanel2.RowStyles.Clear();
            this.tableLayoutPanel2.RowCount = equipBean.Config.DOList.Count;
            roleindex = 0;
            foreach (ConfigIO ioValue in equipBean.Config.DOList.Values)
            {
                this.tableLayoutPanel2.RowStyles.Add(new RowStyle(SizeType.Absolute, 28));
                IOTextControl control = new IOTextControl(ioValue.ElectricalDefinition + "_" + ioValue.Explain, ioValue.ProName);
                control.Click += Control_Click;
                this.tableLayoutPanel2.Controls.Add(control, 0, roleindex);
                roleindex++;
                DOControlList.Add(ioValue.ProName, control);
            }
            this.SuspendLayout();    //此处为不闪屏,一定要有的! 

            cmbWriteIO.DataSource = new List<ConfigIO>(equipBean.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)
        {
            try
            {
                if (!this.Visible)
                {
                    return;
                }
                this.chbMoveStop.Checked = equipBean.MoveStop;
                ReadIOList();
                ReadBtnDO();
                lblOpen.Visible =(! equipBean.OpenXLine);
                lblLastS.Text = equipBean.LastXRayState;
                if (equipBean.xRay!=null && equipBean.xRay.IsRayOpen)
                {
                    lblXrOpen.Text = "XRay射线已打开";
                }
                else
                {
                    lblXrOpen.Text = "";
                }
                if (equipBean.runStatus > RobotRunStatus.Wait)
                {
                    lblName.BackColor = Color.Green;
                    if (btnStart.Enabled)
                    {
                        FormStatus(true);
                    }
                }
                else
                {
                    lblName.BackColor = System.Drawing.Color.DodgerBlue;
                    if (!btnStart.Enabled)
                    {
                        FormStatus(false);
                    }
                }
                if (equipBean.MoveInfo.MoveType.Equals(RobotMoveType.Working) || equipBean.MoveInfo.MoveType.Equals(RobotMoveType.Labelling))
                {
                    lblInoutInfo.Text = (equipBean.MoveInfo.MoveType.Equals(RobotMoveType.Working) ? "工作中:" : "空闲中") + equipBean.MoveInfo.MoveParam.ToStr();
                }
                else
                {
                    lblInoutInfo.Text = "";
                }
                lblMoveInfo.Text = equipBean.GetMoveStr();
                lblStoreStatus.Text = RobotBase.GetRunStr(equipBean.runStatus);
                lblThisSta.Text = equipBean.WarnMsg;
            }
            catch (Exception ex)
            {
                LogUtil.error(equipBean.Name + "界面定时器出错:", ex);
            }
        }

        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 void btnOpenDo_Click(object sender, EventArgs e)
        {
            WriteDO(IO_VALUE.HIGH);
        }
        private void btnWriteSingleDO_Click(object sender, EventArgs e)
        {
            WriteDO(IO_VALUE.LOW);
        }
        private void WriteDO(IO_VALUE value)
        {
            string IP = txtDoName.Text;
            int index = FormUtil.GetIntValue(txtDOIndex);

            ConfigIO io = GetSelectDO();
            if (io.ProName.Equals(IO_Type.X_Lock_On) && value.Equals(IO_VALUE.HIGH))
            {
                if (!equipBean.LockOnCanMove())
                {
                    MessageBox.Show("X光机互锁信号不能打开, 请先检查 急停/门禁/防护门 ", "警告 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            //   IO_VALUE value = checkBox1.Checked ? IO_VALUE.HIGH : IO_VALUE.LOW;
            int time = FormUtil.GetIntValue(txtWriteTime);
            int slaveId = FormUtil.GetIntValue(txtSlaveId);
            if (time > 0)
            {
                IOManager.instance.WriteSingleDO(IP, (byte)slaveId, (ushort)index, (IO_VALUE)value, time);
            }
            else
            {
                IOManager.instance.WriteSingleDO(IP, (byte)slaveId, (ushort)index, (IO_VALUE)value);
            }
        }


        private ConfigIO GetSelectDO()
        {
            string text = cmbWriteIO.SelectedValue.ToString();
            if (equipBean.Config.DOList.ContainsKey(text))
            {
                ConfigIO io = equipBean.Config.DOList[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.IO_IP;
                    txtSlaveId.Text = io.SlaveID.ToString();
                    IOTextControl newControl = DOControlList[io.ProName];
                    if (selectControl != null) { selectControl.BorderStyle = BorderStyle.None; }
                    newControl.BorderStyle = BorderStyle.FixedSingle;
                    // newControl.BackColor = Color.SkyBlue;
                    selectControl = newControl;
                }
            }
        }



        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();
        }
        internal override void FormStatus(bool isStart)
        {
            btnStart.Enabled = true;
            btnStop.Enabled = true;
            if (isStart)
            {
                btnStart.Text = "复位";
            }
            else
            {
                btnStart.Text = "启动调试";
            }
        }


        private void FrmIOStatus_Shown(object sender, EventArgs e)
        {
            LoadDOBtn(groupBox5);
            timer1.Start();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            StartDebug(btnStart);
        }
        private void btnStop_Click(object sender, EventArgs e)
        {
            StopRun(btnStop);
        }


        private void chbDebug_CheckedChanged(object sender, EventArgs e)
        {
            if (!IsLoad)
            {
                return;
            }
            if (!chbDebug.Checked.Equals(equipBean.IsDebug))
            {
                bool IsDebug = chbDebug.Checked;
                string tarStr = IsDebug ? "调试状态" : "非调试状态";

                DialogResult result = MessageBox.Show("确定切换到" + tarStr, "调试状态更改确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                if (result.Equals(DialogResult.Yes))
                {
                    equipBean.IsDebug = IsDebug;
                    equipBean.Config.IsDebug = IsDebug ? 1 : 0;
                    RobotManager.SaveXRayConfig(equipBean.Config);
                    equipBean.ChangeDebug(IsDebug);
                    LogUtil.info(equipBean.Name + "用户切换 调试状态 =" + IsDebug);
                }
            }
        }



        private void btnInDoorUp_Click(object sender, EventArgs e)
        {
            equipBean.CylinderMove(null, IO_Type.X_InDoor_Down, IO_Type.X_InDoor_Up);
        }

        private void btnInDoor_Down_Click(object sender, EventArgs e)
        {
            if (equipBean.InXWork)
            {
                MessageBox.Show("警告:点料过程中,请勿开门", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            equipBean.CylinderMove(null, IO_Type.X_InDoor_Up, IO_Type.X_InDoor_Down);
        }

        private void btnOutDoor_Up_Click(object sender, EventArgs e)
        {
            equipBean.CylinderMove(null, IO_Type.X_OutDoor_Down, IO_Type.X_OutDoor_Up);
        }

        private void btnOutDoor_Down_Click(object sender, EventArgs e)
        {
            if (equipBean.InXWork)
            {
                MessageBox.Show("警告:点料过程中,请勿开门", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            equipBean.CylinderMove(null, IO_Type.X_OutDoor_Up, IO_Type.X_OutDoor_Down);
        }
        private void btnCloseAll_Click(object sender, EventArgs e)
        {

            foreach (Control con in groupBox5.Controls)
            {
                if (con is Button)
                {
                    con.BackColor = BackColor = Color.White;
                }
            }
            foreach (ConfigIO io in equipBase.baseConfig.DOList.Values)
            {
                IOManager.instance.WriteSingleDO(io.IO_IP, io.SlaveID, io.GetIOAddr(), IO_VALUE.LOW);
            }
        }

        private void btnStopCylinder_Up_Click(object sender, EventArgs e)
        {
            equipBean.CylinderMove(null, IO_Type.X_StopCylinder_Down, IO_Type.X_StopCylinder_Up);
        }

        private void btnStopCylinder_Down_Click(object sender, EventArgs e)
        {
            equipBean.CylinderMove(null, IO_Type.X_StopCylinder_Up, IO_Type.X_StopCylinder_Down);
        }

        private void X_Lock_On_Click(object sender, EventArgs e)
        {
            Control c = (Control)sender;
            string ioType = c.Name;
            if (ioType.Equals(IO_Type.X_Lock_On) && (!equipBean.IOValue(IO_Type.X_Lock_On).Equals(IO_VALUE.HIGH)))
            {
                if (!equipBean.LockOnCanMove())
                {
                    MessageBox.Show("X光机互锁信号不能打开, 请先检查 急停/门禁/防护门 ", "警告 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            DoBtnClick(sender);
        }

        private void chbMoveStop_CheckedChanged(object sender, EventArgs e)
        {
            if (!IsLoad)
            {
                return;
            }
            equipBean.MoveStop = chbMoveStop.Checked;
            LogUtil.info(equipBean.Name + "用户切换是否暂停: " + equipBean.MoveStop);
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void btnXStart_Click(object sender, EventArgs e)
        {

            if (!equipBean.xRayCanStart)
            {
                LogUtil.info(equipBean.Name + "用户点击 打开X光 :" + "不能打开X光, 当前状态:" + equipBean.LastXRayState);
                MessageBox.Show("不能打开X光, 当前状态:" + equipBean.LastXRayState, "警告 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (!equipBean.LockOnCanMove())
            {
                LogUtil.info(equipBean.Name + "用户点击 打开X光 :" + "不能打开X光, 请先检查 急停/门禁/防护门");
                MessageBox.Show("不能打开X光, 请先检查 急停/门禁/防护门 ", "警告 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            DialogResult result = MessageBox.Show("确定打开X光,请确保防护门已关闭 ", "确认提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (result.Equals(DialogResult.OK))
            {
                LogUtil.info(equipBean.Name + "用户点击 打开X光 ,调用  xRay.Start");
                equipBean.xRay.Start();
            }
        }

        private void btnXStop_Click(object sender, EventArgs e)
        {
            LogUtil.info(equipBean.Name + "用户点击 关闭X光 ");
            equipBean.xRay.Stop();
        }

        private void btnImage_Click(object sender, EventArgs e)
        {
            LogUtil.info(equipBean.Name + "用户点击 拍照片 ");
            string fileName = equipBean.CapImage();
            MessageBox.Show("获取图片完成:" + fileName);
        }

        private void btnWorkTest_Click(object sender, EventArgs e)
        {
            LogUtil.info(equipBean.Name + "用户点击 点料测试 ");
            try
            {
                string filePath = txtImage.Text.Trim();
                if (!File.Exists(filePath))
                {
                    MessageBox.Show("请先选择正确的图片");
                    return;
                }
                btnWorkTest.Enabled = false;
                txtResult.Text = "点料中...";
                int th = (int)numTh.Value;
                int wsize = (int)numWSize.Value; 
                CountParam param = new CountParam("", th, wsize, DeviceType);
                int outCount = equipBean.GetCountResult(filePath, param, out _);
                txtResult.Text = outCount.ToString();
                LogUtil.info(equipBean.Name + "用户点击 点料测试【" + filePath + "】【" + th + "】【" + wsize + "】 结果:" + outCount);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                LogUtil.error("点料测试出错:" + ex.ToString());
            }
            btnWorkTest.Enabled = true;
        }

        private void btnSaveTh_Click(object sender, EventArgs e)
        {
            int th = (int)numTh.Value;
            ConfigAppSettings.SaveValue(Setting_Init.ThresholdValue, th);
            equipBean.ThresholdValue = th;
            MessageBox.Show("保存阈值[" + th + "]成功!");
            LogUtil.info("用户点击:保存阈值[" + th + "]成功!");
        }

        private void btnSelImage_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.OpenFileDialog openDialog = new System.Windows.Forms.OpenFileDialog();
            openDialog.Title = "请选择要点料的图片";
            openDialog.Filter = "(*.png)|*.png";
             openDialog.DefaultExt = "png";
            openDialog.InitialDirectory = Application.StartupPath + @"\XRAY\tif";
            System.Windows.Forms.DialogResult result = openDialog.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.Cancel)
            {
                return;
            }
            txtImage.Text = openDialog.FileName; 
        }

        private void cmbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!IsLoad)
            {
                return;
            }
            int index = cmbType.SelectedIndex;
            if (index >= 0)
            {
                index--;
                Enum.TryParse<SignType>(index.ToString(), out DeviceType);
                LogUtil.info(Name + " " + equipBean.Name + " 更改 equipBean.DeviceType=" + index);
            }
        }
    }

}