譚子健
級(jí)別: 正式會(huì)員
精華主題: 0
發(fā)帖數(shù)量: 21 個(gè)
工控威望: 61 點(diǎn)
下載積分: 69 分
在線(xiàn)時(shí)間: 20(小時(shí))
注冊(cè)時(shí)間: 2024-12-10
最后登錄: 2025-01-10
查看譚子健的 主題 / 回貼
樓主  發(fā)表于: 16小時(shí)前
圖片:
簡(jiǎn)單寫(xiě)了個(gè)c#連接西門(mén)子PLC的小程序來(lái)練習(xí),源代碼放在下面,可以看看


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

namespace 西門(mén)子連接上位機(jī)
{
    public partial class Form1 : Form
    {
        public Plc Myplc;
        public string IP = "192.168.1.118";//PLC的IP地址
        public void Connect()//創(chuàng)建連接PLC方法
        {
            try
            {
                Myplc = new Plc(CpuType.S71500, IP, 0, 1);//實(shí)例化PLC對(duì)象
            }
            catch { MessageBox.Show("連接失!"); }
        }
        public Form1()
        {
            InitializeComponent();
            Connect();//調(diào)用連接PLC方法
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(Myplc==null)
            {
                MessageBox.Show("連接失!");
                return;
            }
            try
            {
                Myplc.Open();//打開(kāi)PLC連接
                button1.BackColor = Color.Lime;//改變按鈕1顏色為綠色
                button2.BackColor = Color.White;//改變按鈕2顏色為白色
                button1.Text = "已連接PLC";//改變按鈕1文字為已連接PLC
                label1.Text = "已連接到PLC";//改變label1文字為已連接到PLC
                label1.Visible = true;//顯示label1
                
            }
            catch { MessageBox.Show("連接失!"); }//捕獲異常
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (Myplc == null)
            {
                MessageBox.Show("連接失敗!");//判斷PLC是否連接成功
                return;
            }
            try
            {
                Myplc.Close();//關(guān)閉PLC連接
                button2.BackColor = Color.Lime;//改變按鈕2顏色為綠色
                button1.BackColor = Color.White;//改變按鈕1顏色為白色
                button2.Text = "已斷開(kāi)PLC";//改變按鈕2文字為已斷開(kāi)PLC
                label1.Visible = false;//隱藏label1
            }
            catch { MessageBox.Show("斷開(kāi)失!"); }//捕獲異常
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (Myplc == null)
            {
                MessageBox.Show("連接失!");//判斷PLC是否連接成功
                return;
            }
            try
            {
                if ((bool)Myplc.Read("DB1.DBX0.0") == false)//判斷讀取的BOOL變量對(duì)按鈕進(jìn)行取反操作
                {
                    Myplc.Write("DB1.DBX0.0", 1);//寫(xiě)入BOOL變量
                    button3.BackColor = Color.Lime;//改變按鈕3顏色為綠色
                }
                else if ((bool)Myplc.Read("DB1.DBX0.0") == true)
                {
                    Myplc.Write("DB1.DBX0.0", 0);//寫(xiě)入BOOL變量
                    button3.BackColor = Color.White;//改變按鈕3顏色為白色
                }
                else { MessageBox.Show("讀取或?qū)懭胧。?quot;); }//捕獲異常
            }
            catch { MessageBox.Show("讀取或?qū)懭胧。?quot;); }//捕獲異常
            
            
        }
    }
}
本帖最近評(píng)分記錄:
  • 下載積分:+5(leisure) 多發(fā)點(diǎn)Cshape上位機(jī)資料
  • B站賬號(hào) :atanboy