KupaKuper_EthernetWrapper 2.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package KupaKuper_EthernetWrapper --version 2.0.1
                    
NuGet\Install-Package KupaKuper_EthernetWrapper -Version 2.0.1
                    
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="KupaKuper_EthernetWrapper" Version="2.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="KupaKuper_EthernetWrapper" Version="2.0.1" />
                    
Directory.Packages.props
<PackageReference Include="KupaKuper_EthernetWrapper" />
                    
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 KupaKuper_EthernetWrapper --version 2.0.1
                    
#r "nuget: KupaKuper_EthernetWrapper, 2.0.1"
                    
#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 KupaKuper_EthernetWrapper@2.0.1
                    
#: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=KupaKuper_EthernetWrapper&version=2.0.1
                    
Install as a Cake Addin
#tool nuget:?package=KupaKuper_EthernetWrapper&version=2.0.1
                    
Install as a Cake Tool

1. 类概述

BaseEthernet 是一个抽象类,为以太网通信提供基础功能,主要用于与PLC等设备进行连接、数据读写和心跳检测。该类定义了连接管理、数据读写的标准接口,子类需实现具体的通信协议。

命名空间:KupaKuper_EthernetWrapper.Ethernet

2. 类结构与成员

2.1 连接属性与方法

成员 类型 描述
Connected 属性 (bool) 获取当前连接状态,设置时触发ConnectChanged事件
ReconnectTime 属性 (int) 重连间隔时间(毫秒),默认值为1000
Reconnect() 方法 (virtual async Task) 重新连接设备,包含锁定机制防止重复重连
CloseAsync() 方法 (abstract Task) 关闭连接,子类必须实现
ConnectAsync() 方法 (abstract Task) 连接PLC,子类必须实现
ConnectChanged 委托事件 (Action<br><br>?) 连接状态改变时触发的事件

2.2 数据读写方法

成员 类型 描述
ReadNode<T>(string Adr) 方法 (abstract T?) 读取变量地址的底层方法,子类必须实现
ReadNodes<T>(List<string> Adrs) 方法 (abstract T?) 读取变量地址列表的底层方法,子类必须实现
Read(string trg) 方法 (virtual object?) 读取单个变量内容,处理数组类型转换
TryRead(string trg, out object? value) 方法 (bool) 尝试读取变量,返回是否成功
Read(List<string> trgs) 方法 (virtual object?) 读取一列变量内容
TryRead(List<string> trg, out object? value) 方法 (bool) 尝试读取一列变量,返回是否成功
ReadString(string trg, string encoding = "UTF-8") 方法 (virtual string) 读取字符串变量并指定编码格式
TryReadString(string trg, string encoding, out object? value) 方法 (bool) 尝试读取字符串变量,返回是否成功
Read<T>(string trg) 方法 (virtual T?) 读取指定类型的变量
TryRead<T>(string trg, out bool value) 方法 (T?) 尝试读取指定类型变量,返回值和成功状态
WriteNode(string trg, object value) 方法 (abstract void) 写入变量地址的底层方法,子类必须实现
Write(string trg, object value) 方法 (virtual void) 修改变量内容
TryWrite(string trg, object value) 方法 (bool) 尝试修改变量内容,返回是否成功

2.3 心跳检测功能

成员 类型 描述
_heartbeatWriteState 字段 (bool) 心跳写入状态跟踪变量(布尔类型)
_heartbeatNumericState 字段 (int) 心跳写入状态跟踪变量(数值类型)
_isHeartbeatBoolType 字段 (bool) 心跳类型标志,默认为bool类型
OpenConnect(string ClientUrl, string HeartbeatAddress) 方法 (void) 启动连接监控和心跳检测

3. 使用说明

3.1 创建子类实现

要使用BaseEthernet,首先需要创建一个继承自它的子类,并实现所有抽象方法:

public class MyEthernet : BaseEthernet

{

    public override async Task CloseAsync()

    {

        // 实现关闭连接的具体逻辑

    }

    public override async Task ConnectAsync()

    {

        // 实现连接逻辑

        // 连接成功后设置Connected = true;

    }

    public override T? ReadNode<T>(string Adr)

    {

        // 实现单个节点读取的具体逻辑

        return default;

    }

    public override T? ReadNodes<T>(List<string> Adrs)

    {

        // 实现多节点读取的具体逻辑

        return default;

    }

    public override void WriteNode(string trg, object value)

    {

        // 实现写入节点的具体逻辑

    }

}

3.2 初始化与连接

// 创建实例

var ethernet = new MyEthernet();

// 设置重连间隔

ethernet.ReconnectTime = 2000; // 2秒

// 注册连接状态变化事件

ethernet.ConnectChanged = isConnected =>

{

    if (isConnected)

    {

        Console.WriteLine("连接成功");

    }

    else

    {

        Console.WriteLine("连接断开");

    }

};

// 启动连接监控和心跳检测

string clientUrl = "192.168.1.100:502"; // PLC地址

string heartbeatAddress = "DB1.DBX0.0"; // 心跳变量地址

ethernet.OpenConnect(clientUrl, heartbeatAddress);

3.3 数据读写操作

读取数据
// 读取布尔值

bool? boolValue = ethernet.Read<bool>("address");

// 读取整数

int? intValue = ethernet.Read<int>("address");

// 读取字符串

string strValue = ethernet.ReadString("address", "UTF-8");
写入数据
// 写入布尔值

ethernet.Write("address", true);

// 写入整数

ethernet.Write("address", 123); 

// 写入浮点数

ethernet.Write("address", 3.14f);

ethernet.Write("address", 2.71828);

// 写入字符串

string message = "Hello PLC";

ethernet.Write("address", stringBytes);

// 写入数组

int[] dataArray = { 1, 2, 3, 4, 5 };

ethernet.Write("address", dataArray);
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on KupaKuper_EthernetWrapper:

Package Downloads
KupaKuper_HMI_Config

KupaKuper_HMI_Device的支持包,主要包含配置文件读取和解析,提供数据模型和结构

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.0.1 99 10/14/2026
2.2.5 570 7/13/2026
2.2.4 416 4/21/2026
2.2.3 450 3/24/2026
2.2.2 176 3/21/2026
2.2.1-beta 200 3/16/2026
2.1.1 283 2/28/2026
2.0.1 571 8/28/2025
2.0.0 332 8/26/2025
1.2.1 274 8/26/2025
1.2.0 354 8/25/2025
1.2.0-beta 227 8/22/2025
1.1.2-beta 206 8/2/2025
1.1.0-beta 127 8/2/2025
1.0.0 295 7/28/2025