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

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

Open
keeleycenc opened this issue Apr 22, 2024 · 2 comments

Comments

@keeleycenc
Copy link

keeleycenc commented Apr 22, 2024

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

gfoidl commented Apr 22, 2024

@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 keeleycenc changed the title 为什么结构体包含DateTime字段,使用ReadStructAsync会出现无线递归导致的应用程序崩溃? Why does including a DateTime field in a struct cause an application crash due to infinite recursion when using ReadStructAsync? Apr 22, 2024
@keeleycenc
Copy link
Author

@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);

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

No branches or pull requests

2 participants