示例#1
0
 public void Parse(byte[] buffer, int start)
 {
     plcDateTime         = S7.GetDateTimeAt(buffer, start); start = start + 8;
     logEntryNumber      = S7.GetDIntAt(buffer, start);  start = start + 4;
     SRMNumber           = S7.GetIntAt(buffer, start);        start = start + 2;
     logEntryType        = S7.GetStringAt(buffer, start);        start = start + 52;
     logEntryDescription = S7.GetStringAt(buffer, start); start = start + 202;
     functionName        = S7.GetStringAt(buffer, start);       start = start + 52;
     operationMode       = S7.GetStringAt(buffer, start);       start = start + 52;
     faultOrWarningState = (FaultOrWarningState)S7.GetIntAt(buffer, start);    start = start + 2;
     XPosition_mm        = S7.GetDIntAt(buffer, start); start = start + 4;
     YPosition_mm        = S7.GetDIntAt(buffer, start); start = start + 4;
     Z1Position_mm       = S7.GetIntAt(buffer, start); start = start + 2;
     Z2Position_mm       = S7.GetIntAt(buffer, start); start = start + 2;
     Z3Position_mm       = S7.GetIntAt(buffer, start); start = start + 2;
     Z4Position_mm       = S7.GetIntAt(buffer, start); start = start + 2;
     XTarget_mm          = S7.GetDIntAt(buffer, start); start = start + 4;
     YTarget_mm          = S7.GetDIntAt(buffer, start); start = start + 4;
     Z1Target_mm         = S7.GetIntAt(buffer, start); start = start + 2;
     Z2Target_mm         = S7.GetIntAt(buffer, start); start = start + 2;
     Z3Target_mm         = S7.GetIntAt(buffer, start); start = start + 2;
     Z4Target_mm         = S7.GetIntAt(buffer, start); start = start + 2;
     XSpeed_mmps         = S7.GetIntAt(buffer, start); start = start + 2;
     YSpeed_mmps         = S7.GetIntAt(buffer, start); start = start + 2;
     Z1Speed_mmps        = S7.GetIntAt(buffer, start); start = start + 2;
     Z2Speed_mmps        = S7.GetIntAt(buffer, start); start = start + 2;
     Z3Speed_mmps        = S7.GetIntAt(buffer, start); start = start + 2;
     Z4Speed_mmps        = S7.GetIntAt(buffer, start); start = start + 2;
 }
        private bool ReadValue()
        {
            var client = _connection?.PlcClient;

            if (client == null)
            {
                return(false);
            }

            var buffer = new byte[Math.Max(64, _info.ByteCount + 2)];
            var result = client.DBRead(_info.DbNumber, _info.Offset, _info.ByteCount, buffer);

            if (result != 0)
            {
                TronTrace.TraceError($"PlcItem.DBRead({_info.DbNumber}, {_info.Offset}) failed - {result} {PlcResult.GetResultText(result)}");
                return(false);
            }

            switch (_info.PlcDataType)
            {
            case PlcDataTypes.X:
                _value = S7.GetBitAt(buffer, 0, _info.Bit) ? 1 : 0;
                break;

            case PlcDataTypes.B:
                _value = S7.GetByteAt(buffer, 0);
                break;

            case PlcDataTypes.INT:
                _value = S7.GetIntAt(buffer, 0);
                break;

            case PlcDataTypes.DINT:
                _value = S7.GetDIntAt(buffer, 0);
                break;

            case PlcDataTypes.DT:
                _value = S7.GetDateTimeAt(buffer, 0);
                break;

            case PlcDataTypes.STRING:
                _value = S7.GetStringAt(buffer, 0);
                break;
            }

            if (!_value.Equals(_oldValue))
            {
                _oldValue = _value;
                ValueChanged?.Invoke(this);
            }
            return(true);
        }
示例#3
0
        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;
            }
            }
        }
示例#4
0
        public void TestGetDateTimeAt(byte[] buffer)
        {
            var time = new DateTime(2010, 11, 12, 13, 14, 15);

            S7.GetDateTimeAt(buffer, 0).ShouldBe(time);
        }
示例#5
0
        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);
        }
示例#6
0
        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();
        }