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{publicstaticreadonlySystem.DateTimeSpecMinimumDateTime=newSystem.DateTime(1990,1,1);publicstaticreadonlySystem.DateTimeSpecMaximumDateTime=newSystem.DateTime(2169,06,06);publicconstintDEFAULT_ADD_YEAR=1989;internalstaticSystem.DateOnlyFromByteArray(byte[]bytes){Int16dayNumber=(Int16)((bytes[0]<<8)|bytes[1]);System.DateOnlydt=System.DateOnly.FromDayNumber(dayNumber).AddYears(DEFAULT_ADD_YEAR);returndt;}internalstaticbyte[]ToByteArray(System.DateOnlydateOnly){// 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){thrownewArgumentOutOfRangeException("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 handledSystem.DateOnlyadjustedDateOnly=newSystem.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;}internalstaticSystem.DateOnly[]ToArray(byte[]bytes){intcnt=bytes.Length/2;System.DateOnly[]result=newSystem.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: