XrayControl.cs 11.5 KB
using DeviceLibrary;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AutoCountMachine
{
    public partial class XrayControl : UserControl
    {
        readonly System.Windows.Forms.Timer t1 = new System.Windows.Forms.Timer();
        DeviceConfig _Config;
        public DeviceConfig Config
        {
            get { return _Config; }
            set
            {
                _Config = value;
                ioControl1.Config = value;
                configControl1.Config = value;
                axisMoveControl1.LoadData(value);
                LoadADIOList();
            }
        }
        public XrayControl()
        {
            InitializeComponent();
            t1.Stop();
            t1.Interval = 500;
            t1.Tick += T1_Tick;
            GC.KeepAlive(t1);
            countstate += XrayControl_countstate;
            textBox_scancode.KeyUp += TextBox_scancode_KeyUp;
            textBox_scancode.Leave += TextBox_scancode_Leave;
            textBox_scancode.Click += TextBox_scancode_Click;
            tabPage5.GotFocus += TabPage5_GotFocus;
            tabPage5.MouseDown += TabPage5_MouseDown;
            RoleManger.RoleChange += RoleManger_RoleChange;
        }

        private void RoleManger_RoleChange(object sender, Role e)
        {
            tabPage2.Controls[0].Enabled = false;
            tabPage4.Controls[0].Enabled = false;
            if (e != Role.Admin)
                return;
            tabPage2.Controls[0].Enabled = true;
            tabPage4.Controls[0].Enabled = true;
        }

        private void TextBox_scancode_Click(object sender, EventArgs e)
        {
            textBox_scancode.SelectAll();
        }

        private void TextBox_scancode_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Tab || e.KeyCode == Keys.Enter)
            {
                textBox_scancode.Text = textBox_scancode.Text.Trim();
                textBox_scancode.SelectAll();
            }
        }

        private void TabPage5_MouseDown(object sender, MouseEventArgs e)
        {
            textBox_scancode.Focus();
        }

        private void TabPage5_GotFocus(object sender, EventArgs e)
        {
            textBox_scancode.Focus();
        }

        private void TextBox_scancode_Leave(object sender, EventArgs e)
        {
            textBox_scancode.Focus();
        }

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

        private void XrayControl_countstate(object sender, string e)
        {
            LogUtil.info($"手动点料: {e}");
            this.Invoke((EventHandler<string>)delegate
            {
                label_countstate.Text = "当前: "+e;
            },this,e);
            
        }

        private void T1_Tick(object sender, EventArgs e)
        {
            if (this.Visible)
            {
                ReadADIOList();
            }
            if (((Control)tabPage5).Visible) {

                if (!ManualCountRunning && RobotManage.xrayMachine.IOValue(IO_XRay_Type.Start_BTN).Equals(IO_VALUE.HIGH))
                {
                    btn_ManualCount_Click(sender, e);
                }
            }
        }
        private void LoadADIOList()
        {
            //ListViewItem lvi = new ListViewItem(new string[] { "", msg.datetime.ToString(), msg.msgtxt });
            //listView_adio.Columns.Add("名称",200);
            listView_adio.Columns.Add("Define", 150);
            listView_adio.Columns.Add("Value");
            listView_adio.Columns.Add("Base");
            listView_adio.Columns.Add("Result");
            
            t1.Start();
        }
        private void ReadADIOList()
        {
            this.SuspendLayout();
            listView_adio.Items.Clear();
            
            foreach (ConfigIO ioValue in Config.ADIOList.Values)
            {
                var pi = Config.GetType().GetProperty(ioValue.ProName + "_base");
                var basevalue = (int)pi.GetValue(Config);
                var v = IOManager.GetADIOValue("HC", 0, ushort.Parse(ioValue.ProValue));
                var Tray_ADIO_Value = ((Config_XRay)Config).Tray_ADIO_Value;
                var c = ((int)v- basevalue) / Tray_ADIO_Value;
                ListViewItem lvi = new ListViewItem(new string[] { /*ioValue.Explain,*/ioValue.ProName, v.ToString(), basevalue.ToString(), c.ToString() });
                listView_adio.Items.Add(lvi);
            }
            this.ResumeLayout(true);
        }

        private void btn_openXray_Click(object sender, EventArgs e)
        {
            if (RobotManage.xrayMachine.IOValue(IO_XRay_Type.Entry_Close).Equals(IO_VALUE.HIGH) && RobotManage.xrayMachine.IOValue(IO_XRay_Type.Exit_Close).Equals(IO_VALUE.HIGH))
            {
                RobotManage.xrayMachine.IOMove(IO_XRay_Type.Xray_Lock, IO_VALUE.HIGH);
                RobotManage.XRay.Start();
            }
            else {
                MessageBox.Show("X光机防护门未关闭,不可开启X光");
            }
        }

        private void btn_getXrayimage_Click(object sender, EventArgs e)
        {
            RobotManage.xrayMachine.GrabImage();
            pictureBox1.Image = new Bitmap(RobotManage.xrayMachine.xrayImagePath);
        }

        private void btn_closeXray_Click(object sender, EventArgs e)
        {
            RobotManage.XRay.Stop();
            RobotManage.xrayMachine.IOMove(IO_XRay_Type.Xray_Lock, IO_VALUE.LOW);
            
        }

        private void btn_Reset_Click(object sender, EventArgs e)
        {
            if (RobotManage.xrayMachine.runStatus != RunStatus.HomeReset)
            {
                RobotManage.xrayMachine.BeginHomeReset();
            }
        }

        event EventHandler<string> countstate;
        private void btn_ManualCount_Click(object sender, EventArgs e)
        {
            if (RobotManage.isRunning) {
                countstate?.Invoke(this, "机器正在运行中,不能使用手动点料");
            }
            else if (CodeManager.HasRightCode(textBox_scancode.Text))
            {
                RobotManage.xrayMachine.MoveInfo.ReelParam = new ReelParam();
                RobotManage.xrayMachine.MoveInfo.ReelParam.WareCode = textBox_scancode.Text;
                countstate?.Invoke(this, $"开始点料: {textBox_scancode.Text}");
                Thread td = new Thread(new ThreadStart(Count));
                td.Start();
            }
            else {
                countstate?.Invoke(this, "扫描的二维码不符合规则,请重新扫描");
            }
        }
        /// <summary>
        /// 手动点料运行中
        /// </summary>
        bool ManualCountRunning = false;
        string LastCountWareCode = "";
        void Count()
        {
            if (ManualCountRunning)
                return;

            ManualCountRunning = true;
            countstate?.Invoke(this, "关闭防护门");
            RobotManage.xrayMachine.CylinderMove(null, IO_XRay_Type.Entry_Close, IO_XRay_Type.Entry_Open, IO_VALUE.LOW);
            RobotManage.xrayMachine.CylinderMove(null, IO_XRay_Type.Exit_Close, IO_XRay_Type.Exit_Open, IO_VALUE.LOW);
            int wait = 0;
            while (true) {
                if (RobotManage.xrayMachine.IOValue(IO_XRay_Type.Entry_Close).Equals(IO_VALUE.HIGH)
                    && RobotManage.xrayMachine.IOValue(IO_XRay_Type.Exit_Close).Equals(IO_VALUE.HIGH)) {
                    break;
                }
                wait++;
                Thread.Sleep(500);
                countstate?.Invoke(this, "等待关闭防护门");
                if (wait > 10) {
                    countstate?.Invoke(this, "等待关闭防护门超时");
                    ManualCountRunning = false;
                    return;
                }
            }
            try
            {
                LastCountWareCode = RobotManage.xrayMachine.MoveInfo.ReelParam.WareCode;
                countstate?.Invoke(this, "打开X光");
                RobotManage.xrayMachine.IOMove(IO_XRay_Type.Xray_Lock, IO_VALUE.HIGH);
                Thread.Sleep(1000);
                RobotManage.XRay.Start();
                Thread.Sleep(1000);
                countstate?.Invoke(this, "获取图像");
                if (RobotManage.xrayMachine.GrabImage())
                {                    
                    Task.Run(() =>
                    {
                        countstate?.Invoke(this, "开始点料");                        
                        Process p = new Process();
                        p.StartInfo = new ProcessStartInfo("AlgoMatch\\AlgoMatch.exe");
                        p.StartInfo.Arguments =$"singlecount \"{RobotManage.xrayMachine.xrayImagePath}\"";
                        p.StartInfo.WorkingDirectory = Application.StartupPath;
                        p.StartInfo.UseShellExecute = false;
                        p.StartInfo.RedirectStandardOutput = true;
                        p.OutputDataReceived += P_OutputDataReceived;
                        p.Start();
                        p.BeginOutputReadLine();
                        p.WaitForExit();
                        Pn_Algo_Match.LoadFile();
                        p.Dispose();
                        //countstate?.Invoke(this, "完成点料");
                    });
                }
                else
                {
                    countstate?.Invoke(this, "获取图像失败!");
                }
            }
            catch (Exception e){
                countstate?.Invoke(this, "出错:"+e.ToString());
            }
            finally
            {
                RobotManage.XRay.Stop();
                Thread.Sleep(300);
                RobotManage.xrayMachine.IOMove(IO_XRay_Type.Xray_Lock, IO_VALUE.LOW);
                countstate?.Invoke(this, "关闭X光");
                Thread.Sleep(100);
                
            }
            Thread.Sleep(100);
            RobotManage.xrayMachine.CylinderMove(null, IO_XRay_Type.Entry_Close, IO_XRay_Type.Entry_Open, IO_VALUE.HIGH);
            RobotManage.xrayMachine.CylinderMove(null, IO_XRay_Type.Exit_Close, IO_XRay_Type.Exit_Open, IO_VALUE.HIGH);
            countstate?.Invoke(this, "防护门已打开");
            ManualCountRunning = false;
        }

        private void P_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            if (e.Data!=null && e.Data.StartsWith("CountResult")) 
            {
                LogUtil.info($"收到手动点料确认结果:{e.Data}");
                int count = int.Parse(e.Data.Substring(12));
                var cl = ServerConn.inputCounterDataByXRayMachine(LastCountWareCode, count);
                if (cl != null)
                {
                    countstate?.Invoke(this, cl.message);
                    var reelLocation = ServerConn.DetermineReelStorageLocation(LastCountWareCode);
                    if (reelLocation == null)
                    {
                        countstate?.Invoke(this, "打印标签失败");
                    }
                    else {
                        countstate?.Invoke(this, reelLocation.message);
                    }
                }
                else {
                    countstate?.Invoke(this, "上传点料结果失败");
                }
                
            }            
        }

        private void textBox_scancode_TextChanged(object sender, EventArgs e)
        {

        }
    }
}