internal static object GetS7Value(Tag tagItem, byte[] buffer) { object returnValue; if (tagItem.DataType == "Bit") { returnValue = S7.GetBitAt(buffer, tagItem.Position, 0); } else if (tagItem.DataType == "Word") { returnValue = S7.GetWordAt(buffer, 0); } else if (tagItem.DataType == "Short") { returnValue = S7.GetShortAt(buffer, 0); } else if (tagItem.DataType == "DWord") { returnValue = S7.GetDWordAt(buffer, 0); } else if (tagItem.DataType == "DInt") { returnValue = S7.GetDIntAt(buffer, 0); } else if (tagItem.DataType == "Float") { returnValue = S7.GetFloatAt(buffer, 0); } else if (tagItem.DataType == "String") { returnValue = S7.GetStringAt(buffer, 0, buffer.Length); } else if (tagItem.DataType == "PrintableString") { returnValue = S7.GetPrintableStringAt(buffer, 0, buffer.Length); } else if (tagItem.DataType == "Date") { returnValue = S7.GetDateAt(buffer, 0); } else { returnValue = S7.GetStringAt(buffer, 0, buffer.Length); } return(returnValue); }
private void FieldBtn_Click(object sender, EventArgs e) { /* * 0 Byte 8 Bit Word (All) * 1 Word 16 Bit Word (All) * 2 DWord 32 Bit Word (All) * 3 LWord 64 Bit Word (S71500) * 4 USint 8 Bit Unsigned Integer (S71200/1500) * 5 UInt 16 Bit Unsigned Integer (S71200/1500) * 6 UDInt 32 Bit Unsigned Integer (S71200/1500) * 7 ULint 64 Bit Unsigned Integer (S71500) * 8 Sint 8 Bit Signed Integer (S71200/1500) * 9 Int 16 Bit Signed Integer (All) * 10 DInt 32 Bit Signed Integer (S71200/1500) * 11 LInt 64 Bit Signed Integer (S71500) * 12 Real 32 Bit Floating point (All) * 13 LReal 64 Bit Floating point (S71200/1500) * 14 Time 32 Bit Time elapsed ms (All) * 15 LTime 64 Bit Time Elapsed ns (S71500) * 16 Date 16 Bit days from 1990/1/1 (All) * 17 TOD 32 Bit ms elapsed from midnight (All) * 18 DT 8 Byte Date and Time (All) * 19 LTOD 64 Bit time of day (ns) (S71500) * 20 DTL 12 Byte Date and Time Long (S71200/1500) * 21 LDT 64 Bit ns elapsed from 1970/1/1 (S71500) */ int Pos = System.Convert.ToInt32(TxtOffset.Text); switch (CBType.SelectedIndex) { case 0: { TxtValue.Text = "16#" + System.Convert.ToString(Buffer[Pos], 16).ToUpper(); break; } case 1: { UInt16 Word = S7.GetWordAt(Buffer, Pos); TxtValue.Text = "16#" + System.Convert.ToString(Word, 16).ToUpper(); break; } case 2: { UInt32 DWord = S7.GetDWordAt(Buffer, Pos); TxtValue.Text = "16#" + System.Convert.ToString(DWord, 16).ToUpper(); break; } case 3: { UInt64 LWord = S7.GetLWordAt(Buffer, Pos); TxtValue.Text = "16#" + System.Convert.ToString((Int64)LWord, 16).ToUpper(); // <-- Convert.ToString does not handle UInt64 break; } case 4: { UInt16 USInt = S7.GetUSIntAt(Buffer, Pos); TxtValue.Text = System.Convert.ToString(USInt); break; } case 5: { UInt16 UInt = S7.GetUIntAt(Buffer, Pos); TxtValue.Text = System.Convert.ToString(UInt); break; } case 6: { UInt32 UDInt = S7.GetDWordAt(Buffer, Pos); TxtValue.Text = System.Convert.ToString(UDInt); break; } case 7: { UInt64 ULInt = S7.GetLWordAt(Buffer, Pos); TxtValue.Text = System.Convert.ToString(ULInt); break; } case 8: { int SInt = S7.GetSIntAt(Buffer, Pos); TxtValue.Text = System.Convert.ToString(SInt); break; } case 9: { int S7Int = S7.GetIntAt(Buffer, Pos); TxtValue.Text = System.Convert.ToString(S7Int); break; } case 10: { int DInt = S7.GetDIntAt(Buffer, Pos); TxtValue.Text = System.Convert.ToString(DInt); break; } case 11: { Int64 LInt = S7.GetLIntAt(Buffer, Pos); TxtValue.Text = System.Convert.ToString(LInt); break; } case 12: { Single S7Real = S7.GetRealAt(Buffer, Pos); TxtValue.Text = System.Convert.ToString(S7Real); break; } case 13: { Double S7LReal = S7.GetLRealAt(Buffer, Pos); TxtValue.Text = System.Convert.ToString(S7LReal); break; } case 14: { Int32 TimeElapsed = S7.GetDIntAt(Buffer, Pos); // TIME type is a 32 signed number of ms elapsed // Can be added to a DateTime or used as Value. TxtValue.Text = "T#" + System.Convert.ToString(TimeElapsed) + "MS"; break; } case 15: { Int64 TimeElapsed = S7.GetLIntAt(Buffer, Pos); // LTIME type is a 64 signed number of ns elapsed // Can be added (after a conversion) to a DateTime or used as Value. TxtValue.Text = "LT#" + System.Convert.ToString(TimeElapsed) + "NS"; break; } case 16: { DateTime DATE = S7.GetDateAt(Buffer, Pos); TxtValue.Text = DATE.ToString("D#yyyy-MM-dd"); break; } case 17: { DateTime TOD = S7.GetTODAt(Buffer, Pos); TxtValue.Text = TOD.ToString("TOD#HH:mm:ss.fff"); break; } case 18: { DateTime DT = S7.GetDateTimeAt(Buffer, Pos); TxtValue.Text = DT.ToString("DT#yyyy-MM-dd-HH:mm:ss.fff"); break; } case 19: { DateTime LTOD = S7.GetLTODAt(Buffer, Pos); TxtValue.Text = LTOD.ToString("LTOD#HH:mm:ss.fffffff"); break; } case 20: { DateTime DTL = S7.GetDTLAt(Buffer, Pos); TxtValue.Text = DTL.ToString("DTL#yyyy-MM-dd-HH:mm:ss.fffffff"); break; } case 21: { DateTime LDT = S7.GetLDTAt(Buffer, Pos); TxtValue.Text = LDT.ToString("LDT#yyyy-MM-dd-HH:mm:ss.fffffff"); break; } } }
public void TestGetDateAt(byte[] buffer) { var date = new DateTime(1990, 1, 3); S7.GetDateAt(buffer, 0).ShouldBe(date); }
private bool GetValue(int S7Type, int Pos, byte[] Buffer, ref string TxtValue) { /* * 0 Byte 8 Bit Word (All) * 1 Word 16 Bit Word (All) * 2 DWord 32 Bit Word (All) * 3 LWord 64 Bit Word (S71500) * 4 USint 8 Bit Unsigned Integer (S71200/1500) * 5 UInt 16 Bit Unsigned Integer (S71200/1500) * 6 UDInt 32 Bit Unsigned Integer (S71200/1500) * 7 ULint 64 Bit Unsigned Integer (S71500) * 8 Sint 8 Bit Signed Integer (S71200/1500) * 9 Int 16 Bit Signed Integer (All) * 10 DInt 32 Bit Signed Integer (S71200/1500) * 11 LInt 64 Bit Signed Integer (S71500) * 12 Real 32 Bit Floating point (All) * 13 LReal 64 Bit Floating point (S71200/1500) * 14 Time 32 Bit Time elapsed ms (All) * 15 LTime 64 Bit Time Elapsed ns (S71500) * 16 Date 16 Bit days from 1990/1/1 (All) * 17 TOD 32 Bit ms elapsed from midnight (All) * 18 DT 8 Byte Date and Time (All) * 19 LTOD 64 Bit time of day (ns) (S71500) * 20 DTL 12 Byte Date and Time Long (S71200/1500) * 21 LDT 64 Bit ns elapsed from 1970/1/1 (S71500) * 22 Bit */ //When WordLen = S7WLBit the Offset(Start) must be expressed in bits.Ex.The Start for DB4.DBX 10.3 is (10 * 8) + 3 = 83. switch (S7Type) { case 0: { TxtValue = System.Convert.ToString(Buffer[Pos], 10).ToUpper(); break; } case 1: { UInt16 Word = S7.GetWordAt(Buffer, Pos); TxtValue = System.Convert.ToString(Word, 10).ToUpper(); break; } case 2: { UInt32 DWord = S7.GetDWordAt(Buffer, Pos); TxtValue = System.Convert.ToString(DWord, 10).ToUpper(); break; } case 3: { UInt64 LWord = S7.GetLWordAt(Buffer, Pos); TxtValue = System.Convert.ToString((Int64)LWord, 10).ToUpper(); // <-- Convert.ToString does not handle UInt64 break; } case 4: { UInt16 USInt = S7.GetUSIntAt(Buffer, Pos); TxtValue = System.Convert.ToString(USInt); break; } case 5: { UInt16 UInt = S7.GetUIntAt(Buffer, Pos); TxtValue = System.Convert.ToString(UInt); break; } case 6: { UInt32 UDInt = S7.GetDWordAt(Buffer, Pos); TxtValue = System.Convert.ToString(UDInt); break; } case 7: { UInt64 ULInt = S7.GetLWordAt(Buffer, Pos); TxtValue = System.Convert.ToString(ULInt); break; } case 8: { int SInt = S7.GetSIntAt(Buffer, Pos); TxtValue = System.Convert.ToString(SInt); break; } case 9: { int S7Int = S7.GetIntAt(Buffer, Pos); TxtValue = System.Convert.ToString(S7Int); break; } case 10: { int DInt = S7.GetDIntAt(Buffer, Pos); TxtValue = System.Convert.ToString(DInt); break; } case 11: { Int64 LInt = S7.GetLIntAt(Buffer, Pos); TxtValue = System.Convert.ToString(LInt); break; } case 12: { Single S7Real = S7.GetRealAt(Buffer, Pos); TxtValue = System.Convert.ToString(S7Real); break; } case 13: { Double S7LReal = S7.GetLRealAt(Buffer, Pos); TxtValue = System.Convert.ToString(S7LReal); break; } case 14: { Int32 TimeElapsed = S7.GetDIntAt(Buffer, Pos); // TIME type is a 32 signed number of ms elapsed // Can be added to a DateTime or used as Value. TxtValue = System.Convert.ToString(TimeElapsed) + "MS"; break; } case 15: { Int64 TimeElapsed = S7.GetLIntAt(Buffer, Pos); // LTIME type is a 64 signed number of ns elapsed // Can be added (after a conversion) to a DateTime or used as Value. TxtValue = System.Convert.ToString(TimeElapsed) + "NS"; break; } case 16: { DateTime DATE = S7.GetDateAt(Buffer, Pos); TxtValue = DATE.ToString("D#yyyy-MM-dd"); break; } case 17: { DateTime TOD = S7.GetTODAt(Buffer, Pos); TxtValue = TOD.ToString("TOD#HH:mm:ss.fff"); break; } case 18: { DateTime DT = S7.GetDateTimeAt(Buffer, Pos); TxtValue = DT.ToString("DT#yyyy-MM-dd-HH:mm:ss.fff"); break; } case 19: { DateTime LTOD = S7.GetLTODAt(Buffer, Pos); TxtValue = LTOD.ToString("LTOD#HH:mm:ss.fffffff"); break; } case 20: { DateTime DTL = S7.GetDTLAt(Buffer, Pos); TxtValue = DTL.ToString("DTL#yyyy-MM-dd-HH:mm:ss.fffffff"); break; } case 21: { DateTime LDT = S7.GetLDTAt(Buffer, Pos); TxtValue = LDT.ToString("LDT#yyyy-MM-dd-HH:mm:ss.fffffff"); break; } case 22: { bool bit = S7.GetBitAt(Buffer, Pos, 0); TxtValue = bit.ToString(); break; } default: return(false); } return(true); }
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(); }