# secs4net
[![NuGet](https://img.shields.io/nuget/v/secs4net.svg)](https://www.nuget.org/packages/Secs4Net)
**Project Description**
SECS-II/HSMS-SS/GEM implementation on .NET. This library provide easy way to communicate with SEMI standard compatible device.
**Getting started**
## Install nuget package
> dotnet add package Secs4Net
## Configure .NET dependency injection
```cs
public void ConfigureServices(IServiceCollection services)
{
// "secs4net" configuration section in the appsettings.json
// "secs4net": {
// "DeviceId": 0,
// "IsActive": true,
// "IpAddress": "127.0.0.1",
// "Port": 5000
// }
services.AddSecs4Net<DeviceLogger>(Configuration);
}
class DeviceLogger : ISecsGemLogger
{
// implement ISecsGemLogger methods
}
```
## Basic usage
```cs
try
{
var s3f17 = new SecsMessage(3, 17)
{
Name = "CreateProcessJob",
SecsItem = L(
U4(0),
L(
L(
A("Id"),
B(0x0D),
L(
A("carrier id"),
L(
U1(1)),
L(
U1(1),
A("recipe"),
L()),
Boolean(true),
L()))))
};
//access list
s3f17.SecsItem[1][0][0] == A("Id");
foreach(var item in s3f17.SecsItem[1][0][2].Items)
{
}
//access unmanaged arry item
byte b2 = s3f17.SecsItem[0].FirstValue<byte>(); // with different type
s3f17.SecsItem[0].FirstValue<byte>() = 0; // change original value
byte b3 = s3f17.SecsItem[0].GetFirstValueOrDefault<byte>(fallbackValueWhenItemIsEmpty);
Memory<byte> bytes = s3f17.SecsItem[0].GetMemory<byte>();
// access string item
string str = s3f17.SecsItem[1][0][0].GetString(); // str = "Id"
//await secondary message
var s3f18 = await secsGem.SendAsync(s3f17);
// process message with LINQ
var query =
from a in s3f18.SecsItem[3]
select new {
num = a.FirstValue<int>(),
};
}
catch(SecsException)
{
// exception when
// T3 timeout
// device reply SxF0
// device reply S9Fx
}
```
## Handle primary messages
```cs
await foreach (var e in secsGem.GetPrimaryMessageAsync(cancellationToken))
{
using var primaryMsg = e.PrimaryMessage;
//do something for primary message
// reply secondary message to device
using var secondaryMsg = new SecsMessage(...);
await e.TryReplyAsync(secondaryMsg);
};
```
## Creates `Item` via LINQ
```cs
using static Secs4Net.Item;
var s16f15 =
new SecsMessage(16, 15)
{
Name = "CreateProcessJob",
SecsItem = L(
U4(0),
L(
from pj in tx.ProcessJobs
select
L(
A(pj.Id),
B(0x0D),
L(
from carrier in pj.Carriers
select
L(
A(carrier.Id),
L(
from slotInfo in carrier.SlotMap
select
U1(slotInfo.SlotNo)))),
L(
U1(1),
A(pj.RecipeId),
L()),
Boolean(true),
L()))));
```
## Modify `Item` in restrict.
> Basic rule: The `Item.Count` has been fixed while the item was created.
You can only overwrite values on existing memory. String Item is immutable, coz C# `string` is immutable as well.
## Reuse array for large item values
All unmanaged data Item can created from `IMemoryOwner<T>` or `Memory<T>`.
The following sample uses the implementation of `IMemoryOwner<T>` from [`Microsoft.Toolkit.HighPerformance`](https://docs.microsoft.com/en-us/windows/communitytoolkit/high-performance/memoryowner) that has been referenced internally by secs4net..
```cs
var largeArrayOwner = MemoryOwner<int>.Allocate(size: 65535);
// feed the value into largeArrayOwner.Memory or largeArrayOwner.Span
FillLargeArray(largeArrayOwner.Memory);
using var s6f11 = new SecsMessage(6, 11, replyExpected: false)
{
Name = "LargeDataEvent",
SecsItem = L(
L(
I2(1121),
A(""),
I4(largeArrayOwner))), // create Item from largeArrayOwner
};
// apply using on received message as well. coz the item that decoded by PipeDecoder also using MemoryOwner<T> when the data array is big.
using var s6f12 = await secsGem.SendAsync(s6f11);
```
> `IMemoryOwner<T>`, `Item` and `SecsMessage` have implemented `IDisposable` don't forget to Dispose it when they don't need anymore.
Otherwise, the array will not return to the pool till GC collects.
> Since the max encoded bytes length in a single non-List Item was `16,777,215`(3 bytes), we split raw data into separated items.
In that case, creating the Items from sliced `Memory<T>` is more efficient.
## LINQPad
If you like to use LINQPad to dump `Item` object in a simplified format, you probably need a [custom dump method](http://www.linqpad.net/CustomizingDump.aspx) method. Adjust your LINQPad **My Extensions** file as follow
```cs
static object ToDump(object obj)
{
var objType = obj.GetType();
if (Type.GetType("Secs4Net.Item,Secs4Net") is { } itemType && objType.IsSubclassOf(itemType))
{
return Dump(obj);
}
return obj;
}
static object Dump(dynamic item)
{
return (int)item.Format switch
{
(int)SecsFormat.List => new { item.Format, Values = OnDemandList(item), },
(int)SecsFormat.ASCII => new { item.Format, Values = item.GetString() },
(int)SecsFormat.JIS8 => new { item.Format, Values = item.GetString() },
(int)SecsFormat.Binary => new { item.Format, Values = OnDemandMemory<byte>(item) },
(int)SecsFormat.Boolean => new { item.Format, Values = OnDemandMemory<bool>(item) },
(int)SecsFormat.I8 => new { item.Format, Values = OnDemandMemory<long>(item) },
(int)SecsFormat.I1 => new { item.Format, Values = OnDemandMemory<sbyte>(item) },
(int)SecsFormat.I2 => new { item.Format, Values = OnDemandMemory<short>(item) },
(int)SecsFormat.I4 => new { item.Format, Values = OnDemandMemory<int>(item) },
(int)SecsFormat.F8 => new { item.Format, Values = OnDemandMemory<double>(item)},
(int)SecsFormat.F4 => new { item.Format, Values = OnDemandMemory<float>(item) },
(int)SecsFormat.U8 => new { item.Format, Values = OnDemandMemory<ulong>(item) },
(int)SecsFormat.U1 => new { item.Format, Values = OnDemandMemory<byte>(item) },
(int)SecsFormat.U2 => new { item.Format, Values = OnDemandMemory<ushort>(item)},
(int)SecsFormat.U4 => new { item.Format, Values = OnDemandMemory<uint>(item) },
_ => $"Unsupported Format: {item.Format}",
};
static object OnDemandList(dynamic item) => Util.OnDemand($"[{item.Count}]", () => item.Items);
static object OnDemandMemory<T>(dynamic item) => Util.OnDemand($"[{item.Count}]", () => item.GetMemory<T>());
}
// You can also define namespaces, non-static classes, enums, etc.
enum SecsFormat
{
List = 0b_0000_00,
Binary = 0b_0010_00,
Boolean = 0b_0010_01,
ASCII = 0b_0100_00,
JIS8 = 0b_0100_01,
I8 = 0b_0110_00,
I1 = 0b_0110_01,
I2 = 0b_0110_10,
I4 = 0b_0111_00,
F8 = 0b_1000_00,
F4 = 0b_1001_00,
U8 = 0b_1010_00,
U1 = 0b_1010_01,
U2 = 0b_1010_10,
U4 = 0b_1011_00,
}
```
没有合适的资源?快使用搜索试试~ 我知道了~
FREESECS 开源hsms代码
共162个文件
cs:85个
csproj:13个
md:10个
需积分: 5 7 下载量 125 浏览量
2022-04-13
14:43:49
上传
评论
收藏 366KB ZIP 举报
温馨提示
是一个开源的SEMI SECS协议库,采用C#语言实现,可运行于Linux, MacOS X, NetBSD
资源详情
资源评论
资源推荐
收起资源包目录
FREESECS 开源hsms代码 (162个子文件)
NuGet.Config 210B
Form1.Designer.cs 28KB
HsmsConnection.cs 19KB
Item.Decode.cs 16KB
Item.UnitTest.cs 14KB
SmlReader.cs 13KB
SmlWriter.cs 12KB
Range.cs 11KB
PipeDecoder.cs 10KB
SecsGem.cs 10KB
Utf8JsonStreamReader.cs 10KB
NullableAttributes.cs 9KB
ReverseEndianness.cs 9KB
ReverseEndiannessHelper.cs 8KB
Form1.cs 8KB
Item.Factory.cs 7KB
SecsGem.UnitTests.cs 7KB
JsonReader.cs 7KB
Item.cs 6KB
ItemAssertions.cs 6KB
Resources.Designer.cs 6KB
HsmsHub.cs 6KB
PipeDecoder.UnitTests.cs 5KB
PipeDecoding.cs 5KB
MemoryExtensions.cs 4KB
JsonWriter.cs 4KB
JsonSerialization.cs 4KB
ComplexItemEncodeDecode.cs 4KB
SmlSerialization.cs 4KB
Item.List.cs 3KB
Item.Memory.cs 3KB
ChunkedMemory.cs 3KB
RequestResponse.cs 3KB
ExtensionHelper.cs 3KB
TreeViewItemViewModel.cs 3KB
Json.UnitTest.cs 3KB
SecsGemOptions.cs 3KB
ItemEncodeDecode.cs 3KB
PrimaryMessageWrapper.cs 3KB
Sml.UnitTest.cs 2KB
DeviceWorker.cs 2KB
Item.Encode.cs 2KB
Startup.cs 2KB
SecsMessageAssertions.cs 2KB
MessageHeader.cs 2KB
Item.LazyString.cs 2KB
SecsMessageTreeView.cs 2KB
Encoding.Extensions.cs 2KB
PipeConnection.cs 2KB
Item.String.cs 2KB
ISecsConnection.cs 2KB
SecsMessageViewModel.cs 1KB
SecsMessage.cs 1KB
SecsItemViewModel.cs 1KB
WeatherForecastController.cs 1KB
ISecsGemLogger.cs 1021B
BenchmarkConfig.cs 1003B
Error.cshtml.cs 878B
DeviceLogger.cs 825B
ServiceProvider.cs 809B
VeiwModelToTreeViewItemConverter.cs 803B
Program.cs 765B
SkipLocalsInit.cs 726B
Program.cs 724B
SecsMessageList.cs 648B
Channel.Extensions.cs 628B
ReplyExpectedToStringConverter.cs 582B
SecsMessageCollectionViewModel.cs 581B
ItemJsonConverter.cs 571B
Program.cs 496B
Item.MemoryOwner.cs 472B
ModuleInitializer.cs 470B
SecsFormat.cs 441B
EnumerableExtensions.cs 432B
Window1.xaml.cs 419B
MessageType.cs 373B
Program.cs 361B
WeatherForecast.cs 350B
SecsException.cs 338B
MessageIdGenerator.cs 308B
Program.cs 255B
Usings.cs 193B
App.xaml.cs 190B
IsExternalInit.cs 175B
ConnectionState.cs 109B
Usings.cs 46B
Error.cshtml 1KB
Secs4Net.csproj 3KB
Secs4Net.UnitTests.csproj 1KB
Secs4Net.Sml.UnitTests.csproj 1KB
Secs4Net.Json.UnitTests.csproj 1KB
Benchmarks.csproj 1KB
WebApp.Client.csproj 919B
Secs4Net.Json.csproj 727B
WebApp.Server.csproj 615B
DeviceWorkerService.csproj 578B
WpfVisualizer.csproj 573B
Secs4Net.Sml.csproj 568B
SecsDevice.csproj 444B
WebApp.Shared.csproj 213B
共 162 条
- 1
- 2
m0_37463136
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0