技术论坛

 C#与S7-1200的数据读写方法

返回主题列表
作者 主题
davelyu
侠士

经验值: 1589
发帖数: 162
精华帖: 3
楼主    2018-04-21 16:15:11
主题:C#与S7-1200的数据读写方法

S7.Net

Github上有S7.Net的源码,大家随便下

https://github.com/killnine/s7netplus
S7.Net是针对西门子系列PLC的驱动程序, 而且这个驱动只能用于支持Profinet通信的CPU,最常用的还是S7-1200和S7-1500,整个驱动是用C#开发的。
可以通过github进行下载,也可以在微软的Visual Studio中通过NuGet直接下载库文件
http://www.nuget.org/packages/s7netplus

C#编程

这次我通过图形界面的方式进行与PLC的通信,界面的样子是这样的


CPU type可以选择不同类型的CPU。
IP地址是连接PLC的计算机IP,如果是本机运行可以写127.0.0.1。
Rack和Slot针对S7-1200分别是0和1
点击Connect按钮即可实现连接,连接之后,可以通过Read或者Write对指定的数据区进行读取和写入。
代码基本就是下面的样子

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 WindowsFormsApplication1
{
   public partial class FormMain : Form
   {   //
       private Plc plc = null;
       private ErrorCode errorState = ErrorCode.NoError;
       public FormMain()
       {
           InitializeComponent();
       }

       // 关闭窗口
       private void FormMain_FormClosed(object sender, FormClosedEventArgs e)
       {
           try
           {
               if (plc != null)
               {
                   plc.Close();
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(this, ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
           }
       }
       // 窗口加载
       private void FormMain_Load(object sender, EventArgs e)
       {
           try
           {
               cboxCputype.DataSource = Enum.GetNames(typeof(CpuType));
               cboxCputype.SelectedIndex = 3; //for 1200 CPU
           }
           catch (Exception ex)
           {
               MessageBox.Show(this, ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
           }
       }
       //Connect按钮按下
       private void btnConnect_Click(object sender, EventArgs e)
       {
           try
           {
               CpuType cpuType = (CpuType)Enum.Parse(typeof(CpuType), cboxCputype.SelectedValue.ToString());
               string ipAddress = txtIPAddress.Text;
               short rack = short.Parse(txtRack.Text);
               short slot = short.Parse(txtSlot.Text);
               plc = new Plc(cpuType, ipAddress, rack, slot);
               errorState = plc.Open();
               if (errorState != ErrorCode.NoError) throw new Exception(errorState.ToString());
               btnConnect.Enabled = false;

           }
           catch (Exception ex)
           {
               MessageBox.Show(this, ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
           }
       }
       //Disconnect按钮按下
       private void btnDisconnect_Click(object sender, EventArgs e)
       {
           try
           {
               if (plc != null)
               {
                   plc.Close();
               }
               btnConnect.Enabled = true;
           }
           catch (Exception ex)
           {
               MessageBox.Show(this, ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
           }
       }
       //Read按钮按下
       private void btnRead_Click(object sender, EventArgs e)
       {
           try
           {
               if (plc != null)
               {
                   string variable = txtMAddress.Text;
                   object result = plc.Read(variable);
                   txtPV.Text = string.Format("{0}", result.ToString());
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(this, ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
           }
       }
       //Write按钮按下
       private void btnWrite_Click(object sender, EventArgs e)
       {
           try
           {
               if (plc != null)
               {
                   string variable = txtMAddress.Text;
                   object value = txtSP.Text;
                   plc.Write(variable, value);
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(this, ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
           }
       }
   }
}

PLC编程

PLC的编程相对很简单,简单到就没有程序,只是建了一个数据块用于测试读取和写入的功能。使用NetToPLCSim建立模拟器月PLC Sim之前的通信,这一步可以参看之前的文章,都是一样的。
这里需要注意数据块属性中需要取消勾选优化的数据块,CPU的保护属性中需要勾选允许访问通过PUT/GET
这两个地方如果没有设置,会出现通讯不上的问题

联机测试

设置好软件参数后,按照下图,选择一个需要进行读取或者写入的数据区,点击读取,写入试试看看吧,祝大家玩的开心!!


个人微信公众号 "iLearning爱学习",头像就是公众号二维码
威师爷
至圣

经验值: 37430
发帖数: 5126
精华帖: 47
1楼    2018-04-21 16:41:29
主题:回复:C#与S7-1200的数据读写方法

楼主厉害啊!

工业起重机防摇摆 QQ:404136820 AntiSwayControl
海上漂1
奇侠

经验值: 7405
发帖数: 1268
精华帖: 3
2楼    2018-04-21 18:23:23
主题:回复:C#与S7-1200的数据读写方法

 谢谢分享  必须点赞

新手到来 多多指教
黑猫警长W
至圣

经验值: 18392
发帖数: 2409
精华帖: 1
3楼    2018-04-22 09:47:19
主题:回复:C#与S7-1200的数据读写方法


高手,高手!感谢分享

alexma1975
奇侠

经验值: 9878
发帖数: 460
精华帖: 2
4楼    2018-04-22 10:10:21
主题:回复:C#与S7-1200的数据读写方法


谢谢分享!

杨光cn
侠圣

经验值: 2035
发帖数: 275
精华帖: 0
5楼    2018-04-22 14:06:57
主题:回复:C#与S7-1200的数据读写方法


。。。。。。。。。。


天道酬勤,天行健,君子当自强不息。
华山松柏
奇侠

经验值: 5622
发帖数: 541
精华帖: 3
6楼    2018-04-22 22:05:41
主题:回复:C#与S7-1200的数据读写方法


谢谢楼主分享 

学无止境
Mr S
侠圣

经验值: 3723
发帖数: 486
精华帖: 1
7楼    2018-04-22 22:28:25
主题:回复:C#与S7-1200的数据读写方法

高级语言,

永无止境!
学前班一员
侠客

经验值: 849
发帖数: 56
精华帖: 1
8楼    2018-04-22 23:10:50
主题:回复:C#与S7-1200的数据读写方法

好贴,这样就相当于自己开发上位机页面实现不同的需求,可以配合UI和编程人员实现个性化软件平台

老人新手上路,请不辞吝教
止境
奇侠

经验值: 8620
发帖数: 688
精华帖: 0
9楼    2018-04-23 08:51:53
主题:回复:C#与S7-1200的数据读写方法

楼主厉害,


每天更新
您收到0封站内信:
×
×
信息提示
很抱歉!您所访问的页面不存在,或网址发生了变化,请稍后再试。