You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello Dears,
I've just wrote a little code to handle the Date Type inside the Siemens PLC (tested on Siemens 1515).
Unfortunatly, I've used a DateOnly struct, that's available only from .NET 6+.
internalstaticclassDateOnly{publicstaticreadonly System.DateTime SpecMinimumDateTime=new System.DateTime(1990,1,1);publicstaticreadonly System.DateTime SpecMaximumDateTime=new System.DateTime(2169,06,06);publicconstintDEFAULT_ADD_YEAR=1989;internalstatic System.DateOnly FromByteArray(byte[]bytes){Int16dayNumber=(Int16)((bytes[0]<<8)| bytes[1]);
System.DateOnly dt= System.DateOnly.FromDayNumber(dayNumber).AddYears(DEFAULT_ADD_YEAR);returndt;}internalstaticbyte[]ToByteArray(System.DateOnly dateOnly){// Subtract the default year to return the date to the original rangeintyearAdjusted= dateOnly.Year -DEFAULT_ADD_YEAR;// the date within the permissible limitsif(yearAdjusted<0||yearAdjusted>9999){thrownew ArgumentOutOfRangeException("The year of the date is out of the allowed range after subtracting DEFAULT_ADD_YEAR.");}// Create a new DateOnly object with the adjusted year and get the day number// this will be the actual object that needs to be handled
System.DateOnly adjustedDateOnly=new System.DateOnly(yearAdjusted, dateOnly.Month, dateOnly.Day);Int16dayNumber=(Int16)adjustedDateOnly.DayNumber;// Converts the number of the day to a byte arraybyte[]bytes=newbyte[2];
bytes[0]=(byte)(dayNumber>>8);
bytes[1]=(byte)(dayNumber&255);returnbytes;}internalstatic System.DateOnly[]ToArray(byte[]bytes){intcnt= bytes.Length /2;
System.DateOnly[]result=new System.DateOnly[cnt];for(inti=0;i<cnt;i++){
result[i]= FromByteArray(newArraySegment<byte>(bytes,i*2,2).Array);}returnresult;}}
The text was updated successfully, but these errors were encountered:
Hello Dears,
I've just wrote a little code to handle the Date Type inside the Siemens PLC (tested on Siemens 1515).
Unfortunatly, I've used a DateOnly struct, that's available only from .NET 6+.
The text was updated successfully, but these errors were encountered: