Example #1
0
        public bool GetInfo <T>(bool checkPingSuccess, PlcCompany plcCompany, bool isRead, string address, T value, out T output, out string msg) where T : IDataTransfer, new()
        {
            output = default(T);
            msg    = string.Empty;

            try
            {
                if (plcCompany == PlcCompany.Mitsubishi)
                {
                    if (checkPingSuccess)
                    {
                        if (!IsPingSuccess)
                        {
                            IsAlive = false;
                            msg     = "无法连接到 " + IP;
                            return(false);
                        }
                    }

                    if (isRead)//读
                    {
                        OperateResult <T> result = melsec_net.ReadFromPLC <T>(address);
                        if (result.IsSuccess)
                        {
                            output  = result.Content;
                            IsAlive = true;
                            return(true);
                        }
                        else
                        {
                            msg     = string.Format("从{0} 中读取数据出现错误,代码:{1}", address, result.ErrorCode);
                            IsAlive = false;
                            return(false);
                        }
                    }
                    else//写
                    {
                        OperateResult result = melsec_net.WriteIntoPLC <T>(address, value);
                        if (result.IsSuccess)
                        {
                            IsAlive = true;
                            return(true);
                        }
                        else
                        {
                            msg     = string.Format("{0} 中写入 {1} 出现错误,代码:{2}", address, value, result.ErrorCode);
                            IsAlive = false;
                            return(false);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }

            IsAlive = false;
            return(false);
        }
Example #2
0
        private void userButton1_Click(object sender, EventArgs e)
        {
            // M100-M104读取显示
            OperateResult <byte[]> read = melsec_net.ReadFromPLC("M100", 5);

            if (read.IsSuccess)
            {
                //成功读取,True代表通,False代表不通
                bool M100 = read.Content[0] == 1;
                bool M101 = read.Content[1] == 1;
                bool M102 = read.Content[2] == 1;
                bool M103 = read.Content[3] == 1;
                bool M104 = read.Content[4] == 1;
                // 显示
                TextBoxAppendStringLine($"M100:{M100} M101:{M101} M102:{M102} M103:{M103} M104:{M104}");
            }
            else
            {
                //失败读取,显示失败信息
                MessageBox.Show(read.ToMessageShowString());
            }
        }
Example #3
0
 private void button25_Click(object sender, EventArgs e)
 {
     try
     {
         OperateResult <byte[]> read = melsec_net.ReadFromPLC(textBox3.Text, ushort.Parse(textBox9.Text));
         if (read.IsSuccess)
         {
             textBox10.Text = "结果:" + HslCommunication.BasicFramework.SoftBasic.ByteToHexString(read.Content);
         }
         else
         {
             MessageBox.Show("读取失败:" + read.ToMessageShowString( ));
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("读取失败:" + ex.Message);
     }
 }