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
这两个地方如果没有设置,会出现通讯不上的问题
联机测试
设置好软件参数后,按照下图,选择一个需要进行读取或者写入的数据区,点击读取,写入试试看看吧,祝大家玩的开心!!