使用S7netplus包实现西门子s7协议
S7netplus是一个用于与Siemens S7 PLC进行通信的C#库。它提供了一种简单的方式来读取和写入PLC中的数据。
安装
可以通过NuGet包管理器来安装。
S7netplus的使用
一、连接
cs
// CpuType:PLC的CPU型号,咱用的这个设备1200 西门子1212C
// ip:PLC的ip地址
// rack:机台号的位置(plc知识)
// slot:插槽号的位置(plc知识)
// public Plc(CpuType cpu, string ip, short rack, short slot)
Plc plc = new Plc(CpuType.S71200, "192.168.102.202", 0, 1);
try {
// 打开连接
myPlc.Open();
// 是否连接成功
Console.WriteLine(myPlc.IsConnected);
if (myPlc.IsConnected)
{
MessageBox.Show("打开连接成功");
}
}
catch(Exception ex)
{
MessageBox.Show("打开失败:" + ex.Message);
}
二、断开plc
cs
if (myPlc != null && myPlc.IsConnected)
{
myPlc.Close();
}
else
{
MessageBox.Show("请先连接PLC");
}
三、数据写入和读取
cs
// public void Write(string variable, object value)
// variable:PLC具体写入变量的名称("DB1.DBX0.0", "DB20.DBD200", "MB20", "T45", etc)
// value:写入的具体的值
// 写
plc.Write("M10.1", True); // 写入布尔值
// 读
plc.Read("M10.1");
四、实例
cs
using S7.Net;
class Program
{
static void Main()
{
// 创建plc对象
// 所连接的西门子设备是s7-1200
// 参数1: cpu类型 , 参数2: ip地址 , 参数3: 机架号 , 参数4: 插槽号
var plc = new Plc(CpuType.S7300, "192.168.1.10", 0, 2); // 创建一个Plc实例
try
{
plc.Open(); // 打开连接
if (plc.IsConnected)
{
ushort data = (ushort)plc.Read("DB1.DBW10"); // 从DB1块的DBW10地址读取一个字
Console.WriteLine($"Original value: {data}");
plc.Write("DB1.DBW10", (ushort)1234); // 写入一个新的值到DB1块的DBW10地址
data = (ushort)plc.Read("DB1.DBW10"); // 再次读取确认写入
Console.WriteLine($"New value: {data}");
}
}
catch (Exception e)
{
Console.WriteLine($"An error occurred: {e.Message}");
}
finally
{
if (plc.IsConnected)
{
plc.Close(); // 关闭连接
}
}
}
}
不同类型数据的读取示例
cs
Console.WriteLine("--------------bool-------------------");
// 位读取,也就是读取布尔值
bool b = (bool)plc.Read("DB21.DBX0.0");
Console.WriteLine(b);
b = (bool)plc.Read("DB21.DBX0.1");
Console.WriteLine(b);
// 参数:区域类型,db块编号,开始读取字节位置,读取数据类型,读取个数,[位地址]
var ab = plc.Read(DataType.DataBlock, 21, 0, VarType.Bit, 4,1);
Console.WriteLine(ab);
Console.WriteLine("--------------byte-------------------");
byte b1 = (byte)plc.Read("DB21.DBB1");
Console.WriteLine(b1);
b1 = (byte)plc.Read(DataType.DataBlock, 21, 1, VarType.Byte, 1);
Console.WriteLine(b1);
Console.WriteLine("--------------word-------------------");
ushort us1 = (ushort)plc.Read("DB21.DBW2");
Console.WriteLine(us1);
us1 = (ushort)plc.Read(DataType.DataBlock, 21, 2, VarType.Word, 1);
Console.WriteLine(us1);
Console.WriteLine("--------------DWord-------------------");
uint i1 = (uint)plc.Read("DB21.DBD4");
Console.WriteLine(i1);
i1 = (uint)plc.Read(DataType.DataBlock, 21, 4, VarType.DWord, 1);
Console.WriteLine(i1);
Console.WriteLine("--------------USInt-------------------");
b1 = (byte)plc.Read("DB21.DBB1");
Console.WriteLine(b1);
b1 = (byte)plc.Read("DB21.DBB8");
Console.WriteLine(b1);
b1 = (byte)plc.Read(DataType.DataBlock, 21, 8, VarType.Byte, 1);
Console.WriteLine(b1);
Console.WriteLine("--------------SInt-------------------");
sbyte b2 = (sbyte)(byte)plc.Read("DB21.DBB9");
Console.WriteLine(b2);
b2 = (sbyte)(byte)plc.Read(DataType.DataBlock, 21, 9, VarType.Byte, 1);
Console.WriteLine(b2);
Console.WriteLine("--------------UInt-------------------");
ushort us = (ushort)plc.Read("DB21.DBW10");
Console.WriteLine(us);
us = (ushort)plc.Read(DataType.DataBlock, 21, 10, VarType.Word, 1);
Console.WriteLine(us);
Console.WriteLine("--------------Int-------------------");
short s = (short)(ushort)plc.Read("DB21.DBW12");
Console.WriteLine(s);
Console.WriteLine("--------------UDInt-------------------");
uint ui = (uint)plc.Read("DB21.DBD14");
Console.WriteLine(ui);
ui = (uint)plc.Read(DataType.DataBlock, 21, 14, VarType.DWord, 1);
Console.WriteLine(ui);
Console.WriteLine("--------------DInt-------------------");
int i = (int)(uint)plc.Read("DB21.DBD18");
Console.WriteLine(i);
i = (int)plc.Read(DataType.DataBlock, 21, 18, VarType.DInt, 1);
Console.WriteLine(i);
Console.WriteLine("--------------Real-------------------");
float f = ((uint)plc.Read("DB21.DBD22")).ConvertToFloat();
Console.WriteLine(f);
f = (float)plc.Read(DataType.DataBlock, 21, 22, VarType.Real, 1);
Console.WriteLine(f);
Console.WriteLine("--------------LReal-------------------");
double d = (double)plc.Read(DataType.DataBlock, 21, 26, VarType.LReal,1);
Console.WriteLine(d);
Console.WriteLine("--------------DateTime-------------------");
DateTime t = (DateTime)plc.Read(DataType.DataBlock, 21, 44, VarType.DateTimeLong, 1);
Console.WriteLine(t);