设为首页 加入收藏
首 页 企业简介 项目案例 软件定制 行业软件 解决方案 企业资讯 服务专区 客服中心
服务项目
案例展示 更多 >>
·生产流程监控 2020/11/26
·上位机软件定制开发 2020/9/29
·手机端公众号集成开发 2020/6/5
·业务技能培训学习软件 2020/6/5
·润宇司法行政管理综合平… 2010/12/6
·连锁店管理系统软件 2010/7/31
·医院管理系统软件 2010/7/27
·驾校查询系统软件 2010/7/15
·生产管理系统软件 2010/1/30
·人事管理系统软件 2010/1/30
联系人:李先生
电  话:029-87878512
手  机:13468700578
地  址:西安市丝路国际创意梦工厂4号楼
在线咨询:  762176615
Email:junsoft@126.com
 
当前的位置 >> 返回首页 >> 解决方案
西安软件外包读写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)
        {
        }
    }
}

网站首页 | 关于我们 | 售后服务 | 网站地图 | 查看留言 | 在线留言 | 客服中心
© 版权所有:西安润宇软件科技有限公司 
公司地址:西安市丝路国际创意梦工厂4号楼 联系电话:029-87878512 手机:13468700578 联系人:李先生
Copyright ® 2011-2023 Xbwbw.com Inc. All Rights Reserved 
技术支持:西安润宇软件科技有限公司  陕ICP备11000720号