예제 #1
0
파일: Form1.cs 프로젝트: yuanhuihai/CSharp
        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;
            }
            }
        }
예제 #2
0
 public void TestGetDTLAt(byte[] buffer)
 {
     S7.GetDTLAt(buffer, 0).ShouldBe(new DateTime(2570, 10, 10, 10, 10, 0));
 }
예제 #3
0
파일: Program.cs 프로젝트: atpaw/EvaServer
        static void Main(string[] args)
        {
            int       delay           = 10000;
            int       countDisconnect = 0;
            int       index           = 0;
            const int START_INDEX     = 0;


            // Console.BackgroundColor = ConsoleColor.White;
            // Console.Clear();

            while (true)
            {
                if (s7Connect() == 0)
                {
                    Console.WriteLine("Status servera: " + "Nawiązano połączenie\n");
                    int readResult = _s7Plc.DBRead(43, 0, db43Buffer.Length, db43Buffer);
                    Console.WriteLine("Ilość TimeOut: " + countDisconnect + "\n");
                    DateTime s7SaveDay          = S7.GetDTLAt(db43Buffer, 2); //("DTL#yyyy-MM-dd-HH:mm:ss.fffffff");
                    string   s7Data             = s7SaveDay.ToString("dd.MM.yyyy");
                    string   s7FirstTimeRunning = s7SaveDay.ToString("HH:mm:ss");
                    Console.WriteLine("Moment zerowania parametrów: " + s7SaveDay);
                    DateTime s7TimeWorkDay    = S7.GetTODAt(db43Buffer, 14); //("DTL#yyyy-MM-dd-HH:mm:ss.fffffff");
                    string   CzasPracyMaszyny = s7TimeWorkDay.ToString("HH:mm:ss");
                    Console.WriteLine("Czas pracy maszyny: " + CzasPracyMaszyny);

                    DateTime s7TimeRunning = S7.GetTODAt(db43Buffer, 24);  //("DTL#yyyy-MM-dd-HH:mm:ss.fffffff");
                    string   TimeEffect    = s7TimeRunning.ToString("HH:mm:ss");
                    Console.WriteLine("Efektywny czas pracy: " + TimeEffect);

                    DateTime s7LongTimeNextElement = S7.GetTODAt(db43Buffer, 18);  //("DTL#yyyy-MM-dd-HH:mm:ss.fffffff");
                    string   TimeElements          = s7LongTimeNextElement.ToString("HH:mm:ss");
                    Console.WriteLine("Najdłuższy czas między elementami: " + TimeElements);

                    uint s7CounterElements = S7.GetUIntAt(db43Buffer, 0);
                    Console.WriteLine("Dobowa ilość elementów: " + s7CounterElements);
                    bool s7DataOk = S7.GetBitAt(db43Buffer, 22, 0);
                    System.Threading.Thread.Sleep(1000);
                    if (s7DataOk == true)
                    {
                        //   updateBase(index,s7Data,s7FirstTimeRunning,CzasPracyMaszyny,TimeEffect,TimeElements, s7CounterElements);
                        int writeResult = _s7Plc.DBWrite(43, 0, db43WBuffer.Length, db43WBuffer);
                        index = (Wczytajbaze());
                        index++;
                        Console.WriteLine("Update bazy danych... ");
                        updateBase(index, s7Data, s7FirstTimeRunning, TimeEffect, CzasPracyMaszyny, TimeElements, s7CounterElements);
                        // public static void SetBitAt(ref byte[] Buffer, int Pos, int Bit, bool Value)
                        S7.SetBitAt(ref db43WBuffer, 22, 0, false);
                        Console.WriteLine("Wartość Index: " + index);
                    }
                }
                else
                {
                    Console.WriteLine("Status servera: " + "Brak połączenia\n");
                    countDisconnect++;
                    Console.WriteLine("Ilość TimeOut: " + countDisconnect);
                }
                _s7Plc.Disconnect();
                System.Threading.Thread.Sleep(delay);
                Console.Clear();
            }
            while (Console.ReadKey().Key != ConsoleKey.Enter)
            {
            }
        }
예제 #4
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);
        }