本文已参与「新人创作礼」活动,一起开启掘金创作之路。
What is S7.Net 什么是 S7.Net S7.Net is a plc driver that works only with Siemens PLC and only with Ethernet connection. This means that your plc must have a Profinet CPU or a profinet external card (CPxxx card). S7.Net is written entirely in C#, so you can debug it easily without having to go through native dlls. S7.Net 是一个 plc 驱动程序,仅适用于 Siemens PLC 和以太网连接。 这意味着 您的 PLC 必须具有 Profinet CPU 或 Profinet 外部卡(CPxxx 卡)。 S7.Net 完全用 C#编写,因此您无需通过本机 dll 即可轻松调试它。 支持的 PLC S7.Net is compatible with S7-200, S7-300, S7-400, S7-1200, S7-1500. S7.Net 与 S7-200,S7-300,S7-400,S7-1200 和 S7-1500 兼容。 本文是基于S7的说明文档做的开发,但是暴怒知道怎么引用本地文档,所以就只能把代码全部放上来了。文档的话我上传到阿里云盘了,我把分享的资料都上传了 「S7.NET中文说明书.pdf」https://www.aliyundrive.com/s/sZLtGN9E75g 点击链接保存,或者复制本段内容,打开「阿里云盘」APP ,无需下载极速在线查看,视频原画倍速播放。 ```js
using S7.Net; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;
namespace PLCS7netTEST { public partial class PlcForm : Form {
#region 变量
//实例
private Plc myplc;
private CpuType cpuType = new CpuType();
//枚举
private enum DQFS
{
Read = 0,
ReadBytes,
ReadClass,
ReadStruct
}
//枚举
private enum XRFS
{
Write = 0,
WriteBytes,
WriteClass,
WriteStruct
}
private string OutValue = null;
/// <summary>
/// 读取结构体
/// </summary>
private struct testStruct
{
public bool BOOL1;
public bool BOOL2;
public byte BYTE1;
public byte BYTE2;
public ushort WORD1;
public ushort WORD2;
public double FLOAD1;
public double FLOAD2;
public uint MD10;
public uint MD11;
public ushort WORD30;
public ushort WORD31;
public ushort WORD32;
public ushort WORD33;
public ushort WORD34;
public ushort WORD35;
public ushort WORD36;
public ushort WORD37;
public ushort WORD38;
}
//DB3块的结构体
private struct testStructDB3
{
public bool BOOL_1;
public bool BOOL_2;
public ushort WORD_1;
public ushort WORD_2;
}
#endregion
public PlcForm()
{
InitializeComponent();
#region 一开始对下拉框赋值
//PLC的型号
XHText.DataSource = Enum.GetNames(typeof(CpuType));
XHText.SelectedIndex = 4;
//读取的方法选项
DQFFText.DataSource = Enum.GetNames(typeof(DQFS));
DQFFText.SelectedIndex = 0;
//写入的方法选项
XRFFText.DataSource = Enum.GetNames(typeof(XRFS));
XRFFText.SelectedIndex = 0;
//读取
DQFSText.SelectedIndex = 0;
//写入
XRFSText.SelectedIndex = 0;
//读取数据类型
SJLXText.SelectedIndex = 0;
//写入数据类型
XRSJLXText.SelectedIndex = 0;
#endregion
}
/// <summary>
/// 连接PLC
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
try
{
CpuType cpuType = (CpuType)Enum.Parse(typeof(CpuType), XHText.Text, true);
myplc = new Plc(cpuType, IPText.Text, short.Parse(JTHText.Text.ToString()), short.Parse(CCHText.Text.ToString()));
myplc.Open();
if (myplc.IsConnected == false)
{
MessageBox.Show("连接失败");
return;
}
else
{
MessageBox.Show("连接成功");
IPText.Enabled = false;
DKText.Enabled = false;
XHText.Enabled = false;
JTHText.Enabled = false;
CCHText.Enabled = false;
LJBut.Enabled = false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// 断开连接PLC
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DkBut_Click(object sender, EventArgs e)
{
try
{
if (myplc != null)
{
myplc.Close();
IPText.Enabled = true;
DKText.Enabled = true;
XHText.Enabled = true;
JTHText.Enabled = true;
CCHText.Enabled = true;
LJBut.Enabled = true;
MessageBox.Show("已断开连接!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#region 读取
/// <summary>
/// 读取PLC
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void QCBut_Click(object sender, EventArgs e)
{
ValueText.Clear();
}
/// <summary>
/// 读取按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DDQBut_Click(object sender, EventArgs e)
{
try
{
if (myplc == null || myplc.IsConnected == false)
{
MessageBox.Show("未连接,不能读取");
return;
}
switch (DQFFText.SelectedIndex)
{
//按Read
case 0:
switch (DQFSText.SelectedIndex)
{
///一个一个读取按照DB1.DBX0.0
case 0:
if (DZText1.Text == string.Empty)
{
MessageBox.Show("请输入:DB块地址");
return;
}
GetReadValue(true);