S7NetPlus / s7netplus

S7.NET+ -- A .NET library to connect to Siemens Step7 devices
MIT License
1.35k stars 595 forks source link

Why does including a DateTime field in a struct cause an application crash due to infinite recursion when using ReadStructAsync? #527

Open keeleycenc opened 7 months ago

keeleycenc commented 7 months ago

System.StackOverflowException when using DateTime in a struct with ReadStructAsync

I'm experiencing a System.StackOverflowException with HRESULT=0x800703E9 and the message "Exception_WasThrown" when trying to include a DateTime field in a structure in an S7.Net environment. This problem occurs because I am using structs to map directly to a database schema, allowing for direct CRUD operations upon successful mapping. The struct in question has 20 fields, some of which are date fields.

Here's the problematic field in the structure:

public System.DateTime ProductExpiryDate;

The application only functions correctly when all DateTime fields are removed from the structure. Does the ReadStructAsync method not support reading blocks of data that include Date, Time_And_Date, or LDT time types? There is no mention of this limitation in the documentation. Additionally, the documentation states that the "read struct" and "write struct" methods do not support strings, although using the S7String attribute allows strings to be read successfully, as shown below:

[S7String(S7StringType.S7String, 50)]
public string ProductSerialNumber;

However, there are no attributes available for handling time types. Could someone clarify why this is the case and whether there is a workaround for including DateTime fields in data structures read by ReadStructAsync?

And here's how I'm calling ReadStructAsync:

var equipmentProduct = await _s7Protocol.ReadStructAsync<Models.PLCstructModels.EquipmentManagement.EquipmentProduct>(App.Configuration!.S7Communication.ReadStructDBAdr + 1);

The library documentation does not mention limitations regarding DateTime data types, and while string types can be handled with the S7String attribute, there appears to be no equivalent for time types. I am aware of alternatives like ReadBytesAsync or ReadAsync, but using structs is preferable due to their direct mapping to the database schema.

gfoidl commented 7 months ago

@keeleycenc please write the issue in English, so everyone can read it (w/o the use of translators of more or less good quality), and then it's way easier to be actionable on that issue. Thanks!

keeleycenc commented 7 months ago

@gfoidl Thank you for your response and guidance! I have updated my question to English as requested and added some additional details to more accurately describe my problem and use case. Please take a look at the revised question, and I hope to receive further assistance or suggestions. Thank you again for your support and patience!

var result = await _plc.ReadStructAsync(typeof(T), db, startByteAdr);