Ejemplo n.º 1
0
 /// <summary>
 /// 读取整形
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void button2_Click(object sender, EventArgs e)
 {
     if (mxComponentCla != null)
     {
         try
         {
             StringBuilder sb       = new StringBuilder();
             string[]      arry     = textBox2.Text.Trim().Split(new char[] { ',' });
             int           len      = arry.Length;
             short[]       arrayval = new short[] {};
             for (int i = 0; i < len; i++)
             {
                 arrayval[i] = 0;
             }
             int x = mxComponentCla.Read(arry, arrayval);
             if (x == 0)
             {
                 sb.Append("----------批量读取成功----------");
                 for (int i = 0; i < arry.Length; i++)
                 {
                     sb.AppendFormat("寄存器【{0}】成功,值【{1}】", arry[i].ToString(), arrayval[i].ToString());
                 }
             }
             else
             {
                 SetTextBoxInfo("批量读取失败");
             }
         }
         catch (Exception ex)
         {
             SetTextBoxInfo(string.Format("读取错误:{0}", ex.Message));
             WriteLog.WriteTextLog(ex, string.Format("读取寄存器【{0}】失败", textBox1.Text.Trim()), "");
         }
     }
 }
Ejemplo n.º 2
0
        private void KeepAlivePLC()
        {
            while (true)
            {
                int x = -1; short arrayval = 0;
                taskint = Task.Factory.StartNew <int>(new Func <int>(() => { return(mxComponentCla.Read("M8000", out arrayval)); }));
                bool bol = taskint.Wait(new TimeSpan(0, 0, 0, 3));
                if (!bol)
                {
                    connectionStatus = false;
                    this.Invoke(new Action(() => { label3.Text = "未连接"; label3.ForeColor = System.Drawing.Color.Red; }));
                    if (startStopProduct)
                    {
                        startStopProduct = false;
                        this.Invoke(new Action(() => { btn_conn.Enabled = true; }));
                        MessageBox.Show("PLC请求超时,请检查PLC连接状态,重启客户端", " 武汉镭立科技友情提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    x = taskint.Result;
                    if (x != 0)
                    {
                        connectionStatus = false;
                        this.Invoke(new Action(() => { label3.Text = "未连接"; label3.ForeColor = System.Drawing.Color.Red; }));
                        if (startStopProduct)
                        {
                            startStopProduct = false;
                            this.Invoke(new Action(() => { btn_conn.Enabled = true; }));
                            MessageBox.Show("PLC连接已断开,请检查PLC连接状态,重启客户端", " 武汉镭立科技友情提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
                if (!connectionStatus)
                {
                    if (mxComponentCla != null)
                    {
                        mxComponentCla.MxClose();
                        mxComponentCla = null;
                    }
                    mxComponentCla = new MxComponentCla(axActUtlType1, "9");
                    int m = mxComponentCla.MxOpen();
                    if (m == 0 && mxComponentCla.PlcSatte() == 1)
                    {
                        connectionStatus = true;
                        this.Invoke(new Action(() => { label3.Text = "已连接"; label3.ForeColor = System.Drawing.Color.Green; }));
                    }
                    else
                    {
                        connectionStatus = false;
                        this.Invoke(new Action(() => { label3.Text = "未连接"; label3.ForeColor = System.Drawing.Color.Red; }));
                    }
                }

                Thread.Sleep(2000);
            }
        }