fbarresi / Sharp7

Nuget package for Sharp7
MIT License
207 stars 73 forks source link

Reading and writing custom types #22

Open tolgacakir opened 3 years ago

tolgacakir commented 3 years ago

Reading and writing custom types (struct or class) required like below; s7Client.Read<T>(...)

tolgacakir commented 3 years ago

This feature should support types that include arrays. For example;

s7Client.Read<PlcRequestDataBlock>(...); //generic reading operation
//the c# entity model of the data block
public class PlcRequestDataBlock
{
    public byte Var0 { get; set; }
    public PlcRequest[] PlcRequests { get; set; } = new PlcRequest[3];
}
public class PlcRequest
{
    public byte Var1 { get; set; }
    public byte Var2 { get; set; }
    public string Var3 { get; set; } = "";
}

Ekran Alıntısı

fbarresi commented 3 years ago

Hi!

Thank you for your contributions and for this feature request.

We may be would start a discussion based on this topic so I prepared this short post.

Please let me know what do you think about it. Feel free to start a new discussion of go deeper of you have any questions.

Please, don't let me reinvent the wheel

The Problem of transmit complex object from a system to another is already solved for communications with PLC. Both, C# and S7 (in out case) have the capability to define structures (struct).

Why Struct(s)?

Because they can be parsed to byte arrays in a deterministic way, just like S7 also does. Struct in C# also support properties and methods, but only fields are mapped to buffer.

The Problem

S7 works with BigEndian variables, C# and almost the rest of the world works with LittleEndian. So after converting to bytes every, every single field have to be swapped in a recursive way.

So what?

The Question

A feasible Path

I think a good solution would be to offer this functionality into an extension of Sharp7 like Sharp7.Rx or your own struct-swiss-knife.

tolgacakir commented 3 years ago

Hi,

Thanks a lot for your interest.

I've just tried to read data with Sharp7.Rx. The reactive approach and the S7-variable-name-based mapping looks very usable.

But the same issue comes up: the reading/writing custom types (structs or objects). I understand you don't want to add this feature to Sharp7 cause of its compactness. Sharp7.Rx more suitable for this feature.

I think the mapping between the S7 variables and the C# variables should be like Entity Framework data annotations or with like the IEntityTypeConfiguration so the data that will be read/write need to configure.

About the subject, I'm ready to contribute, accordingly the issues that you open at the Sharp7.Rx repository.