【C#】上位機與西門子200SMART通訊測試


using System.Windows.Forms;
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 S7.Net;


namespace HZCsharpProject01
{
    public partial class FormCommucation : Form
    {
        public FormCommucation()
        {
            InitializeComponent();
        }
        Plc plc;//聲明plc連接對象
 
        private void btnConnect_Click(object sender, EventArgs e)
        {
            string TypeName = txtPLCType.Text;//PLC型號名稱
            string Ip = txtIp.Text;//plcIp地址
            switch (TypeName)
            {
                case "200":
                case "1200":
                    /*S7.Net.dll類庫中沒有S7-200Smart類型,想跟S7-200Smart通訊選擇S71200即可*/ 最新S7.NET支持smart200了
                    plc = new Plc(CpuType.S71200, Ip, 0, 1);//創建plc實例
                    break;
                case "300":
                    plc = new Plc(CpuType.S7300, Ip, 0, 1);//創建plc實例
                    break;
                case "400":
                    plc = new Plc(CpuType.S7400, Ip, 0, 1);//創建plc實例
                    break;
                case "1500":
                    plc = new Plc(CpuType.S71500, Ip, 0, 1);//創建plc實例
                    break;
            }

            plc.Open();//連接plc
            if (plc.IsConnected)//檢查plc是否連接上
            {
                MessageBox.Show("與PLC連接成功!");
                //連接成功
            }
            else
            {
                MessageBox.Show("與PLC連接失敗!");
                //連接失敗
            }
               
        }


        private void btnWrite_Click(object sender, EventArgs e)
        {
            //string address = txtAddress.Text;//寄存地址
            //string writedata = txtWriteData.Text;//寫入數據
            //plc.Write(address, writedata);

            if (plc.IsConnected == false)
            {
                MessageBox.Show("未連接PLC!", "連接提示", MessageBoxButtons.OK);
            }
            else
            {
                try
                {
                    string[] arr = (txtAddress .Text.ToUpper()).Split('.');
                    string valuetype = arr[1].Substring(0, 3);

                    if (valuetype == "DBX")
                    {
                        plc.Write(txtAddress .Text.ToUpper(), Convert.ToBoolean(txtWriteData .Text));
                    }

                    else if (valuetype == "DBW")
                    {
                        var value = short.Parse(txtWriteData .Text);
                        plc.Write(txtAddress .Text.ToUpper(), value);
                    }

                    else if (valuetype == "DBD")
                    {
                        float  value = float .Parse(txtWriteData.Text);
                        //double value = double.Parse(txtWriteData.Text);
                        plc.Write(txtAddress.Text.ToUpper(), value);

                    }

                    else
                    {
                        MessageBox.Show("請檢查地址是否輸入錯誤!", "輸入提示", MessageBoxButtons.OK);
                    }

                }
                catch (Exception Ex)
                {
                    MessageBox.Show("請檢查輸入的“地址”或“值”是否錯誤!"+Ex, "輸入提示", MessageBoxButtons.OK);
                }


            }

        }

        private void btnReadSingleData_Click(object sender, EventArgs e)
        {
            //string address = txtAddress2.Text;//寄存地址
            //var bytes = plc.Read(address);
            //txtReadSingleData.Text = bytes.ToString();

            if (plc.IsConnected == false)
            {
                MessageBox.Show("未連接PLC!", "連接提示", MessageBoxButtons.OK);//檢查PLC是否連接;                
            }
            else
            {
                try
                {
                    string[] arr = (txtAddress2 .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(txtAddress2 .Text.ToUpper());
                        txtReadSingleData.Text=txtAddress2.Text + ":" + test1.ToString();
                    }

                    else if (valuetype == "DBW")
                    {
                        short test3 = ((ushort)plc.Read(txtAddress2 .Text.ToUpper())).ConvertToShort();
                        txtReadSingleData.Text = txtAddress2 .Text + ":" + test3.ToString();
                    }

                    else if (valuetype == "DBD")
                    {
                        double test5 = ((uint)plc.Read(txtAddress2 .Text.ToUpper())).ConvertToFloat();
                        txtReadSingleData.Text = txtAddress2 .Text + ":" + test5.ToString();
                    }

                    else
                    {
                        MessageBox.Show("請檢查地址是否輸入錯誤!", "輸入提示", MessageBoxButtons.OK);
                    }
                }
                catch (Exception Ex)
                {
                    MessageBox.Show("請檢查地址是否輸入錯誤!"+Ex , "輸入提示", MessageBoxButtons.OK);
                }
            }

        }


        private void btnDisConnect_Click(object sender, EventArgs e)
        {
            plc.Close();//斷開PLC
        }
    }





}

我用的是VS2015版本,框架是4.6。NUGET下載S7.DLL時要下載以前的版本,最新版本框架都是4.7以上了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM