Sharp7
Sharp7 copied to clipboard
Reading and writing custom types
Reading and writing custom types (struct or class) required like below;
s7Client.Read<T>(...)
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; } = "";
}
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 likeIParsable
with methods forToBytes
andFromBytes
. 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.
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.