CSoft.Protocol.HJ212 10.100.2

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

HJ212 协议库(v2 重构版)

实现 HJ/T 212-2017 / HJ 212-2025《污染源在线自动监控(监测)系统数据传输标准》的 C# 协议库,现场机(设备端)与上位机(服务端)合于一个包: 对旧 CSoft.Protocol.HJ212(D:\GitHub\HJ212)与 CSoft.Protocol.HJ212_Server(D:\GitHub\HJ212_Server)的融合重构—— 协议就是同样两份,当年拆成了两个库、两套模型、两套接口;本库用同一套编解码与命令注册表支撑两侧,协议版本差异数据化、公共接口收敛。

设计文档见 DESIGN.md。

特点

  • 版本差异收敛在一张命令注册表:每条线上命令一行(两版 CN、方向、应答语义、双向 CN 的认领判别)。2017 独有(比对采样 3014、采样周期 3016/3017)、2025 独有(硬件序号 1001、心跳 9015、标样核查 2062/3024 等)都只是数据行,管线代码不动。
  • 一套核心,两侧共用:Hj212Client(现场机)与 Hj212Server(上位机,多客户端)共用帧编解码、CP 编解码、模型与注册表,取代旧版两个 NuGet 包的重复实现;服务端补上 2025 支持。
  • 一条命令一份定义,取代旧版约 6 个工件(UploadReq / SendReq / GetCmd / Rsp / 事件 / 处理方法),旧 IGB 的 85 个成员收敛为 Hj212Client 的十余个。
  • 应答/重发/分包统一在会话层:9011 → 数据 → 9012 查询流程、9013/9014 等待、超时重发(OverTime/ReCount)、PNUM/PNO 分包(各自独立 QN)全部内建。
  • CP 段一次编解码:标量 + 数据组(a1001-Rtd=…,a1001-Flag=N)通用读写,数据组字段不再散落在各命令的字符串插值里。
  • 构建在 CSoft 通信库上:CSoft.TopPortLib 的 PigeonPort(全双工队列 + 主动推送)+ HeadLengthParser(##+长度组帧)+ IPhysicalPort(串口/TCP/命名管道),遵循 caoyiliang/Communication 的 Pigeon 协议模型;请求-应答以 QN 配对(Hj212Req/Hj212AckRsp),CRC 用 Utils.CRC.GBcrc16 逐帧把关。
  • CRC 与帧格式对照标准附录 A(poly 0xA001、init 0xFFFF、高位在前),以标准示例帧 2200/1C80 为基准向量测试。

快速上手

using Communication.Bus.PhysicalPort;
using HJ212.Codec;
using HJ212.Commands;
using HJ212.Session;
using HJ212.Typed;

// 物理口来自 CSoft.Communication(串口/TCP/命名管道均可)
await using var session = new Hj212Client(
    new TcpClient("127.0.0.1", 2756),
    new Hj212Options { Mn = "88888888", Version = Hj212Version.V2025 });

await session.OpenAsync();

// 上传实时数据(等待 9014 数据应答,超时自动重发)
await session.UploadRealtimeAsync(DateTime.Now,
[
    new RealtimeItem("a1001") { Rtd = "20.5", Flag = "N" },
]);

// 处理上位机命令:会话自动回 9011 → 处理器 → 9012
session.On(CommandCatalog.GetSystemTime, req =>
    new CommandReply.Data([new CpSection().Set("SystemTime", DateTime.Now.ToString("yyyyMMddHHmmss"))]));

session.On(CommandCatalog.SetSystemTime, req =>
{
    ApplySystemTime(req.GetDateTime("SystemTime")!.Value);
    return new CommandReply.Success();
});

上位机(服务端)

using Communication.Bus.PhysicalPort;
using HJ212.Commands;
using HJ212.Server;
using HJ212.Session;

await using var server = new Hj212Server(
    new TcpServer("0.0.0.0", 2756),
    new Hj212Options { Version = Hj212Version.V2025 });

server.OnClientConnect += async clientId => { /* … */ await Task.CompletedTask; };

// 接收上传:自动回 9014/9013 后分发
server.On(CommandCatalog.UploadRealtimeData, (clientId, req) =>
{
    var dataTime = req.GetDateTime("DataTime");
    return Task.CompletedTask;
});

await server.StartAsync();

// 下发控制命令:自动组装 9011 → 9012 事务
await server.SetSystemTimeAsync(clientId, "w01018", DateTime.Now);

// 下发查询命令:自动组装 9011 → 数据包*N(逐包回 9014)→ 9012
var packets = await server.GetStatisticsDataAsync(clientId,
    CommandCatalog.GetMinuteData, beginTime, endTime);

自定义命令(未写糖的)直接走通用 API:

var cp = new CpSection()
    .Set("DataTime", dataTime)
    .Add(new DataGroup("w01018").Add("SampleRd", "12.5").Add("StandardValue", "12").Add("ResultType", "1"));
await session.SendAsync(CommandCatalog.UploadAutoStandardCheckData, cp);

离线组帧(调试/回放):

string frame = session.BuildFrame(CommandCatalog.UploadMinuteData, cp);

从旧版迁移

现场机(旧 CSoft.Protocol.HJ212 / IGB):

旧 API(IGB / GB) 新 API
new GB(name, port, mn, version: HJT212_2025) new Hj212Client(port, new(){ Mn = mn, Version = V2025 })
gb.UploadRealTimeData(dt, data) session.UploadRealtimeAsync(dt, items)
gb.SendRealTimeData(dt, data) session.SendAsync(CommandCatalog.UploadRealtimeData, cp, new(){ WaitAck = false })
gb.GetSendMinuteDataCmd(...) session.BuildFrame(CommandCatalog.UploadMinuteData, cp)
gb.OnGetSystemTime += h session.On(CommandCatalog.GetSystemTime, req => …)
gb.OnSetOverTimeAndReCount += h session.On(CommandCatalog.SetOverTimeAndReCount, req => …)
List<StatisticsData> List<StatisticsItem>(字段同名:Cou/Min/Avg/Max/Zs*/Flag)

上位机(旧 CSoft.Protocol.HJ212_Server / IGB_Server,现并入本库并支持 2025):

旧 API(IGB_Server / GB_Server) 新 API
new GB_Server(new TcpServer(...)) new Hj212Server(new TcpServer(...), new(){ Version = V2025 })
gb.SetSystemTimeAsync(clientId, mn, pw, st, …) server.SetSystemTimeAsync(clientId, polId, time)(MN/PW/ST 取自 Hj212Options)
gb.GetMinuteDataAsync(clientId, …) server.GetStatisticsDataAsync(clientId, CommandCatalog.GetMinuteData, begin, end)
gb.OnUploadRealTimeData += h server.On(CommandCatalog.UploadRealtimeData, (clientId, req) => …)
gb.OnAskSetSystemTime += h server.On(CommandCatalog.AskSetSystemTime, (clientId, req) => …)
逐命令方法(40 成员) 通用 QueryAsync / ControlAsync + ServerExtensions 薄糖

行为差异(修复旧版问题):

  • 分包消息各自独立 QN(标准要求,旧版全包同 QN)。
  • 2025 新增 RF 补传标志:SendOptions.Retransmit = true。
  • 留样信息按标准走 CN 3015(2025 C63);旧版误用 2031。
  • 按标准,2017 的 3014/3016/3017 在 2025 版本下不可用(注册表 Cn2025 = null),旧版不区分。

目录结构

src/HJ212/
  Framing/    Crc16(Utils.CRC 包装), Hj212Frame(##dddd…CRC…CRLF)
  Messages/   Hj212Message(QN/ST/CN/PW/MN/Flag/PNUM/PNO/RF/CP), MessageCodec
  Codec/      CpSection, DataGroup, CpCodec
  Commands/   CommandSpec, CommandCatalog(全量命令注册表)
  Session/    Hj212Client(现场机,PigeonPort + IProtocol), Hj212Req/各 Rsp, CommandReply, 选项
Server/     Hj212Server(上位机,PigeonPort_Server 多客户端), ServerExtensions 强类型糖
  Typed/      数据组模型(RealtimeItem/StatisticsItem), 便捷扩展
tests/HJ212.Tests/   853 项测试:42 项单元/会话/服务端事务 + 811 项协议语料回归
                     (附录 C 示例原文 134 条 × 解析/版本位/CP 往返/方向认领/分包位 6 项校验 + 重点语义断言)

构建与测试

dotnet build HJ212.slnx
dotnet test

目标框架:net8.0 / net9.0 / net10.0。

版本 10.100.1 新增:Hj212Identity 逐调用 MN/PW/ST 覆盖(多设备服务端)、SendOptions.TimeoutMs 逐调用超时、GetSystemTimeAsync 支持 PolId。

标准差异要点(详见 DESIGN.md)

  • 帧结构、CRC、应答机制两版一致;2025 新增 RF 补传标志、心跳 9015、SM4 加密(本库暂未实现加密,预留扩展点)。
  • Flag:bit0=应答、bit1=分包,两版位置不变(A/D 仅命名对调);版本位 2017=4、2025=8。
  • ST 51/52 两版同值异义(工况 → 温室气体/碳足迹),跨版本解析先按 Flag 判版。
  • 数据段上限 1024 字符触发分包。
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 is compatible.  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 is compatible.  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

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
11.100.1 149 11/13/2026
11.100.0 103 11/13/2026
10.100.3 99 10/28/2026
10.100.2 102 10/28/2026
10.100.1 99 10/28/2026
10.100.0 103 10/28/2026
10.1.0 179 5/21/2026
10.0.8 163 1/28/2026
10.0.7 149 1/27/2026
10.0.6 146 1/14/2026
10.0.5 152 1/14/2026
10.0.4 159 1/13/2026
10.0.3 156 1/13/2026
10.0.2 156 1/13/2026
10.0.1 150 1/12/2026
10.0.0 358 11/12/2025
9.4.7 270 9/24/2025
9.4.6 267 8/15/2025
9.4.4 274 8/14/2025
9.4.3 329 8/5/2025
Loading failed

现场机与上位机融合重构(Hj212Client/Hj212Server):统一命令注册表承载 2017/2025 版本差异,PigeonPort/PigeonPort_Server 构建,应答/重发/分包统一会话层,协议附录 C 示例全量回归。