Io.Github.KerwinXu.OpenProtocol
0.2.2
.NET 6.0
This package targets .NET 6.0. The package is compatible with this framework or higher.
.NET Framework 4.0
This package targets .NET Framework 4.0. The package is compatible with this framework or higher.
dotnet add package Io.Github.KerwinXu.OpenProtocol --version 0.2.2
NuGet\Install-Package Io.Github.KerwinXu.OpenProtocol -Version 0.2.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="Io.Github.KerwinXu.OpenProtocol" Version="0.2.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Io.Github.KerwinXu.OpenProtocol" Version="0.2.2" />
<PackageReference Include="Io.Github.KerwinXu.OpenProtocol" />
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 Io.Github.KerwinXu.OpenProtocol --version 0.2.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Io.Github.KerwinXu.OpenProtocol, 0.2.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 Io.Github.KerwinXu.OpenProtocol@0.2.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=Io.Github.KerwinXu.OpenProtocol&version=0.2.2
#tool nuget:?package=Io.Github.KerwinXu.OpenProtocol&version=0.2.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
摘要
我这个程序是类似ORM,ORM是面向对象与关系数据库之间的匹配的,而这个OpenProtocol是面向对象与通讯的字节流之间的匹配的,是一种序列化技术。
Summary
My program is similar to ORM, ORM is object-oriented and relational database matching, and OpenProtocol is object-oriented matching between communicating byte streams, which is a serialization technology.
所有注解:
- DataItem : 数目的项目
- Index : 下标
- Endianness : 字节序, ABCD : 大端,DCBA,小端
- Count : 某个数组的个数
- Value : 数据的个数
- 各个继承
- DefaultSize : 固定的个数
- OtherSize :根据别的项目判断
- StaticSizeByOther : 在某个变量为某个值的情况下,固定多少个
- VarSizeByOther :在某个变量为某个值的情况下,某个变量的值是个数
- Check : 校验的
Demo
// See https://aka.ms/new-console-template for more information
using Io.Github.KerwinXu.OpenProtocol;
using Io.Github.KerwinXu.OpenProtocol.Attributes;
using Io.Github.KerwinXu.OpenProtocol.Attributes.Checks;
using Io.Github.KerwinXu.OpenProtocol.Attributes.Counts;
namespace Demo
{
/// <summary>
/// modbus的请求
/// </summary>
class ModbusRequest
{
[DataItem(0)] // DataItem是数据项目,0表示第一项。
public byte SlaveId { get; set; }
[DataItem(1)]
public byte FunCode { get; set; }
[DataItem(2, "AB")] // AB表示大端。
public ushort RegAddress { get; set; }
[DataItem(3, "AB")]
public ushort RegCount { get; set; }
[DataItem(4)]
[DefaultSize(2)] // 固定的2个元素。可以
[CheckModbus(0, 3)]
public byte[] Sum { get; set; }
}
/// <summary>
/// modbus的响应
/// </summary>
class ModbusResponse
{
[DataItem(0)] // DataItem是数据项目,0表示第一项。
public byte SlaveId { get; set; }
[DataItem(1)]
public byte FunCode { get; set; }
[DataItem(2)]
public byte BytesCount { get; set; }
[DataItem(3, "AB")]
[OtherSize("BytesCount", true)] // 由BytesCount指定了字节数,是以字节计数的。
public ushort[] Data { get; set; }
[DataItem(4)]
[DefaultSize(2)] // 固定的2个元素。
[CheckModbus(0, 3)]
public byte[] Sum { get; set; }
}
class Program
{
static void Main(string[] args)
{
// 一个简单的modbus
// 请求命令
byte[] data1 = { 0x01, 0x03, 0x00, 0x00, 0x00, 0x01, 0x84, 0x0a };
BytesSerializer bytesSerializer = new BytesSerializer();
int end_index = 0;
// 转成对象
var obj = bytesSerializer.Deserialize<ModbusRequest>(data1, 0, ref end_index);
// 再转回去
var data2 = bytesSerializer.Serialize(obj);
Console.WriteLine($"{data1.SequenceEqual(data2)}");
// 回复的
byte[] data10 = { 0x01, 0x03, 0x02, 0x19, 0x98, 0xb2, 0x7e};
var obj2 = bytesSerializer.Deserialize<ModbusResponse>(data10,0, ref end_index);
Console.WriteLine($"{Convert.ToString(obj2.Data[0], 16)}");
// 这里添加一个更多的数组
byte[] data20 = { 0x01,
0x01, 0x03, 0x02, 0x19, 0x98, 0xb2, 0x7e ,
0x01, 0x03, 0x02, 0x19, 0x98, 0xb2, 0x7e ,
0x7e
};
var obj3 = bytesSerializer.Deserializes<ModbusResponse>(data20, 0, ref end_index);
Console.WriteLine($"count; {obj3.Count}, end_index:{end_index}");
}
}
}
支援作者 donation
contact me
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. 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 Framework | net40 is compatible. net403 was computed. net45 was computed. net451 was computed. net452 was computed. net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 is compatible. net481 was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETFramework 4.0
- No dependencies.
-
.NETFramework 4.8
- No dependencies.
-
net6.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.