Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reading and writing custom types #22

Open
tolgacakir opened this issue Feb 9, 2021 · 3 comments
Open

Reading and writing custom types #22

tolgacakir opened this issue Feb 9, 2021 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@tolgacakir
Copy link
Contributor

tolgacakir commented Feb 9, 2021

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

@tolgacakir tolgacakir changed the title ead and write custom types Reading and writing custom types Feb 9, 2021
@fbarresi fbarresi added the enhancement New feature or request label Feb 9, 2021
@fbarresi fbarresi self-assigned this Feb 9, 2021
@tolgacakir
Copy link
Contributor Author

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
Copy link
Owner

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?

  • Sharp7 handles with bytes: Any application that uses struct and takes care of this special handling before write and after read already works at glance right now.
  • Alternative: Interfaces
    Sharp7 could provide an Interface like IParsable with methods for ToBytes and FromBytes. But I think that only moves the problem into every Type definition and supports code duplication end errors.

The Question

  • Should a low level communication driver (like Sharp7) offer functions and methods for the right handling of Structure?
    I don't think so. 😞

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
Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants