西门子PLC与电脑走S7通讯

458 阅读7分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

What is S7.Net 什么是 S7.Net S7.Net is a plc driver that works only with Siemens PLC and only with Ethernet connection. This means that your plc must have a Profinet CPU or a profinet external card (CPxxx card). S7.Net is written entirely in C#, so you can debug it easily without having to go through native dlls. S7.Net 是一个 plc 驱动程序,仅适用于 Siemens PLC 和以太网连接。 这意味着 您的 PLC 必须具有 Profinet CPU 或 Profinet 外部卡(CPxxx 卡)。 S7.Net 完全用 C#编写,因此您无需通过本机 dll 即可轻松调试它。 支持的 PLC S7.Net is compatible with S7-200, S7-300, S7-400, S7-1200, S7-1500. S7.Net 与 S7-200,S7-300,S7-400,S7-1200 和 S7-1500 兼容。 本文是基于S7的说明文档做的开发,但是暴怒知道怎么引用本地文档,所以就只能把代码全部放上来了。文档的话我上传到阿里云盘了,我把分享的资料都上传了 「S7.NET中文说明书.pdf」www.aliyundrive.com/s/sZLtGN9E7… 点击链接保存,或者复制本段内容,打开「阿里云盘」APP ,无需下载极速在线查看,视频原画倍速播放。


using S7.Net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PLC_S7net_TEST
{
    public partial class PlcForm : Form
    {

        #region 变量

        //实例
        private Plc myplc;

        
        private CpuType cpuType = new CpuType();

        //枚举
        private enum DQFS
        {
            Read = 0,
            ReadBytes,
            ReadClass,
            ReadStruct
        }

        //枚举
        private enum XRFS
        {
            Write = 0,
            WriteBytes,
            WriteClass,
            WriteStruct
        }

        private string OutValue = null;

        /// <summary>
        /// 读取结构体
        /// </summary>
        private struct testStruct
        {
            public bool BOOL1;
            public bool BOOL2;
            public byte BYTE1;
            public byte BYTE2;
            public ushort WORD1;
            public ushort WORD2;
            public double FLOAD1;
            public double FLOAD2;
            public uint MD10;
            public uint MD11;
            public ushort WORD30;
            public ushort WORD31;
            public ushort WORD32;
            public ushort WORD33;
            public ushort WORD34;
            public ushort WORD35;
            public ushort WORD36;
            public ushort WORD37;
            public ushort WORD38;
        }

        //DB3块的结构体
        private struct testStructDB3
        {
            public bool BOOL_1;
            public bool BOOL_2;
            public ushort WORD_1;
            public ushort WORD_2;
        }

        #endregion




        public PlcForm()
        {
            InitializeComponent();

            #region 一开始对下拉框赋值
            //PLC的型号
            XHText.DataSource = Enum.GetNames(typeof(CpuType));
            XHText.SelectedIndex = 4;

            //读取的方法选项
            DQFFText.DataSource = Enum.GetNames(typeof(DQFS));
            DQFFText.SelectedIndex = 0;


            //写入的方法选项
            XRFFText.DataSource = Enum.GetNames(typeof(XRFS));
            XRFFText.SelectedIndex = 0;

            //读取
            DQFSText.SelectedIndex = 0;

            //写入
            XRFSText.SelectedIndex = 0;

            //读取数据类型
            SJLXText.SelectedIndex = 0;

            //写入数据类型
            XRSJLXText.SelectedIndex = 0;

            #endregion


        }


        /// <summary>
        /// 连接PLC
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                CpuType cpuType = (CpuType)Enum.Parse(typeof(CpuType), XHText.Text, true);

                myplc = new Plc(cpuType, IPText.Text, short.Parse(JTHText.Text.ToString()), short.Parse(CCHText.Text.ToString()));

                myplc.Open();

                if (myplc.IsConnected == false)
                {
                    MessageBox.Show("连接失败");
                    return;
                }
                else
                {
                    MessageBox.Show("连接成功");

                    IPText.Enabled = false;
                    DKText.Enabled = false;
                    XHText.Enabled = false;
                    JTHText.Enabled = false;
                    CCHText.Enabled = false;
                    LJBut.Enabled = false;

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 断开连接PLC
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DkBut_Click(object sender, EventArgs e)
        {
            try
            {
                if (myplc != null)
                {
                    myplc.Close();
                    IPText.Enabled = true;
                    DKText.Enabled = true;
                    XHText.Enabled = true;
                    JTHText.Enabled = true;
                    CCHText.Enabled = true;
                    LJBut.Enabled = true;
                    MessageBox.Show("已断开连接!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #region 读取
        /// <summary>
        /// 读取PLC
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void QCBut_Click(object sender, EventArgs e)
        {
            ValueText.Clear();
        }

        /// <summary>
        /// 读取按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DDQBut_Click(object sender, EventArgs e)
        {
            try
            {
                if (myplc == null || myplc.IsConnected == false)
                {
                    MessageBox.Show("未连接,不能读取");
                    return;
                }

                switch (DQFFText.SelectedIndex)
                {
                    //按Read
                    case 0:

                        switch (DQFSText.SelectedIndex)
                        {
                            ///一个一个读取按照DB1.DBX0.0
                            case 0:
                                if (DZText1.Text == string.Empty)
                                {
                                    MessageBox.Show("请输入:DB块地址");
                                    return;
                                }
                                GetReadValue(true);
                                break;

                            ///一个一个按照偏移量读取
                            case 1:
                                if (DBKText1.Text == string.Empty || QPYLText1.Text == string.Empty || QPYLText1.Text == string.Empty)
                                {
                                    MessageBox.Show("请输入:DB块orDB开始偏移量");
                                    return;
                                }
                                GetReadValue(false);
                                break;
                        }
                        break;

                    //按ReadBytes
                    case 1:
                        GetReadBytes();
                        break;

                    //按ReadClass
                    case 2:
                        GetReadClass();
                        break;

                    //按ReadStruct
                    case 3:
                        GetReadStruct();
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 读取单个数据
        /// </summary>
        /// <returns></returns>
        private void GetReadValue(bool bl)
        {
            try
            {
                if (bl)
                {
                    var Value = myplc.Read(DZText1.Text.ToUpper());
                    switch (SJLXText.SelectedIndex)
                    {
                        //Bit
                        case 0:
                            OutValue = ((bool)Value).ToString();
                            break;
                        //Byte
                        case 1:
                            OutValue = ((byte)Value).ToString();
                            break;
                        //Word
                        case 2:
                            OutValue = ((ushort)Value).ConvertToShort().ToString();
                            break;
                        //DWord
                        case 3:
                            OutValue = ((uint)Value).ConvertToInt().ToString();
                            break;
                        //Int
                        case 4:
                            OutValue = ((ushort)Value).ConvertToShort().ToString();
                            break;
                        //DInt
                        case 5:
                            OutValue = ((uint)Value).ConvertToInt().ToString();
                            break;
                        //Real
                        case 6:
                            OutValue = ((uint)Value).ConvertToDouble().ToString();
                            break;
                        default:
                            MessageBox.Show("暂时未涉及数据类型。");
                            break;
                    }
                    SetValueText(DZText1.Text + ":" + OutValue);
                }
                else
                {
                    SetValueText("此方法为代码写固定,具体实现请查看代码:开始读取");
                    switch (SJLXText.SelectedIndex)
                    {
                        //Bit
                        case 0:
                            var ValueBit = myplc.Read(DataType.DataBlock, (int)DBKText1.Value, (int)QPYLText1.Value, VarType.Bit, (int)ZPYLText1.Value, 0);
                            OutValue = ((bool)ValueBit).ToString();
                            break;
                        //Byte
                        case 1:
                            var ValueByte = myplc.Read(DataType.DataBlock, (int)DBKText1.Value, (int)QPYLText1.Value, VarType.Byte, (int)ZPYLText1.Value, 0);

                            OutValue = Convert.ToSByte(ValueByte).ToString();
                            break;
                        //Word
                        case 2:
                            var ValueWord = myplc.Read(DataType.DataBlock, (int)DBKText1.Value, (int)QPYLText1.Value, VarType.Word, (int)ZPYLText1.Value, 0);

                            OutValue = ((ushort)ValueWord).ConvertToShort().ToString();
                            break;
                        //DWord
                        case 3:
                            var ValueDWord = myplc.Read(DataType.DataBlock, (int)DBKText1.Value, (int)QPYLText1.Value, VarType.DWord, (int)ZPYLText1.Value, 0);

                            OutValue = ((uint)ValueDWord).ConvertToInt().ToString();
                            break;
                        //Int
                        case 4:
                            var ValueInt = myplc.Read(DataType.DataBlock, (int)DBKText1.Value, (int)QPYLText1.Value, VarType.Int, (int)ZPYLText1.Value, 0);

                            OutValue = ((ushort)ValueInt).ConvertToShort().ToString();
                            break;
                        //DInt
                        case 5:
                            var ValueDInt = myplc.Read(DataType.DataBlock, (int)DBKText1.Value, (int)QPYLText1.Value, VarType.DInt, (int)ZPYLText1.Value, 0);

                            OutValue = ((uint)ValueDInt).ConvertToInt().ToString();
                            break;
                        //Real
                        case 6:
                            var ValueReal = myplc.Read(DataType.DataBlock, (int)DBKText1.Value, (int)QPYLText1.Value, VarType.Real, (int)ZPYLText1.Value, 0);

                            OutValue = ((uint)ValueReal).ConvertToDouble().ToString();
                            break;
                        default:
                            MessageBox.Show("暂时未涉及数据类型。");
                            break;
                    }
                    SetValueText("DB" + DBKText1.Value.ToString() + "." + QPYLText1.Value.ToString() + ".0:" + OutValue);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("转换数据类型不对:" + ex.Message);
            }

            return;
        }

        /// <summary>
        /// 读取ReadBytes
        /// </summary>
        /// <returns></returns>
        private void GetReadBytes()
        {
            try
            {
                SetValueText("此方法为代码写固定,具体实现请查看代码:开始读取");
                var bytes = myplc.ReadBytes(DataType.DataBlock, 1, 0, 42);

                OutValue = ((bool)bytes[0].SelectBit(0)).ToString();
                SetValueText("DB1.DBX0.0:" + OutValue);

                OutValue = ((bool)bytes[0].SelectBit(1)).ToString();
                SetValueText("DB1.DBX0.1:" + OutValue);

                OutValue = S7.Net.Types.Byte.FromByteArray(bytes.Skip(1).Take(1).ToArray()).ToString();
                SetValueText("DB1.DBB1.0:" + OutValue);

                OutValue = S7.Net.Types.Byte.FromByteArray(bytes.Skip(2).Take(1).ToArray()).ToString();
                SetValueText("DB1.DBB2.0:" + OutValue);

                OutValue = S7.Net.Types.Word.FromByteArray(bytes.Skip(4).Take(2).ToArray()).ToString();
                SetValueText("DB1.DBW4.0:" + OutValue);

                OutValue = S7.Net.Types.Word.FromByteArray(bytes.Skip(6).Take(2).ToArray()).ToString();
                SetValueText("DB1.DBW6.0:" + OutValue);

                OutValue = S7.Net.Types.Double.FromByteArray(bytes.Skip(8).Take(4).ToArray()).ToString();
                SetValueText("DB1.DBD8.0:" + OutValue);

                OutValue = S7.Net.Types.Double.FromByteArray(bytes.Skip(12).Take(4).ToArray()).ToString();
                SetValueText("DB1.DBD12.0:" + OutValue);

                OutValue = S7.Net.Types.DInt.FromByteArray(bytes.Skip(16).Take(4).ToArray()).ToString();
                SetValueText("DB1.DBD16.0:" + OutValue);

                OutValue = S7.Net.Types.DInt.FromByteArray(bytes.Skip(20).Take(4).ToArray()).ToString();
                SetValueText("DB1.DBD20.0:" + OutValue);

                OutValue = S7.Net.Types.Word.FromByteArray(bytes.Skip(24).Take(2).ToArray()).ToString();
                SetValueText("DB1.DBW24.0:" + OutValue);

                OutValue = S7.Net.Types.Word.FromByteArray(bytes.Skip(26).Take(2).ToArray()).ToString();
                SetValueText("DB1.DBW26.0:" + OutValue);

                OutValue = S7.Net.Types.Word.FromByteArray(bytes.Skip(28).Take(2).ToArray()).ToString();
                SetValueText("DB1.DBW28.0:" + OutValue);

                OutValue = S7.Net.Types.Word.FromByteArray(bytes.Skip(30).Take(2).ToArray()).ToString();
                SetValueText("DB1.DBW30.0:" + OutValue);

                OutValue = S7.Net.Types.Word.FromByteArray(bytes.Skip(32).Take(2).ToArray()).ToString();
                SetValueText("DB1.DBW32.0:" + OutValue);

                OutValue = S7.Net.Types.Word.FromByteArray(bytes.Skip(34).Take(2).ToArray()).ToString();
                SetValueText("DB1.DBW34.0:" + OutValue);

                OutValue = S7.Net.Types.Word.FromByteArray(bytes.Skip(36).Take(2).ToArray()).ToString();
                SetValueText("DB1.DBW36.0:" + OutValue);

                OutValue = S7.Net.Types.Word.FromByteArray(bytes.Skip(38).Take(2).ToArray()).ToString();
                SetValueText("DB1.DBW38.0:" + OutValue);

                OutValue = S7.Net.Types.Word.FromByteArray(bytes.Skip(40).Take(2).ToArray()).ToString();
                SetValueText("DB1.DBW40.0:" + OutValue);

                SetValueText("此方法为代码写固定,具体实现请查看代码:读取结束");
            }
            catch (Exception ex)
            {
                MessageBox.Show("转换数据类型不对:" + ex.Message);
            }

            return;
        }

        /// <summary>
        /// 读取Class
        /// </summary>
        /// <returns></returns>
        private void GetReadClass()
        {
            try
            {
                SetValueText("此方法为代码写固定,具体实现请查看代码:开始读取");

                //DB3
                ReadClassDB3 readClassDB3 = new ReadClassDB3();
                myplc.ReadClass(readClassDB3, 3, 0);
                Type type = readClassDB3.GetType();

                foreach (PropertyInfo info in type.GetProperties())
                {
                    SetValueText(info.Name.ToString() + ":" + info.GetValue(readClassDB3));
                }


                //ReadClass readClass = new ReadClass();
                //myplc.ReadClass(readClass, 1, 0);
                //Type type = readClass.GetType();

                //foreach (PropertyInfo info in type.GetProperties())
                //{
                //    SetValueText(info.Name.ToString() + ":" + info.GetValue(readClass));
                //}

                SetValueText("此方法为代码写固定,具体实现请查看代码:读取结束");
            }
            catch (Exception ex)
            {
                MessageBox.Show("转换数据类型不对:" + ex.Message);
            }

            return;
        }

        /// <summary>
        /// 读取Struct
        /// </summary>
        /// <returns></returns>
        private void GetReadStruct()
        {
            try
            {
                SetValueText("此方法为代码写固定,具体实现请查看代码:开始读取");

                testStruct testStruct = (testStruct)myplc.ReadStruct(typeof(testStruct), 1, 0);

                Type type = typeof(testStruct);

                FieldInfo[] fieldInfos = type.GetFields();

                foreach (FieldInfo fieldInfo in fieldInfos)
                {
                    SetValueText(fieldInfo.Name.ToString() + ":" + fieldInfo.GetValue(testStruct));
                }

                SetValueText("此方法为代码写固定,具体实现请查看代码:读取结束");
            }
            catch (Exception ex)
            {
                MessageBox.Show("转换数据类型不对:" + ex.Message);
            }

            return;
        }

        /// <summary>
        /// DQFS选择内容的处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DQFSText_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (DQFSText.SelectedIndex)
            {
                case 0:
                    DQFSText.SelectedIndex = 0;
                    DZText1.Enabled = true;
                    SJLXText.Enabled = true;
                    DBKText1.Enabled = false;
                    QPYLText1.Enabled = false;
                    break;
                case 1:
                    DZText1.Enabled = false;
                    SJLXText.Enabled = true;
                    DBKText1.Enabled = true;
                    QPYLText1.Enabled = true;
                    break;
            }

        }


        /// <summary>
        /// 读取方法触发事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DQFFText_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (DQFFText.SelectedIndex)
            {
                case 0:
                    DQFSText.Enabled = true;

                    break;
                case 1:
                    DQFSText.Enabled = false;
                    break;
                case 2:
                    DQFSText.Enabled = false;

                    break;
                case 3:
                    DQFSText.Enabled = false;

                    break;
            }
        }

        /// <summary>
        /// 更新UI
        /// </summary>
        /// <param name="Value"></param>
        private void SetValueText(string Value)
        {
            this.Invoke(new Action(() =>
            {
                ValueText.AppendText("【" + DateTime.Now.ToString("hh:mm:ss") + "】 " + Value + "\r\n");
            }));
        }

        /// <summary>
        /// DQFS启用时候的处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DQFSText_EnabledChanged(object sender, EventArgs e)
        {
            if (DQFSText.Enabled)
            {
                DQFSText.SelectedIndex = 0;
                DZText1.Enabled = true;
                SJLXText.Enabled = true;
            }
            else
            {
                DQFSText.SelectedIndex = 0;
                DZText1.Enabled = false;
                SJLXText.Enabled = false;
            }
        }

        #endregion

        #region 写入

        /// <summary>
        /// 写入单个数据
        /// </summary>
        /// <param name="bl"></param>
        private void SetWriteValue(bool bl)
        {
            try
            {
                if (bl)
                {
                    switch (XRSJLXText.SelectedIndex)
                    {

                        //Bit
                        case 0:
                            myplc.Write(XRDZText1.Text.ToString().ToUpper(), Convert.ToBoolean(WriteText.Text.ToString().ToUpper()));
                            break;
                        //Byte
                        case 1:
                            myplc.Write(XRDZText1.Text.ToString().ToUpper(), Convert.ToByte(WriteText.Text.ToString().ToUpper()));

                            break;
                        //Word
                        case 2:
                            myplc.Write(XRDZText1.Text.ToString().ToUpper(), Convert.ToUInt16(WriteText.Text.ToString().ToUpper()));

                            //OutValue = ((ushort)Value).ConvertToShort().ToString();
                            break;
                        //DWord
                        case 3:
                            myplc.Write(XRDZText1.Text.ToString().ToUpper(), Convert.ToUInt32(WriteText.Text.ToString().ToUpper()));

                            //OutValue = ((uint)Value).ConvertToInt().ToString();
                            break;
                        //Int
                        case 4:
                            myplc.Write(XRDZText1.Text.ToString().ToUpper(), Convert.ToUInt16(WriteText.Text.ToString().ToUpper()));

                            //OutValue = ((ushort)Value).ConvertToShort().ToString();
                            break;
                        //DInt
                        case 5:
                            myplc.Write(XRDZText1.Text.ToString().ToUpper(), Convert.ToUInt32(WriteText.Text.ToString().ToUpper()));

                            //OutValue = ((uint)Value).ConvertToInt().ToString();
                            break;
                        //Real
                        case 6:
                            myplc.Write(XRDZText1.Text.ToString().ToUpper(), Convert.ToUInt32(WriteText.Text.ToString().ToUpper()));

                            //OutValue = ((uint)Value).ConvertToDouble().ToString();
                            break;
                        default:
                            MessageBox.Show("暂时未涉及数据类型。");
                            break;
                    }
                    MessageBox.Show("写入成功");
                }
                else
                {
                    //SetValueText("此方法为代码写固定,具体实现请查看代码:开始写入");
                    switch (XRSJLXText.SelectedIndex)
                    {
                        //Bit
                        case 0:
                            myplc.Write(DataType.DataBlock, (int)XRDBKText1.Value, (int)XRQPYLText1.Value, Convert.ToBoolean(WriteText.Text.ToString().ToUpper()));

                            break;
                        //Byte
                        case 1:
                            myplc.Write(DataType.DataBlock, (int)XRDBKText1.Value, (int)XRQPYLText1.Value, Convert.ToByte(WriteText.Text.ToString().ToUpper()));

                            break;
                        //Word
                        case 2:
                            myplc.Write(DataType.DataBlock, (int)XRDBKText1.Value, (int)XRQPYLText1.Value, Convert.ToUInt16(WriteText.Text.ToString().ToUpper()));

                            break;
                        //DWord
                        case 3:
                            myplc.Write(DataType.DataBlock, (int)XRDBKText1.Value, (int)XRQPYLText1.Value, Convert.ToUInt32(WriteText.Text.ToString().ToUpper()));

                            break;
                        //Int
                        case 4:
                            myplc.Write(DataType.DataBlock, (int)XRDBKText1.Value, (int)XRQPYLText1.Value, Convert.ToUInt16(WriteText.Text.ToString().ToUpper()));

                            break;
                        //DInt
                        case 5:
                            myplc.Write(DataType.DataBlock, (int)XRDBKText1.Value, (int)XRQPYLText1.Value, Convert.ToUInt32(WriteText.Text.ToString().ToUpper()));

                            break;
                        //Real
                        case 6:
                            myplc.Write(DataType.DataBlock, (int)XRDBKText1.Value, (int)XRQPYLText1.Value, Convert.ToUInt32(WriteText.Text.ToString().ToUpper()));

                            break;
                        default:
                            MessageBox.Show("暂时未涉及数据类型。");
                            break;
                    }
                    MessageBox.Show("写入成功");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "或者请检查DB输入是否正确");
            }
        }

        /// <summary>
        /// 写入WriteBytes
        /// </summary>
        private void SetWriteBytes()
        {
            try
            {
                MessageBox.Show("此方法为固定写,主要为了学习方法应用。具体实现请查看代码");
                byte[] db1byts = new byte[1];//声明一个byte数组

                db1byts[0] = S7.Net.Types.Boolean.SetBit(db1byts[0], 0);//给DB1.DBX0.0设置数值

                myplc.WriteBytes(DataType.DataBlock, 1, 0, db1byts);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 写入CLASS
        /// </summary>
        private void SetWriteClass()
        {
            try
            {
                MessageBox.Show("此方法为固定写,主要为了学习方法应用。具体实现请查看代码");

                ReadClassDB3 readClassDB3 = new ReadClassDB3();
                readClassDB3.BOOL_1 = false;
                readClassDB3.BOOL_2 = false;
                readClassDB3.WORD_1 = 30;
                readClassDB3.WORD_2 = 40;
                myplc.WriteClassAsync(readClassDB3, 3);
                MessageBox.Show("写入成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void SetWriteStruct()
        {
            try
            {
                MessageBox.Show("此方法为固定写,主要为了学习方法应用。具体实现请查看代码");

                testStructDB3 testStructDB3 = new testStructDB3();
                testStructDB3.BOOL_1 = true;
                testStructDB3.WORD_1 = 100;
                testStructDB3.WORD_2 = 200;
                myplc.WriteStructAsync(testStructDB3,3,0);
                MessageBox.Show("写入成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 写入按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void XRBut_Click(object sender, EventArgs e)
        {
            try
            {
                if (myplc == null || myplc.IsConnected == false)
                {
                    MessageBox.Show("未连接,不能写入");
                    return;
                }

                switch (XRFFText.SelectedIndex)
                {
                    //按Write
                    case 0:

                        switch (XRFSText.SelectedIndex)
                        {
                            ///一个一个读取按照DB1.DBX0.0
                            case 0:
                                if (XRDZText1.Text == string.Empty)
                                {
                                    MessageBox.Show("请输入:DB块地址");
                                    return;
                                }
                                SetWriteValue(true);
                                //MessageBox.Show("写入成功");
                                break;

                            ///一个一个按照偏移量读取
                            case 1:
                                if (XRDBKText1.Text == string.Empty || XRQPYLText1.Text == string.Empty || XRZPYLText1.Text == string.Empty)
                                {
                                    MessageBox.Show("请输入:DB块orDB开始偏移量");
                                    return;
                                }
                                SetWriteValue(false);
                                //MessageBox.Show("写入成功");
                                break;
                        }
                        break;

                    //按ReadBytes
                    case 1:
                        SetWriteBytes();
                        break;

                    //按WriteClass
                    case 2:
                        SetWriteClass();
                        break;

                    //按WriteStruct
                    case 3:
                        SetWriteStruct();
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 启用时处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void XRFSText_EnabledChanged(object sender, EventArgs e)
        {
            if (XRFSText.Enabled)
            {
                XRFSText.SelectedIndex = 0;
                XRDZText1.Enabled = true;
                XRSJLXText.Enabled = true;

            }
            else
            {
                XRFSText.SelectedIndex = 0;
                XRDZText1.Enabled = false;
                XRSJLXText.Enabled = false;
            }
        }

        /// <summary>
        /// 选择时处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void XRFFText_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (XRFFText.SelectedIndex)
            {
                case 0:
                    XRFSText.Enabled = true;
                    WriteText.Enabled= true;

                    break;
                case 1:
                    XRFSText.Enabled = false;
                    WriteText.Enabled = false;

                    break;
                case 2:
                    XRFSText.Enabled = false;
                    WriteText.Enabled = false;


                    break;
                case 3:
                    XRFSText.Enabled = false;
                    WriteText.Enabled = false;


                    break;
            }
        }

        /// <summary>
        /// 选择时处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void XRFSText_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (XRFSText.SelectedIndex)
            {
                case 0:
                    XRFSText.SelectedIndex = 0;
                    XRDZText1.Enabled = true;
                    XRSJLXText.Enabled = true;
                    XRDBKText1.Enabled = false;
                    XRQPYLText1.Enabled = false;
                    break;
                case 1:
                    XRDZText1.Enabled = false;
                    XRSJLXText.Enabled = true;
                    XRDBKText1.Enabled = true;
                    XRQPYLText1.Enabled = true;
                    break;
            }
        }
        #endregion
    }

    /// <summary>
    /// 读取类
    /// </summary>
    public class ReadClass
    {
        public bool BOOL1 { get; set; }
        public bool BOOL2 { get; set; }
        public byte BYTE1 { get; set; }
        public byte BYTE2 { get; set; }
        public ushort WORD1 { get; set; }
        public ushort WORD2 { get; set; }
        public double FLOAD1 { get; set; }
        public double FLOAD2 { get; set; }
        public uint MD10 { get; set; }
        public uint MD11 { get; set; }
        public ushort WORD30 { get; set; }
        public ushort WORD31 { get; set; }
        public ushort WORD32 { get; set; }
        public ushort WORD33 { get; set; }
        public ushort WORD34 { get; set; }
        public ushort WORD35 { get; set; }
        public ushort WORD36 { get; set; }
        public ushort WORD37 { get; set; }
        public ushort WORD38 { get; set; }
    }

    /// <summary>
    /// DB2读取类
    /// </summary>
    public class ReadClassDB3
    {
        public bool BOOL_1 { get; set; }
        public bool BOOL_2 { get; set; }

        public ushort WORD_1 { get; set; }
        public ushort WORD_2 { get; set; }
       
    }

}