TianWeiToolsPro 3.1.0

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

TianWeiToolsPro

TianWeiToolsPro 是天威开发的设备/通信相关工具集合,主要沉淀 PLC、串口设备等常用工业通讯与设备控制能力,并与 TianWeiToolsPro.Common(通用基础能力)以及 TianWeiToolsPro.Controls(WPF 控件/主题库)配套使用。

目标框架

该项目为多目标框架构建(以 TianWeiToolsPro.csproj 为准),在当前工作区可见包含:

  • .NET Framework 4.7.2
  • .NET Framework 4.8
  • .NET 6 (Windows)
  • .NET 8 (Windows)
  • .NET 10 (Windows)

模块概览

Device

设备与通信相关能力集中在 Device 命名空间下。

Plc

PLC 相关操作助手(以源码实现为准),当前工程中可见:

  • 欧姆龙(Omron)UDP 协议相关 PLC 操作
  • 欧姆龙(Omron)TCP 协议相关 PLC 操作(新增)
  • 三菱(Melsec)UDP 协议相关 PLC 操作
  • 三菱(Melsec)TCP 协议相关 PLC 操作(新增)
  • Modbus UDP 协议相关 PLC 操作(2026-02-11 16:30 新增)
  • Modbus TCP 协议相关 PLC 操作(2026-02-11 16:30 新增)
  • 三菱 MC 协议 UDP 仿真器 MelsecMcUdpSimulator(新增,用于本地联调/测试)

典型能力(以实际类/方法为准):

  • 连接/断开
  • 读写寄存器/位
  • 常用地址解析与报文封装
SerialPort

串口设备操作类:用于直流电源、电子负载等设备的通讯与控制(以具体设备驱动类为准)。

典型能力(以实际类/方法为准):

  • 串口参数配置(波特率/校验位/停止位等)
  • 发送/接收以及协议封装
  • 设备状态查询与指令执行

Service

Service 命名空间主要提供一些偏“应用层”的通用服务封装(通常与 UI 线程/提示框/网络连通性等相关),便于业务侧直接调用。

MsgBoxService

用于弹出模态对话框类消息(内部会切换到 UI Dispatcher 执行)。依赖 TianWeiToolsPro.Controls 中的 MessageBox

using TianWeiToolsPro.Service;

// 信息/错误/异常
MsgBoxService.ShowInfos("操作完成");
MsgBoxService.ShowError("参数错误");
MsgBoxService.ShowException(new Exception("something wrong"));

// 询问(是/否)
bool ok = MsgBoxService.ShowAsk("是否继续?");

注意:该服务依赖 Application.Current.Dispatcher,通常需在 WPF 应用环境中使用。

NoticeBoxService

用于弹出非模态通知提示(Toast/Notice),支持自动消失时间。依赖 TianWeiToolsPro.Controls 中的 NoticeBox

using TianWeiToolsPro.Service;

NoticeBoxService.ShowInfos("连接成功");
NoticeBoxService.ShowWarning("温度偏高", time: 5);
NoticeBoxService.ShowError("写入失败");

try
{
    // ...
}
catch (Exception ex)
{
    NoticeBoxService.ShowException(ex);
}
PingHelper

用于检测网络连通性(ICMP Ping),支持同步/异步。

using TianWeiToolsPro.Service;

bool reachable = PingHelper.Ping("192.168.1.10", timeout: 1000);
bool reachable2 = await PingHelper.PingAsync("192.168.1.10", timeout: 1000);
TimeHelper

提供一个“稍微高精度”的延时方法(忙等待 + Thread.Sleep(0)),误差约 1~2ms。

using TianWeiToolsPro.Service;

// 延时 10ms(参数单位:毫秒)
TimeHelper.Delay(10);

注意:这是阻塞式延时,会占用线程;不建议在 UI 线程中长时间调用。

与其他项目的关系

  • TianWeiToolsPro.Common:通用扩展方法、CRC/校验、MVVM 基类、命令/事件总线等基础能力
  • TianWeiToolsPro.Controls:WPF 自定义控件与主题资源(若你的应用是 WPF UI)

使用方式

方式一:项目引用

TianWeiToolsPro 添加到解决方案并在你的业务项目中引用。

方式二:引用 DLL

编译后引用输出的 DLL。

说明

由于该库涉及与设备通讯,部分能力与现场协议、设备型号、寄存器地址等强相关;建议在业务侧对协议/地址配置做隔离与版本管理,避免硬编码分散。

变更记录 2026-02-11 16:30

  • 新增:MelsecTcp
  • 新增:OmronTcp
  • 新增:ModbusTcp
  • 新增:ModbusUdp
  • 新增:MelsecMcUdpSimulator,用于本地联调/测试,后期将支持更多协议仿真
ModbusTcp / ModbusUdp 使用示例

注意:ModbusTcp / ModbusUdp 当前地址只支持“纯数字的字符串”格式(例如 "0""1""100"),不支持 "D100""X1""M100" 等带前缀的地址写法。

using System;
using TianWeiToolsPro.Device.Plc;

// 仅示例:以源码中的构造参数/方法为准

// TCP
var modbusTcp = new ModbusTcp("192.168.1.10", port: 502);
modbusTcp.Connect();
modbusTcp.WriteWord("100", 123);
var v1 = modbusTcp.ReadWord("100");
modbusTcp.Disconnect();

// UDP
var modbusUdp = new ModbusUdp("192.168.1.10", port: 502);
modbusUdp.Connect();
modbusUdp.WriteBit("1", true);
var b1 = modbusUdp.ReadBit("1");
modbusUdp.Disconnect();

Console.WriteLine($"Word[100]={v1}, Bit[1]={b1}");
MelsecMcUdpSimulator 使用示例

用于在本机启动一个“三菱 MC 协议 UDP”的简易仿真器,便于业务侧在没有真实 PLC 的情况下进行联调/自动化测试。

using System;
using System.Net;
using TianWeiToolsPro.Device.Plc.Simulator;

// 仅示例:以源码中的构造参数/方法为准
// 初始化时可配置端口与绑定 IP(不指定则默认 IPAddress.Any)
var simulator = new MelsecMcUdpSimulator(port: 6000, bindAddress: IPAddress.Any);

// 启动监听
simulator.Start();

// 写入一些测试数据(例如 D 寄存器),然后用你的 Melsec MC/UDP 客户端去读取验证
// 下面 API 名称/地址格式以源码实现为准:
simulator.WriteWord("D100", 123);
simulator.WriteBit("M100", true);

// 读取当前仿真器内存(可用于断言/调试)
var d100 = simulator.ReadWord("D100");
var m100 = simulator.ReadBit("M100");

Console.WriteLine($"D100={d100}, M100={m100}");

// 停止
simulator.Stop();

外部监控地址数据变更(订阅事件):

using System;
using System.Net;
using TianWeiToolsPro.Device.Plc;

// 初始化时可配置端口与绑定 IP(不指定则默认 IPAddress.Any)
var simulator = new MelsecMcUdpSimulator(port: 6000, bindAddress: IPAddress.Any);

// 订阅:当外部客户端(或本地 SetD) 写入 D 寄存器时触发
simulator.DWordChanged += (s, e) =>
{
    // 可按地址过滤要监控的点位
    if (e.Address == 100)
    {
        Console.WriteLine($"D{e.Address} changed: {e.OldValue} -> {e.NewValue}");
    }
};

simulator.Start();

// ...此时用你的 MC/UDP 客户端写入 D100,或调用 simulator.SetD(100, xxx)

simulator.Stop();
Product Compatible and additional computed target framework versions.
.NET net6.0-windows7.0 is compatible.  net7.0-windows was computed.  net8.0-windows was computed.  net8.0-windows7.0 is compatible.  net9.0-windows was computed.  net10.0-windows was computed.  net10.0-windows7.0 is compatible. 
.NET Framework net472 is compatible.  net48 is compatible.  net481 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
3.1.0 103 3/23/2026
3.0.61 128 2/27/2026
Loading failed