MasterServer.Lib.Plc.S7Net
1.0.9
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package MasterServer.Lib.Plc.S7Net --version 1.0.9
NuGet\Install-Package MasterServer.Lib.Plc.S7Net -Version 1.0.9
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MasterServer.Lib.Plc.S7Net" Version="1.0.9" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MasterServer.Lib.Plc.S7Net" Version="1.0.9" />
<PackageReference Include="MasterServer.Lib.Plc.S7Net" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MasterServer.Lib.Plc.S7Net --version 1.0.9
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: MasterServer.Lib.Plc.S7Net, 1.0.9"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package MasterServer.Lib.Plc.S7Net@1.0.9
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MasterServer.Lib.Plc.S7Net&version=1.0.9
#tool nuget:?package=MasterServer.Lib.Plc.S7Net&version=1.0.9
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Automation.Lib.Plc.S7Net
西门子PLC通信库,基于S7.Net实现与Siemens S7系列PLC的通信功能。 作者邮箱:dengjianhua1999@qq.com
特性
- 支持Siemens S7-200/300/400/1200/1500系列PLC
- 基于S7.Net库的稳定实现
- 支持多种数据块操作(DB, M, I, Q, T, C等)
- 支持单个和批量读写操作
- 自动连接管理和重连
- 集成日志记录
- 支持多种数据类型和数据块
支持的PLC型号
- S7-200
- S7-300
- S7-400
- S7-1200
- S7-1500
支持的数据类型
- 布尔型 (bool): 位操作
- 字节型 (byte/sbyte)
- 短整型 (short/ushort)
- 整型 (int/uint)
- 长整型 (long/ulong)
- 单精度浮点 (float)
- 双精度浮点 (double)
- 字符串 (string)
- 字节数组 (byte[])
- 日期时间 (DateTime)
核心组件
1. S7ConnectInfo
西门子PLC连接信息类,包含:
- Address: PLC的IP地址
- Port: 端口号(默认102)
- CpuType: PLC型号(S71200, S71500等)
- Rack: 机架号(默认0)
- Slot: 插槽号(默认1)
- TimeOut: 超时时间
- ReConnectSeconds: 重连间隔
2. S7Item<T>
单个数据项操作类,支持:
- 读取指定地址的数据
- 写入指定地址的数据
- 支持DB块、M区、I区、Q区等地址格式
3. S7Array<T>
数组数据操作类,支持:
- 批量读取连续地址的数据
- 批量写入连续地址的数据
- 数据变化检测
使用方式
1. 定义PLC配置(案例)
与MasterServer.Lib.Plc使用方式完全相同。
using Automation.Lib.Plc;
public class PlcConfig : BasePlcConfig
{
/// <summary>
/// 心跳
/// </summary>
[XmlElement]
public DataItem<short> Heartbeat { get; set; }
//**************************下面是单个数**********************************//
[XmlElement]
public DataItem<short> TestShort { get; set; }
[XmlElement]
public DataItem<float> TestFloat { get; set; }
[XmlElement]
public DataItem<bool> TestBool { get; set; }
//********************************下面是数组******************************//
[XmlElement]
public ArrayNode<short> TestShortArray { get; set; }
[XmlElement]
public ArrayNode<float> TestFloatArray { get; set; }
[XmlElement]
public ArrayNode<float> TestFloatArray2 { get; set; }
[XmlElement]
public ArrayNode<bool> TestBoolArray { get; set; }
[XmlElement]
public DataItem<string> TestString { get; set; }
2.连接和操作
与MasterServer.Lib.Plc使用方式略有不同。Installer.Install("S7Net");参数修改为S7Net,其他完全相同。
string ConfigDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config");
Installer.Install("S7Net");
XmlSerializer ser = new XmlSerializer(typeof(PlcConfig), Installer.ExtraXmlTypes.ToArray());
using (var reader = new StreamReader(Path.Combine(ConfigDir, "PlcConfig.xml")))
{
string xml = reader.ReadToEnd();
PlcConfig = Serializer.FromXml<PlcConfig>(xml, ser);
}
PlcConfig.InitializeArrayNodes();
// 读取数据
short value = PlcConfig.TestShort.Read();
// 写入数据
PlcConfig.TestShort.Write(123);
// 订阅数据变化
plcConfig.TestShortArray.StartPolling(TimeSpan.FromMilliseconds(200));//开始轮询
plcConfig.TestShortArray.DataChanged.Subscribe(newValue =>
{
Console.WriteLine($"数据变化: {newValue}");
});
3. 数组操作
与MasterServer.Lib.Plc使用方式完全相同。
// 读取数组
float[] values = PlcConfig.MyArray.Read();
// 写入数组(参数也可以用List<T>)
plcConfig.MyArray.Write(new float[] { 1.0f, 2.0f, 3.0f });
4. xml样例
与MasterServer.Lib.Plc使用方式略有不同。
- 把所有的
xsi:type按下面的方式进行修改 - Connection 下面的方式进行修改
<?xml version="1.0" encoding="utf-8"?>
<PlcConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Connection xsi:type="S7ConnectInfo">
<Id>plc1</Id>
<Address>127.0.0.1</Address>
<CpuType>S71200</CpuType>
<TimeOut>5000</TimeOut>
<ReConnectSeconds>3</ReConnectSeconds>
</Connection>
<Heartbeat xsi:type="S7ItemOfInt16" StartAddress="40" />
<TestShort xsi:type="S7ItemOfInt16" StartAddress="1" />
<TestFloat xsi:type="S7ItemOfSingle" StartAddress="2" />
<TestBool xsi:type="S7ItemOfBoolean" StartAddress="4" BitAdr="3" />
<TestShortArray xsi:type="S7ArrayOfInt16" StartAddress="5">
<DataItem xsi:type="S7ItemOfInt16" Name="x" Index="0" />
<DataItem xsi:type="S7ItemOfInt16" Name="y" Index="1" />
<DataItem xsi:type="S7ItemOfInt16" Name="z" Index="2" />
</TestShortArray>
<TestFloatArray xsi:type="S7ArrayOfSingle" StartAddress="8">
<DataItem xsi:type="S7ItemOfSingle" Name="x" Index="0" />
<DataItem xsi:type="S7ItemOfSingle" Name="y" Index="1" />
<DataItem xsi:type="S7ItemOfSingle" Name="z" Index="2" />
</TestFloatArray>
<TestBoolArray xsi:type="S7ArrayOfBoolean" StartAddress="16">
<DataItem xsi:type="S7ItemOfBoolean" Name="x" Index="0" />
<DataItem xsi:type="S7ItemOfBoolean" Name="y" Index="1" />
<DataItem xsi:type="S7ItemOfBoolean" Name="z" Index="2" />
</TestBoolArray>
<TestString xsi:type="S7ItemOfString" StartAddress="1" StringLength="20" />
</PlcConfig>
安装
通过NuGet安装:
Install-Package Automation.Lib.Plc.S7Net
依赖
- S7.Net
- Automation.Lib.Plc
- Automation.Lib.Logging</content>
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- MasterServer.Lib.Plc (>= 1.0.8)
- S7netplus (>= 0.20.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
增加描述文件;增加统一的日志