Read and Write Class - Kết nối C# với PLC S7-1200 bằng thư viện S7.Net - Communication C# with PLC S7-1200

Làm thế nào để kết nối C# với PLC S7-1200?

Bài viết này hướng dẫn các bạn kết nối C# với PLC S7-1200 sử dụng thư viện S7.Net ở chế độ "Read and Write Class".

Các bạn có thể tham khảo thêm chế độ:


Giao diện:
Read and Write Class - Kết nối C# với PLC S7-1200 bằng thư viện S7.Net - Communication C# with PLC S7-1200


Source Code:

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 READ_AND_WRITE_CLASS
{
    public partial class Form1 : Form
    {
        Timer timer = new Timer();
        Plc plc_s7_1200;
        CpuType CPU_Type = CpuType.S71200;
        string Ip = "192.168.1.195";
        short Rack = 0;
        short Slot = 1;
        short Time_Update = 100;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            plc_s7_1200 = new Plc(CPU_Type, Ip, Rack, Slot);
            plc_s7_1200.Open();
            if (plc_s7_1200.IsConnected)
            {
                timer.Interval = Time_Update;
                timer.Start();
                timer.Tick += Timer_Tick;
            }
        }
        private void Timer_Tick(object sender, EventArgs e)
        {
            if (plc_s7_1200.IsConnected)
                progressBar1.Value = 100;
            else
                progressBar1.Value = 0;

            DB1 db1 = new DB1();
            plc_s7_1200.ReadClass(db1, 1);
            TB_Read_a.Text = db1.a.ToString();
            TB_Read_b.Text = db1.b.ToString();
            TB_Read_c.Text = db1.c.ToString();
            TB_Read_d.Text = Convert.ToSingle(db1.d).ToString();
        }
        public class DB1
        {
            public bool a { get; set; }
            public short b { get; set; }
            public int c { get; set; }
            public double d { get; set; }
        }
        private void Btn_Sent_Click(object sender, EventArgs e)
        {
            DB1 db1 = new DB1();
            db1.a = Convert.ToBoolean(TB_Write_a.Text);
            db1.b = Convert.ToInt16(TB_Write_b.Text);
            db1.c = Convert.ToInt32(TB_Write_c.Text);
            db1.d = Convert.ToSingle(TB_Write_d.Text);
            if (plc_s7_1200.IsConnected)
                plc_s7_1200.WriteClass(db1, 1);
        }
    }
}


Link tải Source Code: Download

Video hướng dẫn:


1 comment:

  1. If you are be} worried about your gambling or affected by another person's gambling, please contact GamblingTherapy or GamblersAnonymous for help. Blackjack by Brainium, captures what makes the unique casino sport so exhilarating, whereas making the expertise simple, lovely, and enjoyable to play. A low-stakes, party-style variant of our world-leading Live Blackjack, full with massively scalable Bet Behind. With music and bubbly chat and sport steering from our TV-trained duo of vendor and co-presenter/hostess, major target|the primary focus} is firmly on enjoyable for 1xbet korean the best number of players. BlackjackInfo.com was previously run by Kenneth R Smith but has now been acquired by an affiliate advertisement network.

    ReplyDelete