Sail.Device.Protocol 0.1.1

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

Sail.Device.Protocol

独立下位机协议组件,目标 net8.0,当前版本 0.1.1。没有 WPF、WinForms、数据库、日志库或其他组件依赖;在 Sail.Component 解决方案中独立打成 DLL / NuGet 包。文件直接放在项目根目录,避免为小组件增加目录层级。

已提取的内容

默认入口 DeviceCommands 使用旧程序已经运行的17个发送格式:ACK、基本状态、动作/密封/时间试验、调压、修正、压力置零、放气、IPv4网络配置、单口、压紧机构、传感器配置及停止。密封及稳压输入分钟,保留历史短请求、特殊长度和尾部字段。

LegacyDeviceCommands 仍可使用,与默认入口共享同一编码实现。YlsfDocumentCommands 保留文档候选实现,包括多口、DA、模拟读取及22命令族,只有固件确认后才显式使用;不进入当前默认接入流程。网络端口及MAC没有已确认布局,不生成推测报文。

字节序低位在前;CRC16/Modbus 的实际算法及低字节顺序来自旧代码和运行日志(文档没有CRC章节),设备报文不是标准Modbus。文档与旧代码的差异、仍待固件确认的矛盾,见解决方案 docs/下位机协议核对.md。

DeviceFrameDecoder 处理 TCP 半帧、多帧和 CRC 错误;每连接单独实例,串行调用,重连时 Reset()。DeviceProtocolClient 与直接调用的 DeviceMessageParser 均默认 LegacyApplication:描点时间UInt32,动作完成开启压力Int16、关闭压力Int32并保留两个接点位图,调压按载荷首字节判断。实时状态、时间、密封及修正输出独立模型。语义字段不足返回 MalformedDeviceMessage(直接调用解析器则抛 InvalidDataException),未知合法报文保留原始帧。

协议不修改全局变量、累计校验次数、筛掉重复描点、弹窗或写报告,这些由宿主根据返回模型处理。

连接及发送由宿主提供

using Sail.Device.Protocol;

// stream 由主程序连接后提供,例如 TcpClient.GetStream()。
// 多个并发命令共用此适配器,宿主不能同时绕过它写同一条连接。
var protocol = new DeviceProtocolClient(new StreamDeviceTransport(stream));
await protocol.SendAsync(DeviceCommands.ReadBaseState(), cancellationToken);
await protocol.SendAsync(DeviceCommands.PressureCheck(rate: 3, contactCount: 1, pressureLimitKpa: 100), cancellationToken);
await protocol.SendAsync(DeviceCommands.SealCheck(durationMinutes: 2, pressureKpa: 100, stabilizationMinutes: 5), cancellationToken);

也可实现 IDeviceTransport 对接自己的通信库。该接口必须完整写出报文,写入失败和取消向上抛异常。协议不会自动重发控制命令,成功返回仅表示写入成功,设备是否接受及动作完成必须由接收通知确认。连接、心跳、超时、自动重连和流的释放由宿主负责。

以上代码无需指定协议版本即可使用旧版。已有 LegacyDeviceCommands 调用保持有效。

将来确认固件与文档一致后,发送和接收必须一起显式切换:

var protocol = new DeviceProtocolClient(transport, dialect: DeviceProtocolDialect.YlsfDocument);
await protocol.SendAsync(YlsfDocumentCommands.ReadBaseState());
await protocol.SendAsync(YlsfDocumentCommands.SealCheck(120, 100)); // 文档版:密封秒,无稳压扩展。

两版“动作完成”和“调压通知”的字段含义不同,不能只切换发送命令而保留错误的接收版本。

接收循环处理 ReadAsync 的实际长度,不能传入缓冲区的未读尾部:

var buffer = new byte[1024];
while (true)
{
    int count = await stream.ReadAsync(buffer, cancellationToken);
    if (count == 0) break; // 对端关闭,退出读取并由宿主处理重连。
    foreach (var message in protocol.Feed(buffer.AsSpan(0, count)))
    {
        if (message is BaseStateMessage state)
        {
            // WPF 更新绑定时通过 Dispatcher 切回 UI 线程。
            Console.WriteLine($"压力 {state.PressureKpa} kPa,温度 {state.TemperatureCelsius} ℃");
        }
    }
}
protocol.Reset();

接收长度约定及待设备核对项

文档的下位机反馈行统一标记载荷长度 0A,报文总长16字节;按实际长度字段读取,允许未知固件扩展。接收格式为 02 / 功能 / 子命令 / 载荷字节数 / 载荷 / CRC低 / CRC高,总长度为第四字节加6;兼容旧短帧 FE 与 0E 两字节正文总长4。历史描点日志 0202010A01003A00000000000000A134 同时确认16字节布局和CRC,固定向量已独立写入测试,不读取外部日志文件。

各文档类型已按所列偏移与合成响应验证,仍需实际固件联调,当前没有连接设备。

如果固件接收长度字节另有含义,可明确按功能/子命令覆盖总长(含帧头、CRC),例如确认基本状态帧总长为 14 字节后:

var lengths = new Dictionary<DeviceFrameKey, int>
{
    [new(0x01, 0x01)] = 14
};
var protocol = new DeviceProtocolClient(transport, lengths);

不要将发送命令的特殊长度直接套用到接收帧。损坏的长度字节可能使组帧器等待更多数据;缓冲上限为 261 字节,宿主接收超时或重连时调用 Reset。正常半帧不会因载荷中的 02 被截断;已知 CRC 失败后,以后续完整且 CRC 合法的帧重新同步。

压力×100、时间校验秒×1000、模拟电压×100 的系数来自旧实现,文档未重述。文档版密封输入秒、上限64800秒;旧版密封输入分钟并追加稳压分钟。文档描点时间为UInt16、顶部说明秒;旧版为UInt32原值。TimeRaw 不做累加,宿主按所选版本处理。DA、模拟值、距离和氢气值的比例未定义,以原值返回。

输入越界、非有限数、非法枚举会拒绝。默认旧版单点固定1..6、批量六组。文档修正点顶部1..10与六点表格有差异,仅显式文档入口允许单点1..10,批量仍固定六组。上述差异及发送长度矛盾均在核对文档中列明。

验证与打包

dotnet test tests/Sail.Device.Protocol.Tests/Sail.Device.Protocol.Tests.csproj -c Release
dotnet run --project samples/DeviceProtocolDemo/DeviceProtocolDemo.csproj
./build/Pack-DeviceProtocol.ps1
./build/Verify-DeviceProtocolPackage.ps1

演示默认完全离线:输出旧版编码报文、输入合成半帧和旧动作完成帧并验证默认解析,不连接或控制实际设备。测试涵盖默认发送报文的旧固定向量、默认旧接收、显式文档布局、特殊长度、CRC、边界、半帧、多帧、负数、传输异常及取消。打包生成 artifacts/nuget/Sail.Device.Protocol.0.1.1.nupkg,不公开上传。

从0.1.0升级:此前文档 DeviceCommands 改为 YlsfDocumentCommands;继续文档试验需显式指定 YlsfDocument。默认 DeviceCommands.SealCheck 改为三个参数(密封分钟、压力、稳压分钟),不接受原文档两参数秒调用,避免单位变化被静默接受。默认入口只包含旧程序已有命令,多口、DA等文档扩展使用显式文档入口。

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.
  • net8.0

    • No dependencies.

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
0.1.1 119 10/13/2026