润宇软件
首 页 企业简介 项目案例 软件定制 行业软件 解决方案 企业动态 服务专区 客服中心
业务介绍:西安软件公司、软件开发、软件定制、软件外包
软件 方案 文章
  润宇软件 >> 新闻资讯  >> 解决方案

西安软件开发中读写PLC数据

发布时间:2022/8/19  浏览次数:次  字体【    】
西安软件外包采用读写PLC数据
using S7.Net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 读写PLC数据
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
       
        public Plc plc = new Plc(CpuType.S71200, "127.0.0.1", 0, 1);  //定义新的PLC对象  
        private void Button1_Click(object sender, EventArgs e)
        {
            plc.Open();//连接PLC
            if (plc.IsConnected)
            {
                MessageBox.Show("连接成功!", "连接提示", MessageBoxButtons.OK);
            }
            else
            {
                MessageBox.Show("连接失败!", "连接提示", MessageBoxButtons.OK);
            }
        }
        private void Btn_disconnect_Click(object sender, EventArgs e)
        {
            plc.Close();//断开PLC
        }
        #region 读PLC数据
        private void Btn_read_Click(object sender, EventArgs e)
        {
            if (plc.IsConnected == false)
            {
                MessageBox.Show("未连接PLC!", "连接提示", MessageBoxButtons.OK);//检查PLC是否连接;               
            }
            else
            {
                try
                {
                    string[] arr = (txt_read_addr.Text.ToUpper()).Split(’.’);//将txt_read_addr文本框中的数据转为大写字母,并用“.”拆分后存放到arr数组中
                    string valuetype = arr[1].Substring(0, 3);//取数组中的第二个元素的前三位,用以确认读取的PLC数据类型
                                                              //PLC数据类型:DBX(位,bool)DBB(字节,byte)DBW(字,word)DBD(双字,dword)
                                                              //以下是按不同的数据类型,对PLC数据进行读取
                    if (valuetype == "DBX")
                    {
                        bool test1 = (bool)plc.Read(txt_read_addr.Text.ToUpper());
                        ShowMsg(txt_read_addr.Text + ":" + test1.ToString());
                    }
                    else if (valuetype == "DBW")
                    {
                        short test3 = ((ushort)plc.Read(txt_read_addr.Text.ToUpper())).ConvertToShort();
                        ShowMsg(txt_read_addr.Text + ":" + test3.ToString());
                    }
                    else if (valuetype == "DBD")
                    {
                        double test5 = ((uint)plc.Read(txt_read_addr.Text.ToUpper())).ConvertToFloat();
                        ShowMsg(txt_read_addr.Text + ":" + test5.ToString());
                    }
                    else
                    {
                        MessageBox.Show("请检查地址是否输入错误!", "输入提示", MessageBoxButtons.OK);
                    }
                }
                catch (Exception Ex)
                {
                    MessageBox.Show("请检查地址是否输入错误!", "输入提示", MessageBoxButtons.OK);
                }
            }
        }
        #endregion

        #region  将数据写入PLC
        private void Btn_write_Click(object sender, EventArgs e)
        {
            if (plc.IsConnected == false)
            {
                MessageBox.Show("未连接PLC!", "连接提示", MessageBoxButtons.OK);
            }
            else
            {
                try
                {
                    string[] arr = (txt_write_addr.Text.ToUpper()).Split(’.’);
                    string valuetype = arr[1].Substring(0, 3);
                    if (valuetype == "DBX")
                    {
                        plc.Write(txt_write_addr.Text.ToUpper(), Convert.ToBoolean(txt_value.Text));
                    }
                    else if (valuetype == "DBW")
                    {
                        var value = short.Parse(txt_value.Text);
                        plc.Write(txt_write_addr.Text.ToUpper(), value);
                    }
                    else if (valuetype == "DBD")
                    {
                        double value = double.Parse(txt_value.Text);
                        plc.Write(txt_write_addr.Text.ToUpper(), value);
                    }
                    else
                    {
                        MessageBox.Show("请检查地址是否输入错误!", "输入提示", MessageBoxButtons.OK);
                    }
                }
                catch (Exception Ex)
                {
                    MessageBox.Show("请检查输入的“地址”或“值”是否错误!", "输入提示", MessageBoxButtons.OK);
                }
            }

        }
        #endregion
        private void Btn_clear_Click(object sender, EventArgs e)
        {
            txt_result.Text = null;//清空“结果”文本框
        }
        private void ShowMsg(string v)
        {
            txt_result.AppendText(v + "\r\n");//将读取的PLC数据追加到“结果”文本框中
        }
        private void Txt_result_TextChanged(object sender, EventArgs e)
        {
        }
    }
}
  关闭本页
西部IT网合作伙伴 合作伙伴
陕西省 | 榆林 | 延安 | 铜川 | 渭南 | 商洛 | 宝鸡 | 汉中 | 安康 | 咸阳
网站首页 | 关于我们 | 售后服务 | 项目合同 | 查看留言 | 在线留言 | 客服中心
© 版权所有:西安润宇软件科技有限公司 
公司地址:西安市丝路国际创意梦工厂4号楼 联系电话:029-87878512 手机:13468700578 联系人:李先生
Copyright ® 2009-2020 RunYusoft.com Inc. All Rights Reserved 
技术支持:西安润宇软件科技有限公司  陕ICP备11000720号