首页

C#与西门子1200通讯实例

c#

2020-9-6

using System;
using System.Windows.Forms;
using S7.Net;
using System.Configuration;
namespace s71200
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            comDT.Items.Add("Bool");
            comDT.Items.Add("int");
            comDT.Items.Add("Real");
            comDT.Text = "Real";
            comDT.Height = 25;

        }
        Plc S71200;
        private void btnLink_Click(object sender, EventArgs e)
        {

            S71200 = new Plc(CpuType.S71200, txtIP.Text, Convert.ToInt16(txtRack.Text), Convert.ToInt16(txtSlot.Text));
            if (S71200.Open() == 0)
            {
                //MessageBox.Show("IP为:"   txtAddress.Text   " PLC连接成功!");
                toolStripStatusLabel2.Text = "连接成功!";

            }
            else
            {
                //MessageBox.Show("IP为:"   txtAddress.Text   " PLC连接失败!");
                toolStripStatusLabel2.Text = "连接失败!";
            }
            btnLink.Enabled = false;
            bntBreak.Enabled = true;
        }

        private void bntBreak_Click(object sender, EventArgs e)
        {
            S71200.Close();
            //MessageBox.Show("IP为:"   txtAddress.Text   " PLC断开成功!");
            toolStripStatusLabel2.Text = "断开成功!";
            bntBreak.Enabled = false;
            btnLink.Enabled = true;
        }

        private void btnRead_Click(object sender, EventArgs e)
        {
            try
            {
                //浮点
                if (comDT.Text == "Real")
                {
                    double MyPlcData = ((uint)S71200.Read(txtAddress.Text)).ConvertToDouble();
                    //MessageBox.Show(MyPlcData.ToString());
                    txtValue.Text = MyPlcData.ToString();
                }
                //整数            
                else if (comDT.Text == "Int")
                {
                    short MyPlcData = ((ushort)S71200.Read(txtAddress.Text)).ConvertToShort();
                    //MessageBox.Show(MyPlcData.ToString());
                    txtValue.Text = MyPlcData.ToString();
                }
                else if (comDT.Text == "Bool")
                {
                    byte MyPlcData = ((byte)S71200.Read(txtAddress.Text));
                    int n = Convert.ToInt32(txtByte.Text);

                    //MessageBox.Show(MyPlcData.SelectBit(n).ToString());

                    txtValue.Text = MyPlcData.SelectBit(0).ToString();
                }
            }
            catch (Exception e1)
            {
                MessageBox.Show("数据类型或者地址格式错误!--详细信息如下:"   e1);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int count = ConfigurationSettings.AppSettings.Count;
            String key;
            String value;
            byte bt;
            int bit;
            String dev="";
            String rtdata="";
            System.DateTime.Now.ToString();
            for (int i=0; i < count; i  ) {
                if (ConfigurationSettings.AppSettings.Get(i).Contains("MD") || ConfigurationSettings.AppSettings.Get(i).Contains("DBD"))
                {
                    key = ConfigurationSettings.AppSettings.GetKey(i);
                    value = ((uint)S71200.Read(ConfigurationSettings.AppSettings.Get(i))).ConvertToDouble().ToString();

                    rtdata  = key   ": "   value   "\r\n";

                    //MessageBox.Show(key   ": "   value);
                }
                if (ConfigurationSettings.AppSettings.Get(i).Contains("MB")||ConfigurationSettings.AppSettings.Get(i).Contains("DBB"))
                {
                    key = ConfigurationSettings.AppSettings.GetKey(i);
                    value = ConfigurationSettings.AppSettings.Get(i);
                    
                    bt = ((byte)S71200.Read(value.Substring(0, value.Length - 2)));
                    bit = Convert.ToInt32(value.Substring(value.Length-1));
                    value = bt.SelectBit(bit).ToString();
                    rtdata  = key   ": "   value   "\r\n";
                    //MessageBox.Show(key   ": "   value "/br");
                }


                
            }
            rtdata = System.DateTime.Now.ToString()   "\r\n"   rtdata;
            MessageBox.Show(rtdata);

        }


    }
}

资源下载此资源下载价格为3D币(VIP免费),请先
资源文件列表
s71200/s71200/bin/Debug/S7.Net.dll , 38400
s71200/s71200/bin/Debug/s71200.exe , 15360
s71200/s71200/bin/Debug/s71200.pdb , 24064
s71200/s71200/bin/Debug/s71200.vshost.exe , 11600
s71200/s71200/bin/Debug/s71200.vshost.exe.manifest , 490
s71200/s71200/Form1.cs , 4577
s71200/s71200/Form1.Designer.cs , 15799
s71200/s71200/Form1.resx , 6017
s71200/s71200/obj/x86/Debug/DesignTimeResolveAssemblyReferences.cache , 5420
s71200/s71200/obj/x86/Debug/DesignTimeResolveAssemblyReferencesInput.cache , 6350
s71200/s71200/obj/x86/Debug/s71200.csproj.FileListAbsolute.txt , 460
s71200/s71200/obj/x86/Debug/s71200.csproj.GenerateResource.Cache , 975
s71200/s71200/obj/x86/Debug/s71200.exe , 15360
s71200/s71200/obj/x86/Debug/s71200.Form1.resources , 180
s71200/s71200/obj/x86/Debug/s71200.pdb , 24064
s71200/s71200/obj/x86/Debug/s71200.Properties.Resources.resources , 180
s71200/s71200/Program.cs , 487
s71200/s71200/Properties/AssemblyInfo.cs , 1344
s71200/s71200/Properties/Resources.Designer.cs , 2864
s71200/s71200/Properties/Resources.resx , 5612
s71200/s71200/Properties/Settings.Designer.cs , 1093
s71200/s71200/Properties/Settings.settings , 249
s71200/s71200/s71200.csproj , 3996
s71200/s71200.sln , 860
s71200/s71200.suo , 20480
s71200/s71200/App.config , 309
s71200/s71200/bin/Debug/s71200.exe.config , 309
s71200/s71200/bin/Debug/s71200.vshost.exe.config , 309
s71200/s71200/bin/Debug.rar , 33686
没有账号? 忘记密码?

社交账号快速登录