private void InitializeProperties() { /* * Walk through the properties in this class * eg * Class UDT1 { * JPLCProperty<bool> a; * JPLCProperty<int> b; * JPLCProperty<short> c; * } */ var walker = new JPLC_BASEWalker(this, OrderedProperties); //=================================================================== // BOOL //=================================================================== walker.BooleanFound += (propertyWrapper, offsetFromStartOfWalk) => { propertyWrapper.Property = new JPLCProperty <bool>(propertyWrapper.Name, this, offsetFromStartOfWalk); }; //=================================================================== // INT //=================================================================== walker.IntegerFound += (propertyWrapper, offsetFromStartOfWalk) => { propertyWrapper.Property = new JPLCProperty <int>(propertyWrapper.Name, this, offsetFromStartOfWalk); }; //=================================================================== // REAL //=================================================================== walker.RealFound += (propertyWrapper, offsetFromStartOfWalk) => { propertyWrapper.Property = new JPLCProperty <float>(propertyWrapper.Name, this, offsetFromStartOfWalk); }; //=================================================================== // SHORT //=================================================================== walker.ShortFound += (propertyWrapper, offsetFromStartOfWalk) => { propertyWrapper.Property = new JPLCProperty <short>(propertyWrapper.Name, this, offsetFromStartOfWalk); }; //=================================================================== // BYTE //=================================================================== walker.ByteFound += (propertyWrapper, offsetFromStartOfWalk) => { propertyWrapper.Property = new JPLCProperty <byte>(propertyWrapper.Name, this, offsetFromStartOfWalk); }; //=================================================================== // DATETIME //=================================================================== walker.DateTimeFound += (propertyWrapper, offsetFromStartOfWalk) => { propertyWrapper.Property = new JPLCProperty <DateTime>(propertyWrapper.Name, this, offsetFromStartOfWalk); }; //=================================================================== // DATE //=================================================================== walker.DateFound += (propertyWrapper, offsetFromStartOfWalk) => { JPLCProperty <S7Date> date = new JPLCProperty <S7Date>(propertyWrapper.Name, this, offsetFromStartOfWalk); date.Value = new S7Date(); propertyWrapper.Property = date; }; //=================================================================== // TIME //=================================================================== walker.TimeFound += (propertyWrapper, offsetFromStartOfWalk) => { JPLCProperty <S7Time> time = new JPLCProperty <S7Time>(propertyWrapper.Name, this, offsetFromStartOfWalk); time.Value = new S7Time(); propertyWrapper.Property = time; }; //=================================================================== // UDT //=================================================================== walker.UDTFound += (propertyWrapper, offsetFromStartOfWalk) => { Type genericType = propertyWrapper.PropertyType.GetGenericArguments()[0]; var udt = Activator.CreateInstance(genericType, (int)(offsetFromStartOfWalk + this.Address)) as JPLC_BASE; // Creates the udt propertyWrapper.Property = Activator.CreateInstance(propertyWrapper.PropertyType, udt, propertyWrapper.Name, this, (int)offsetFromStartOfWalk); // Creates the PLCProperty<udt> }; //=================================================================== // STRING //=================================================================== walker.StringFound += (propertyWrapper, offsetFromStartOfWalk) => { var stringProperty = new JPLCProperty <string>(propertyWrapper.Name, this, offsetFromStartOfWalk); stringProperty.Value = ""; propertyWrapper.Property = stringProperty; }; //=================================================================== // END OF WALK //=================================================================== walker.WalkCompleted += (offsetFromStartOfWalk) => { SizeInBytes = (int)offsetFromStartOfWalk; }; walker.Walk(); }
public void ReadFromByteArray(byte[] data) { var walker = new JPLC_BASEWalker(this, OrderedProperties); //=================================================================== // BOOL //=================================================================== walker.BooleanFound += (propertyWrapper, offsetFromStartOfWalk) => { JPLCProperty <bool> JPLCProperty = propertyWrapper.Property as JPLCProperty <bool>; // int bitNumber = (int)((offsetFromStartOfWalk - (int)offsetFromStartOfWalk)*10); // (2.3 -2)*10 = 3 // int bitNumber = (int)((offsetFromStartOfWalk * 1.0 - ((int)offsetFromStartOfWalk * 1.0)) * 10.0); // (2.3 -2)*10 = 3 double truncatedOffset = Math.Truncate(offsetFromStartOfWalk); double difference = Math.Round((offsetFromStartOfWalk - truncatedOffset) * 10); int bitNumber = (int)(difference); // (2.3 -2)*10 = 3 JPLCProperty.Value = S7.GetBitAt(data, (int)offsetFromStartOfWalk, bitNumber); }; //=================================================================== // BYTE //=================================================================== walker.ByteFound += (propertyWrapper, offsetFromStartOfWalk) => { JPLCProperty <byte> JPLCProperty = propertyWrapper.Property as JPLCProperty <byte>; JPLCProperty.Value = S7.GetByteAt(data, (int)offsetFromStartOfWalk); }; //=================================================================== // INT //=================================================================== walker.IntegerFound += (propertyWrapper, offsetFromStartOfWalk) => { JPLCProperty <int> JPLCProperty = propertyWrapper.Property as JPLCProperty <int>; JPLCProperty.Value = S7.GetDIntAt(data, (int)offsetFromStartOfWalk); }; //=================================================================== // REAL //=================================================================== walker.RealFound += (propertyWrapper, offsetFromStartOfWalk) => { JPLCProperty <float> JPLCProperty = propertyWrapper.Property as JPLCProperty <float>; JPLCProperty.Value = S7.GetRealAt(data, (int)offsetFromStartOfWalk); }; //=================================================================== // SHORT //=================================================================== walker.ShortFound += (propertyWrapper, offsetFromStartOfWalk) => { JPLCProperty <short> JPLCProperty = propertyWrapper.Property as JPLCProperty <short>; JPLCProperty.Value = (short)S7.GetIntAt(data, (int)offsetFromStartOfWalk); }; //=================================================================== // DATETIME //=================================================================== walker.DateTimeFound += (propertyWrapper, offsetFromStartOfWalk) => { JPLCProperty <DateTime> JPLCProperty = propertyWrapper.Property as JPLCProperty <DateTime>; JPLCProperty.Value = S7.GetDateTimeAt(data, (int)offsetFromStartOfWalk); }; //=================================================================== // DATE //=================================================================== walker.DateFound += (propertyWrapper, offsetFromStartOfWalk) => { JPLCProperty <S7Date> JPLCProperty = propertyWrapper.Property as JPLCProperty <S7Date>; JPLCProperty.Value = new S7Date(); JPLCProperty.Value.Date = S7.GetDateAt(data, (int)offsetFromStartOfWalk); }; //=================================================================== // TIME //=================================================================== walker.TimeFound += (propertyWrapper, offsetFromStartOfWalk) => { JPLCProperty <S7Time> JPLCProperty = propertyWrapper.Property as JPLCProperty <S7Time>; JPLCProperty.Value = new S7Time(); JPLCProperty.Value.Time = S7.GetTODAt(data, (int)offsetFromStartOfWalk); }; //=================================================================== // UDT //=================================================================== walker.UDTFound += (propertyWrapper, offsetFromStartOfWalk) => { // Console.WriteLine(propertyWrapper.Property); var udt = (propertyWrapper.PropertyType.GetProperty("Value").GetValue(propertyWrapper.Property, null)) as JPLC_BASE; byte[] extractedByteArray = data.Skip((int)offsetFromStartOfWalk).Take(udt.SizeInBytes).ToArray(); udt.ReadFromByteArray(extractedByteArray); }; //=================================================================== // STRING //=================================================================== walker.StringFound += (propertyWrapper, offsetFromStartOfWalk) => { JPLCProperty <string> JPLCProperty = propertyWrapper.Property as JPLCProperty <string>; JPLCProperty.Value = S7.GetStringAt(data, (int)offsetFromStartOfWalk); }; walker.Walk(); }
public byte[] WriteToByteArray() { byte[] data = new byte[SizeInBytes]; var walker = new JPLC_BASEWalker(this, OrderedProperties); //=================================================================== // BOOL //=================================================================== walker.BooleanFound += (propertyWrapper, offsetFromStartOfWalk) => { JPLCProperty <bool> JPLCProperty = propertyWrapper.Property as JPLCProperty <bool>; double truncatedOffset = Math.Truncate(offsetFromStartOfWalk); double difference = Math.Round((offsetFromStartOfWalk - truncatedOffset) * 10); int bitNumber = (int)(difference); // (2.3 -2)*10 = 3 S7.SetBitAt(ref data, (int)(offsetFromStartOfWalk), bitNumber, JPLCProperty.Value); }; //=================================================================== // INT //=================================================================== walker.IntegerFound += (propertyWrapper, offsetFromStartOfWalk) => { JPLCProperty <int> JPLCProperty = propertyWrapper.Property as JPLCProperty <int>; S7.SetDIntAt(data, (int)offsetFromStartOfWalk, JPLCProperty.Value); }; //=================================================================== // REAL //=================================================================== walker.RealFound += (propertyWrapper, offsetFromStartOfWalk) => { JPLCProperty <float> JPLCProperty = propertyWrapper.Property as JPLCProperty <float>; S7.SetRealAt(data, (int)offsetFromStartOfWalk, JPLCProperty.Value); }; //=================================================================== // SHORT //=================================================================== walker.ShortFound += (propertyWrapper, offsetFromStartOfWalk) => { JPLCProperty <short> JPLCProperty = propertyWrapper.Property as JPLCProperty <short>; S7.SetIntAt(data, (int)offsetFromStartOfWalk, JPLCProperty.Value); }; //=================================================================== // BYTE //=================================================================== walker.ByteFound += (propertyWrapper, offsetFromStartOfWalk) => { JPLCProperty <byte> JPLCProperty = propertyWrapper.Property as JPLCProperty <byte>; S7.SetByteAt(data, (int)offsetFromStartOfWalk, JPLCProperty.Value); }; //=================================================================== // DATETIME //=================================================================== walker.DateTimeFound += (propertyWrapper, offsetFromStartOfWalk) => { JPLCProperty <DateTime> JPLCProperty = propertyWrapper.Property as JPLCProperty <DateTime>; S7.SetDateTimeAt(data, (int)offsetFromStartOfWalk, JPLCProperty.Value); }; //=================================================================== // DATE //=================================================================== walker.DateFound += (propertyWrapper, offsetFromStartOfWalk) => { JPLCProperty <S7Date> JPLCProperty = propertyWrapper.Property as JPLCProperty <S7Date>; S7.SetDateAt(data, (int)offsetFromStartOfWalk, JPLCProperty.Value.Date); }; //=================================================================== // TIME //=================================================================== walker.TimeFound += (propertyWrapper, offsetFromStartOfWalk) => { JPLCProperty <S7Time> JPLCProperty = propertyWrapper.Property as JPLCProperty <S7Time>; S7.SetTODAt(data, (int)offsetFromStartOfWalk, JPLCProperty.Value.Time); }; //=================================================================== // UDT //=================================================================== walker.UDTFound += (propertyWrapper, offsetFromStartOfWalk) => { var udt = (propertyWrapper.PropertyType.GetProperty("Value").GetValue(propertyWrapper.Property, null)) as JPLC_BASE; byte[] udtByteArray = udt.WriteToByteArray(); // Add byte array to master byte array for (int i = 0; i < udtByteArray.Length; i++) { data[(int)offsetFromStartOfWalk + i] = udtByteArray[i]; } }; //=================================================================== // STRING //=================================================================== walker.StringFound += (propertyWrapper, offsetFromStartOfWalk) => { JPLCProperty <string> JPLCProperty = propertyWrapper.Property as JPLCProperty <string>; S7.SetStringAt(data, (int)offsetFromStartOfWalk, propertyWrapper.StringAttribute.PLCString, JPLCProperty.Value); }; walker.Walk(); return(data); }