MasterServer.Lib.Plc.Opc 2.0.0

dotnet add package MasterServer.Lib.Plc.Opc --version 2.0.0
                    
NuGet\Install-Package MasterServer.Lib.Plc.Opc -Version 2.0.0
                    
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.Opc" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MasterServer.Lib.Plc.Opc" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="MasterServer.Lib.Plc.Opc" />
                    
Project file
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.Opc --version 2.0.0
                    
#r "nuget: MasterServer.Lib.Plc.Opc, 2.0.0"
                    
#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.Opc@2.0.0
                    
#: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.Opc&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=MasterServer.Lib.Plc.Opc&version=2.0.0
                    
Install as a Cake Tool

MasterServer.Lib.Plc.Opc

OPC UA通信库,基于OpcUaHelper实现与OPC UA服务器的通信功能。 作者邮箱:dengjianhua1999@qq.com

特性

  • 支持OPC UA协议
  • 基于OpcUaHelper库的稳定实现
  • 支持节点读写操作
  • 支持订阅和数据变化通知
  • 支持批量操作
  • 自动连接管理和重连
  • 集成日志记录
  • 支持多种数据类型

支持的数据类型

  • 布尔型 (bool)
  • 字节型 (byte/sbyte)
  • 短整型 (short/ushort)
  • 整型 (int/uint)
  • 长整型 (long/ulong)
  • 单精度浮点 (float)
  • 双精度浮点 (double)
  • 字符串 (string)
  • 日期时间 (DateTime)
  • 字节数组 (byte[])
  • 对象 (object)

核心组件

1. OpcConnectionInfo

OPC UA连接信息类,包含:

  • ServerUrl: OPC UA服务器URL
  • UserName: 用户名(可选)
  • Password: 密码(可选)
  • TimeOut: 超时时间
  • ReConnectSeconds: 重连间隔

2. OpcItem<T>

单个节点操作类,支持:

  • 读取指定节点的数据
  • 写入指定节点的数据
  • 节点ID格式:ns=2;s=节点名 或 ns=2;i=数字ID

3. OpcArray<T>

数组节点操作类,支持:

  • 批量读取节点数据
  • 批量写入节点数据
  • 数据变化检测和订阅

使用方式

1. 定义PLC配置(案例)

与MasterServer.Lib.Plc使用方式完全相同

using MasterServer.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("Opc");参数修改为Opc,其他完全相同。

string ConfigDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config");
Installer.Install("Opc");  
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="OpcConnectionInfo">
    <Id>plc2</Id>
    <Address>opc.tcp://127.0.0.1:53530/OPCUA/SimulationServer/</Address>
    <TimeOut>5000</TimeOut>
    <ReConnectSeconds>1</ReConnectSeconds>
  </Connection>

  <Heartbeat xsi:type="OpcItemOfInt16" Addr="ns=3;i=1030" />


  >
  <TestShort xsi:type="OpcItemOfInt16"  Addr="ns=3;i=1014" />
  <TestFloat xsi:type="OpcItemOfSingle" Addr="ns=3;i=1015" />
  <TestBool xsi:type="OpcItemOfBoolean" Addr="ns=3;i=1016" />

  <TestShortArray xsi:type="OpcArrayOfInt16" Addr="ns=3;i=1008" Name="测试int数组" IsSubscribe="true" IsVisible="true">
    <DataItem xsi:type="OpcItemOfInt16" Name="TestShortArray1" />
    <DataItem xsi:type="OpcItemOfInt16" Name="y" />
    <DataItem xsi:type="OpcItemOfInt16" Name="z" />
  </TestShortArray>
  <TestFloatArray xsi:type="OpcArrayOfSingle" Addr="ns=3;i=1009" Name="测试float数组" IsSubscribe="true" IsVisible="true">
    <DataItem xsi:type="OpcItemOfSingle" Name="TestFloatArray1.1" />
    <DataItem xsi:type="OpcItemOfSingle" Name="y" />
    <DataItem xsi:type="OpcItemOfSingle" Name="z" />
  </TestFloatArray>

  <TestBoolArray xsi:type="OpcArrayOfBoolean" Addr="ns=3;i=1013" Name="测试bool数组" IsSubscribe="true" IsVisible="true">
    <DataItem xsi:type="OpcItemOfBoolean" Name="x">
      <ExtParams>
        <TrueColor>Red</TrueColor>
      </ExtParams>
    </DataItem>
    <DataItem xsi:type="OpcItemOfBoolean" Name="y">
      <ExtParams>
        <TrueColor>Green</TrueColor>
      </ExtParams>
    </DataItem>
    <DataItem xsi:type="OpcItemOfBoolean" Name="z">
      <ExtParams>
        <TrueColor>Blue</TrueColor>
      </ExtParams>
    </DataItem>
  </TestBoolArray>

  <TestString xsi:type="OpcItemOfString" Addr="ns=3;i=1012" />
</PlcConfig>

节点ID格式

OPC UA节点ID支持多种格式:

  • 字符串格式: ns=2;s=MyVariable
  • 数字格式: ns=2;i=12345
  • GUID格式: ns=2;g=12345678-1234-1234-1234-123456789012
  • 字节字符串格式: ns=2;b=SGVsbG8=

安装

通过NuGet安装:

Install-Package MasterServer.Lib.Plc.Opc

依赖

  • OpcUaHelper
  • MasterServer.Lib.Plc
  • MasterServer.Lib.Logging</content>
Product 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.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0 99 4/23/2026
1.0.6 174 12/26/2025
1.0.5 190 12/25/2025
1.0.4 191 11/26/2025
1.0.2 200 11/26/2025
1.0.1 202 11/26/2025
1.0.0 200 11/26/2025