コード例 #1
0
        private static void DownloadNcFile(string ipAddress = "192.168.214.1")
        {
            using (var con = new PLCConnection("DownloadNcFile"))
            {
                con.Configuration.CpuIP   = ipAddress;
                con.Configuration.CpuSlot = 4;
                con.Connect();

                try
                {
                    if (!con.Connected)
                    {
                        con.Connect();
                    }

                    string s = "Hallo NC";

                    string NcPath = "/_N_MPF_DIR/_N_MY_TEST_MPF";
                    con.DownloadToNC(NcPath, null, s);
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debugger.Break();
                }
            }
        }
コード例 #2
0
        private static void UploadNcFile(string ipAddress = "192.168.214.1")
        {
            using (var con = new PLCConnection("UploadNcFile"))
            {
                con.Configuration.CpuIP   = ipAddress;
                con.Configuration.CpuSlot = 4;
                con.Connect();

                try
                {
                    if (!con.Connected)
                    {
                        con.Connect();
                    }

                    bool   F_XFER    = true; // false for system ini files
                    string NcPath    = "/_N_CST_DIR/_N_PROG_EVENT_SPF";
                    string sEditFile = con.UploadNcFile(NcPath, F_XFER);
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debugger.Break();
                }
            }
        }
コード例 #3
0
        private void cmdLoadBlockList_Click(object sender, RoutedEventArgs e)
        {
            myConn.Connect();
            var blks = myConn.PLCListBlocks(PLCBlockType.AllEditableBlocks);

            lstBlocks.ItemsSource = blks;
            myConn.Disconnect();
        }
コード例 #4
0
        private void button4_Click(object sender, EventArgs e)
        {
            myConn = new PLCConnection("SimpleCSharpDemonstrationConnection");
            myConn.Connect();

            //var _tags = new List<PLCTag>();
            //var j = 0;
            //for (var i = 0; i < 96; i++)
            //{
            //    Console.WriteLine("DB1.DBD" + j.ToString(CultureInfo.InvariantCulture));
            //    _tags.Add(new PLCTag("DB1.DBD" + j.ToString(CultureInfo.InvariantCulture))
            //        {
            //            TagDataType = TagDataType.Float
            //        });
            //    j += 4;
            //}
            //myConn.ReadValues(_tags, false);

            var tag = new PLCTag();
            tag.TagDataType = TagDataType.Word;
            tag.SymbolicAccessKey = "8a0e000124134d054000000a";
            myConn.ReadValue(tag);
            /*tag.Controlvalue = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 88, 1, 2, 3, 4, 5, 6, 7, 8, 9, 77 };
            myConn.WriteValue(tag);
            var db = myConn.PLCGetBlockInMC7("DB99");
            MessageBox.Show("DB:" + Encoding.ASCII.GetString(db));
            myConn.PLCPutBlockFromMC7toPLC("DB98", db);*/
        }
コード例 #5
0
        private void button4_Click(object sender, EventArgs e)
        {
            myConn = new PLCConnection("SimpleCSharpDemonstrationConnection");
            myConn.Connect();


            //var _tags = new List<PLCTag>();
            //var j = 0;
            //for (var i = 0; i < 96; i++)
            //{
            //    Console.WriteLine("DB1.DBD" + j.ToString(CultureInfo.InvariantCulture));
            //    _tags.Add(new PLCTag("DB1.DBD" + j.ToString(CultureInfo.InvariantCulture))
            //        {
            //            TagDataType = TagDataType.Float
            //        });
            //    j += 4;
            //}
            //myConn.ReadValues(_tags, false);

            var tag = new PLCTag();

            tag.TagDataType       = TagDataType.Word;
            tag.SymbolicAccessKey = "8a0e000124134d054000000a";
            myConn.ReadValue(tag);

            /*tag.Controlvalue = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 88, 1, 2, 3, 4, 5, 6, 7, 8, 9, 77 };
             * myConn.WriteValue(tag);
             * var db = myConn.PLCGetBlockInMC7("DB99");
             * MessageBox.Show("DB:" + Encoding.ASCII.GetString(db));
             * myConn.PLCPutBlockFromMC7toPLC("DB98", db);*/
        }
コード例 #6
0
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (_myconn == null)
            {
                _myconn = new PLCConnection(_connname);
                _myconn.Connect();

                foreach (string itm in _myconn.PLCListBlocks(PLCBlockType.AllEditableBlocks))
                {
                    BlockList.Items.Add(itm);
                }
            }
            else
            {
                if (dispatcherTimer != null)
                {
                    dispatcherTimer.Stop();
                }
                if (myDiag != null)
                {
                    myDiag.Close();
                    myDiag.RemoveDiagnosticData();
                    textEditor.Text = myBlock.ToString();
                }
                _myconn.Dispose();
                _myconn = null;
                myDiag  = null;
                BlockList.Items.Clear();
                blockName.Content  = "FCxx";
                Upload.IsEnabled   = false;
                Optimize.IsEnabled = false;
                Diag.IsEnabled     = false;
            }
        }
コード例 #7
0
        private void ReEstablishConnectionsThreadProc(object config)
        {
            try
            {
                LibNoDaveConfig connectionConfig = config as LibNoDaveConfig;
                while (true)
                {
                    if (ConnectionList.ContainsKey(connectionConfig))
                    {
                        PLCConnection plcConn = ConnectionList[connectionConfig] as PLCConnection;
                        if (plcConn != null && !plcConn.Connected && ((LibNoDaveConfig)connectionConfig).StayConnected)
                        {
                            try
                            {
                                plcConn.Connect();
                                Logging.LogText("Connection: " + connectionConfig.Name + " connected", Logging.LogLevel.Information);
                            }
                            catch (ThreadAbortException ex)
                            {
                                throw ex;
                            }
                            catch (Exception ex)
                            {
                                Logging.LogText("Connection: " + connectionConfig.Name, ex, Logging.LogLevel.Warning);
                            }
                        }
                    }

                    Thread.Sleep(connectionConfig.ReconnectInterval);
                }
            }
            catch (ThreadAbortException)
            {
            }
        }
コード例 #8
0
        private void synchronizePLCTimes(object tmp)
        {
            foreach (ConnectionConfig connectionConfig in akConfig.Connections)
            {
                LibNoDaveConfig plcConnConf = connectionConfig as LibNoDaveConfig;
                if (plcConnConf != null)
                {
                    PLCConnection tmpConn = (PLCConnection)ConnectionList[connectionConfig];

                    if (plcConnConf.SynchronizePLCTime)
                    {
                        try
                        {
                            if (!tmpConn.Connected)
                            {
                                tmpConn.Connect();
                            }

                            tmpConn.PLCSetTime(DateTime.Now);
                            if (!plcConnConf.StayConnected)
                            {
                                tmpConn.Disconnect();
                            }
                        }
                        catch (Exception ex)
                        { }
                    }
                }
            }
        }
コード例 #9
0
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (_myconn == null)
            {
                _myconn = new PLCConnection(_connname);
                _myconn.Connect();

                foreach (string itm in _myconn.PLCListBlocks(PLCBlockType.AllEditableBlocks))
                    BlockList.Items.Add(itm);
            }
            else
            {
                if (dispatcherTimer!=null)
                    dispatcherTimer.Stop();
                if (myDiag != null)
                {
                    myDiag.Close();
                    myDiag.RemoveDiagnosticData();
                    textEditor.Text = myBlock.ToString();
                }
                _myconn.Dispose();
                _myconn = null;
                myDiag = null;
                BlockList.Items.Clear();
                blockName.Content = "FCxx";
                Upload.IsEnabled = false;
                Optimize.IsEnabled = false;
                Diag.IsEnabled = false;                             
            }

        }
コード例 #10
0
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                PLCConnection myConn = new PLCConnection(lstConnections.SelectedItem.ToString());
                myConn.Connect();

                int anz  = readBytes.Length / Convert.ToInt32(txtSize.Text);
                int olen = Convert.ToInt32(txtSize.Text);
                int len  = Convert.ToInt32(txtNewSize.Text);

                for (int n = 0; n < anz; n++)
                {
                    PLCTag plcTag = new PLCTag()
                    {
                        TagDataType = TagDataType.ByteArray, TagDataSource = MemoryArea.Datablock, DataBlockNumber = Convert.ToInt32(txtDB.Text), ByteAddress = Convert.ToInt32(txtStartByte.Text) + n * Convert.ToInt32(txtNewSize.Text), ArraySize = Convert.ToInt32(txtSize.Text)
                    };

                    byte[] ctrlV = new byte[len];
                    Array.Copy(readBytes, olen * n, ctrlV, 0, olen);
                    plcTag.Controlvalue = ctrlV;

                    myConn.WriteValue(plcTag);
                }
                lblState.Text = anz.ToString() + " Strukturen a " + len.ToString() + " Bytes geschrieben!";
            }
            catch (Exception ex)
            {
                lblState.Text = ex.Message;
            }
        }
コード例 #11
0
        private void synchronizePLCTimes(object tmp)
        {
            foreach (ConnectionConfig connectionConfig in akConfig.Connections)
            {
                LibNoDaveConfig plcConnConf = connectionConfig as LibNoDaveConfig;
                if (plcConnConf != null)
                {
                    PLCConnection tmpConn = (PLCConnection)ConnectionList[connectionConfig];

                    if (plcConnConf.SynchronizePLCTime)
                    {
                        try
                        {
                            if (!tmpConn.Connected)
                            {
                                Logging.LogTextToLog4Net("synchronizePLCTimes() => \"" + plcConnConf.Name + "\" => Connect...");
                                tmpConn.Connect();
                            }

                            tmpConn.PLCSetTime(DateTime.Now);
                            if (!plcConnConf.StayConnected)
                            {
                                Logging.LogTextToLog4Net("synchronizePLCTimes() => \"" + plcConnConf.Name + "\" Discconnect because !StayConnected");
                                tmpConn.Disconnect();
                            }
                        }
                        catch (Exception ex)
                        { }
                    }
                }
            }
        }
コード例 #12
0
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                PLCConnection myConn = new PLCConnection(lstConnections.SelectedItem.ToString());
                myConn.Connect();

                int anz = readBytes.Length/Convert.ToInt32(txtSize.Text);
                int olen = Convert.ToInt32(txtSize.Text);
                int len = Convert.ToInt32(txtNewSize.Text);

                for (int n = 0; n < anz; n++)
                {
                    PLCTag plcTag = new PLCTag() {TagDataType = TagDataType.ByteArray, TagDataSource = MemoryArea.Datablock, DataBlockNumber = Convert.ToInt32(txtDB.Text), ByteAddress = Convert.ToInt32(txtStartByte.Text) + n*Convert.ToInt32(txtNewSize.Text), ArraySize = Convert.ToInt32(txtSize.Text)};

                    byte[] ctrlV = new byte[len];
                    Array.Copy(readBytes, olen*n, ctrlV, 0, olen);
                    plcTag.Controlvalue = ctrlV;

                    myConn.WriteValue(plcTag);
                }
                lblState.Text = anz.ToString() + " Strukturen a " + len.ToString() + " Bytes geschrieben!";
            }
            catch (Exception ex)
            {
                lblState.Text = ex.Message;
            }
        }
コード例 #13
0
 static void Main(string[] args)
 {
     if (args.Length > 0)
     {
         if (args[0] == "/config" || args[0] == "-config" || args[0] == "--config")
         {
             Configuration.ShowConfiguration("CommandWhenBitSetConn", true);
         }
     }
     else
     {
         try
         {
             PLCConnection myConn = new PLCConnection("CommandWhenBitSetConn");
             myConn.Connect();
             PLCTag tag = new PLCTag(Settings.Default.PLCVar);
             myConn.ReadValue(tag);
             if ((bool)tag.Value == true)
             {
                 Process P = new Process();
                 P.StartInfo.FileName  = Settings.Default.CommandToRun;
                 P.StartInfo.Arguments = Settings.Default.CommandToRun;
                 P.Start();
             }
         }
         catch (Exception ex)
         {
             Console.WriteLine(ex.Message);
         }
     }
 }
コード例 #14
0
        public override void Connect_To_Database(StorageConfig config)
        {
            myConfig = config as PLCConfig;

            _plcConnection = new PLCConnection(myConfig.Configuration);
            _plcConnection.AutoConnect = true;
            _plcConnection.Connect();
        }
コード例 #15
0
        public override void Connect_To_Database(StorageConfig config)
        {
            myConfig = config as PLCConfig;

            _plcConnection             = new PLCConnection(myConfig.Configuration);
            _plcConnection.AutoConnect = true;
            _plcConnection.Connect();
        }
コード例 #16
0
        public override void Connect_To_Database(StorageConfig config)
        {
            myConfig = config as PLCConfig;

            _plcConnection             = new PLCConnection(myConfig.Configuration);
            _plcConnection.AutoConnect = true;
            _plcConnection.Connect();
            Logging.LogTextToLog4Net("Connect_To_Database() => \"" + _plcConnection.Name + "\" => Connect...");
        }
コード例 #17
0
ファイル: MainForm.cs プロジェクト: MaxOLydian/s7-diff-merge
        private void button4_Click(object sender, EventArgs e)
        {
            myConn = new PLCConnection("SimpleCSharpDemonstrationConnection");
            myConn.Connect();

            var db = myConn.PLCGetBlockInMC7("DB99");
            MessageBox.Show("DB:" + Encoding.ASCII.GetString(db));
            myConn.PLCPutBlockFromMC7toPLC("DB98", db);
        }
コード例 #18
0
        public override void Connect_To_Database(StorageConfig config)
        {
            myConfig = config as PLCConfig;

            _plcConnection = new PLCConnection(myConfig.Configuration);
            _plcConnection.AutoConnect = true;
            _plcConnection.Connect();
            Logging.LogTextToLog4Net("Connect_To_Database() => \"" + _plcConnection.Name + "\" => Connect...");
        }
コード例 #19
0
        private void button4_Click(object sender, EventArgs e)
        {
            myConn = new PLCConnection("SimpleCSharpDemonstrationConnection");
            myConn.Connect();

            var db = myConn.PLCGetBlockInMC7("DB99");

            MessageBox.Show("DB:" + Encoding.ASCII.GetString(db));
            myConn.PLCPutBlockFromMC7toPLC("DB98", db);
        }
コード例 #20
0
 private void button2_Click(object sender, EventArgs e)
 {
     try
     {
         myConn = new PLCConnection("SimpleCSharpDemonstrationConnection");
         myConn.Connect();
         timer.Enabled = true;
     }
     catch (Exception ex)
     { }
 }
コード例 #21
0
 private void button2_Click(object sender, EventArgs e)
 {
     try
     {
         myConn = new PLCConnection("SimpleCSharpDemonstrationConnection");
         myConn.Connect();
         timer.Enabled = true;
     }
     catch(Exception ex)
     { }
 }
コード例 #22
0
        private static void WriteNck(string ipAddress = "192.168.214.1")
        {
            using (var con = new PLCConnection("WriteNck"))
            {
                con.Configuration.CpuIP   = ipAddress;
                con.Configuration.CpuSlot = 4;
                con.Connect();

                try
                {
                    if (!con.Connected)
                    {
                        con.Connect();
                    }

                    #region Channel 1 R[0]
                    var R0 = new NC_Var(0x82, 0x41, 0x1, 0x1, 0x15, 0x1, 0xF, 0x8).GetNckTag();
                    R0.Controlvalue = 5;
                    con.WriteValue(R0);
                    #endregion

                    #region List of R-Parameter
                    var rpa     = new NC_Var(0x82, 0x40, 0x1, 0x0, 0x15, 0x1, 0xF, 0x8);
                    var tags    = new List <PLCNckTag>();
                    int channel = 1;
                    for (int i = 1; i < 10; i++)
                    {
                        tags.Add(rpa.GetNckTag(channel, i));
                        tags.Last().Controlvalue = i;
                    }
                    con.WriteValues(tags);
                    #endregion
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debugger.Break();
                }
            }
        }
コード例 #23
0
        private static void ReadFromNck(string ipAddress = "192.168.214.1")
        {
            using (var con = new PLCConnection("ReadFromNck"))
            {
                con.Configuration.CpuIP   = ipAddress;
                con.Configuration.CpuSlot = 4;
                con.Connect();

                try
                {
                    if (!con.Connected)
                    {
                        con.Connect();
                    }

                    #region Channel 1 R[0]
                    var R0 = con.ReadValue(new NC_Var(0x82, 0x41, 0x1, 0x1, 0x15, 0x1, 0xF, 0x8));
                    Console.WriteLine("R0: {0}", R0);
                    #endregion

                    #region List of R-Parameter
                    var rpa     = new NC_Var(0x82, 0x40, 0x1, 0x0, 0x15, 0x1, 0xF, 0x8);
                    var tags    = new List <PLCNckTag>();
                    int channel = 1;
                    for (int i = 1; i < 10; i++)
                    {
                        tags.Add(rpa.GetNckTag(channel, i));
                        tags.Last().Tag = string.Format("R[{0}]", i - 1);
                    }
                    con.ReadValues(tags);
                    tags.ForEach(f => Console.WriteLine("{0}: {1}", f.Tag, f.Value));
                    #endregion
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debugger.Break();
                }
            }
        }
コード例 #24
0
ファイル: Form1.cs プロジェクト: pwndubey77/s7-diff-merge
 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
 {
     try
     {
         myConn.Connect();
         myDelegate += new stateConnectedDelegate(stateConnected);
         this.Invoke(myDelegate);
     }
     catch (Exception)
     {
         backgroundWorker1.ReportProgress(0);
     }
 }
コード例 #25
0
        private void ConnectPLC()
        {
            plcConnection = new List <PLCConnection>();
            try
            {
                PLCConnection akConn = new PLCConnection(conn[0]);

                plcConnection.Add(akConn);
                akConn.Connect();
            }
            catch (Exception ex)
            {
                DisconnectPLC();
                return;
            }
        }
コード例 #26
0
 private void DownloadBlock_Load(object sender, EventArgs e)
 {            
     try
     {
         myConn = new PLCConnection(ConnectionName);
         label1.Text = ConnectionName + "\r\n" +
                       (new PLCConnectionConfiguration(ConnectionName)).ToString();
         myConn.Connect();
         listBox1.Items.AddRange(myConn.PLCListBlocks(PLCBlockType.AllEditableBlocks).ToArray());
         //myConn.PLCSendPassword("admin");
     }
     catch(Exception ex)
     {
         MessageBox.Show(ex.Message);
         this.Close();
     }
 }
コード例 #27
0
 private void DownloadBlock_Load(object sender, EventArgs e)
 {
     try
     {
         myConn      = new PLCConnection(ConnectionName);
         label1.Text = ConnectionName + "\r\n" +
                       (new PLCConnectionConfiguration(ConnectionName)).ToString();
         myConn.Connect();
         listBox1.Items.AddRange(myConn.PLCListBlocks(PLCBlockType.AllEditableBlocks).ToArray());
         //myConn.PLCSendPassword("admin");
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
         this.Close();
     }
 }
コード例 #28
0
        protected override void OnStart(string[] args)
        {
            base.OnStart(args);

            var cfg = new PLCConnectionConfiguration("myConnection", LibNodaveConnectionConfigurationType.ObjectSavedConfiguration);
            cfg.ConnectionType = (int) LibNodaveConnectionTypes.ISO_over_TCP;
            cfg.CpuIP = "192.168.1.185";
            cfg.CpuSlot = 2;

            myConn = new PLCConnection(cfg);
            myConn.Connect();
            
            threadShouldRun = true;

            myThread = new Thread(new ThreadStart(this.ThreadProc));
            myThread.Start();                                
        }
コード例 #29
0
        public bool Connect()
        {
            _plcConnection.Connect();

            if (!_plcConnection.Connected)
            {
                RaiseError();
            }
            else
            {
                RaiseIsConnected();
            }

            _dataReadTimer.Start();

            return(_plcConnection.Connected);
        }
コード例 #30
0
        private void searchPasswordToolStripMenuItem_Click(object sender, EventArgs e)
        {
            char[] zeichen = { ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };


            myConn = new PLCConnection((string)lstConnections.SelectedItem);
            myConn.Connect();

            Int64 anz = 0;

            bool   pwdCorr = false;
            string pwd     = "12345678";

            int[] cnt = new int[8];
            while (!pwdCorr)
            {
                cnt[0]++;
                for (int n = 0; n < 7; n++)
                {
                    if (cnt[n] >= zeichen.Length)
                    {
                        cnt[n + 1]++;
                        cnt[n] = 0;
                    }
                }

                if (cnt[7] >= zeichen.Length)
                {
                    MessageBox.Show("Password not found!");
                    return;
                }


                pwd = "";
                for (int n = 0; n < 8; n++)
                {
                    pwd += zeichen[cnt[n]];
                }

                anz++;
                pwdCorr = myConn.PLCSendPassword(pwd.Trim());
            }

            MessageBox.Show("Passwort:" + pwd + "\n" + "Anzahl geprüfter Kennwörter:" + anz.ToString());
        }
コード例 #31
0
 private void button1_Click(object sender, EventArgs e)
 {
     try
     {
         PLCConnection myConn = new PLCConnection(lstConnections.SelectedItem.ToString());
         myConn.Connect();
         PLCTag plcTag=new PLCTag(){TagDataType = TagDataType.ByteArray, TagDataSource = MemoryArea.Datablock, DataBlockNumber = Convert.ToInt32(txtDB.Text), ByteAddress = Convert.ToInt32(txtStartByte.Text), ArraySize = Convert.ToInt32(txtBytes.Text)};
         myConn.ReadValue(plcTag);
         readBytes = (byte[]) plcTag.Value;
         myConn.Disconnect();
         lblState.Text = readBytes.Length.ToString() + " Bytes gelesen";
         MessageBox.Show("So nun den neuen DB übertragen....");
     }
     catch (Exception ex)
     {
         lblState.Text = ex.Message;
     }
 }
コード例 #32
0
        protected override void OnStart(string[] args)
        {
            base.OnStart(args);

            var cfg = new PLCConnectionConfiguration("myConnection", LibNodaveConnectionConfigurationType.ObjectSavedConfiguration);

            cfg.ConnectionType = LibNodaveConnectionTypes.ISO_over_TCP;
            cfg.CpuIP          = "192.168.1.185";
            cfg.CpuSlot        = 2;

            myConn = new PLCConnection(cfg);
            myConn.Connect();

            threadShouldRun = true;

            myThread = new Thread(new ThreadStart(this.ThreadProc));
            myThread.Start();
        }
コード例 #33
0
 private void watchToolStripMenuItem_Click(object sender, EventArgs e)
 {
     lblStatus.Text = "";
     try
     {
         if (myConn != null)
         {
             myConn.Dispose();
         }
         myConn = new PLCConnection((string)lstConnections.SelectedItem);
         myConn.Connect();
         fetchPLCData.Enabled = true;
     }
     catch (Exception ex)
     {
         lblStatus.Text = ex.Message;
     }
     //myConn.Disconnect();
 }
コード例 #34
0
        private void startToolStripMenuItem_Click(object sender, EventArgs e)
        {
            plcConnections = new List<PLCConnection>();
            try
            {
                for (int n = 0; n < 6; n++)
                {
                    PLCConnection akConn = new PLCConnection(conn[n]);
                    plcConnections.Add(akConn);
                    akConn.Connect();
                }
            }
            catch (Exception ex)
            {
                closeConnections();
                MessageBox.Show("Fehler beim Verbindungsaufbau: " + ex.Message);
            }

            read_timer_plc1.Enabled = true;
            read_timer_plc2.Enabled = true;
        }
コード例 #35
0
 private void button1_Click(object sender, EventArgs e)
 {
     try
     {
         PLCConnection myConn = new PLCConnection(lstConnections.SelectedItem.ToString());
         myConn.Connect();
         PLCTag plcTag = new PLCTag()
         {
             TagDataType = TagDataType.ByteArray, TagDataSource = MemoryArea.Datablock, DataBlockNumber = Convert.ToInt32(txtDB.Text), ByteAddress = Convert.ToInt32(txtStartByte.Text), ArraySize = Convert.ToInt32(txtBytes.Text)
         };
         myConn.ReadValue(plcTag);
         readBytes = (byte[])plcTag.Value;
         myConn.Disconnect();
         lblState.Text = readBytes.Length.ToString() + " Bytes gelesen";
         MessageBox.Show("So nun den neuen DB übertragen....");
     }
     catch (Exception ex)
     {
         lblState.Text = ex.Message;
     }
 }
コード例 #36
0
        private void startToolStripMenuItem_Click(object sender, EventArgs e)
        {
            plcConnections = new List <PLCConnection>();
            try
            {
                for (int n = 0; n < 6; n++)
                {
                    PLCConnection akConn = new PLCConnection(conn[n]);
                    plcConnections.Add(akConn);
                    akConn.Connect();
                }
            }
            catch (Exception ex)
            {
                closeConnections();
                MessageBox.Show("Fehler beim Verbindungsaufbau: " + ex.Message);
            }

            read_timer_plc1.Enabled = true;
            read_timer_plc2.Enabled = true;
        }
コード例 #37
0
        private void tryConnect_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            PLCConnection tmpConn = null;

            try
            {
                var backup = myConfig.ConfigurationType;
                myConfig.ConfigurationType = LibNodaveConnectionConfigurationType.ObjectSavedConfiguration;
                tmpConn = new PLCConnection(myConfig);
                myConfig.ConfigurationType = backup;

                tmpConn.Connect();
                changeStatusLabel("Connected!");
                try
                {
                    var szlDat = tmpConn.PLCGetSZL(0x0111, 1);
                    if (szlDat.SZLDaten.Length > 0)
                    {
                        xy11Dataset xy11Szl = szlDat.SZLDaten[0] as xy11Dataset;
                        if (xy11Szl != null)
                        {
                            changeStatusLabel("Connected! (MLFB:" + xy11Szl.MlfB + ")");
                        }
                    }
                }
                catch (Exception ex)
                { }
                tmpConn.Dispose();
            }
            catch (Exception ex)
            {
                changeStatusLabel(ex.Message);
                tmpConn.Dispose();
            }
            finally
            {
                enableCmdTest();
            }
        }
コード例 #38
0
        private void cmdReadStruct_Click(object sender, EventArgs e)
        {
            myConn = new PLCConnection("SimpleCSharpDemonstrationConnection");
            myConn.Connect();
            //PLCTagGeneric
            PLCTag <TestStruct> tst = new PLCTag <TestStruct>()
            {
                DatablockNumber = 97, ByteAddress = 0
            };

            myConn.ReadValue(tst);
            TestStruct read = tst.GenericValue;

            TestStruct wrt = new TestStruct();

            wrt.aa           = 11;
            wrt.bb           = 12;
            wrt.cc           = 13;
            wrt.ee           = 14;
            wrt.ff           = 15;
            wrt.test         = "Bin da!";
            tst.Controlvalue = wrt;
            myConn.WriteValue(tst);
        }
コード例 #39
0
        public string CheckConfiguration(bool TestConnections)
        {
            string error = "";

            var ConnectionList = new Dictionary <ConnectionConfig, PLCConnection>();

            //Try to Connect to the PLCs
            if (TestConnections)
            {
                foreach (ConnectionConfig connectionConfig in Connections)
                {
                    LibNoDaveConfig lConn = connectionConfig as LibNoDaveConfig;
                    if (lConn != null)
                    {
                        using (PLCConnection myConn = new PLCConnection(lConn.Configuration))
                        {
                            try
                            {
                                myConn.Connect();
                                ConnectionList.Add(connectionConfig, myConn);
                            }
                            catch (Exception ex)
                            {
                                error += "Error: Error connecting \"" + lConn.Name + "\" : " + ex.Message + Environment.NewLine;
                            }
                        }
                    }
                }
            }

            var TCPIPKeys = new Dictionary <string, int>();

            foreach (DatasetConfig datasetConfig in Datasets)
            {
                var DatasetConnectionKeys = new Dictionary <string, object>();

                //Look if TCPIP Connection is only used in one Dataset, because we need the length for each Connection!
                try
                {
                    if (datasetConfig.DatasetConfigRows.Count > 0)
                    {
                        if (datasetConfig.DatasetConfigRows[0].Connection != null)
                        {
                            var tcp       = datasetConfig.DatasetConfigRows[0].Connection as TCPIPConfig;
                            var byteCount = ReadData.GetCountOfBytesToRead(datasetConfig.DatasetConfigRows);
                            if (tcp != null && !tcp.DontUseFixedTCPLength)
                            {
                                if (!TCPIPKeys.ContainsKey(datasetConfig.DatasetConfigRows[0].Connection.Name))
                                {
                                    TCPIPKeys.Add(datasetConfig.DatasetConfigRows[0].Connection.Name, byteCount);
                                }

                                if (TCPIPKeys[datasetConfig.DatasetConfigRows[0].Connection.Name] != byteCount)
                                {
                                    error += "Error: Dataset \"" + datasetConfig.Name + "\" - The same TCP/IP Connection is used in more than one Dataset with differnet bytes sizes, but fixed Length should be used!" + Environment.NewLine;
                                }
                            }
                        }
                    }
                    else
                    {
                        error += "Error: Dataset \"" + datasetConfig.Name + "\" - No DatasetConfigRow is set!" + Environment.NewLine;
                    }
                }
                catch
                {
                    error += "Error: Dataset \"" + datasetConfig.Name + "\" - The same TCP/IP Connection is used in more than one Dataset!" + Environment.NewLine;
                }

                //Look if Trigger on a Dataset with TCP/IP Connection is Incoming Data, and that this trigger is not used on a Connection without TCP/IP
                try
                {
                    if (!(datasetConfig.TriggerConnection is TCPIPConfig && (datasetConfig.Trigger == DatasetTriggerType.Triggered_By_Incoming_Data_On_A_TCPIP_Connection)))
                    {
                        error += "Error: Dataset \"" + datasetConfig.Name + "\" - The selected Connection for incoming Trigger is no TCP/IP Connection !" + Environment.NewLine;
                    }
                }
                catch { }

                //Look if Trigger Connection was selected (Handshake Trigger)
                if (datasetConfig.Trigger == DatasetTriggerType.Tags_Handshake_Trigger && datasetConfig.TriggerConnection == null)
                {
                    error += "Error: Dataset \"" + datasetConfig.Name + "\" Trigger Connection not set!" + Environment.NewLine;
                }

                //Look if Trigger Connection was selected (TCPIP Trigger)
                if (datasetConfig.Trigger == DatasetTriggerType.Triggered_By_Incoming_Data_On_A_TCPIP_Connection && datasetConfig.TriggerConnection == null)
                {
                    error += "Error: Dataset \"" + datasetConfig.Name + "\" Trigger Connection not set!" + Environment.NewLine;
                }

                if (datasetConfig.Storage == null)
                {
                    error += "Error: Dataset \"" + datasetConfig.Name + " - Storage is not set!" + Environment.NewLine;
                }

                foreach (DatasetConfigRow datasetConfigRow in datasetConfig.DatasetConfigRows)
                {
                    if (datasetConfigRow.Connection == null && datasetConfig.Trigger != DatasetTriggerType.Triggered_By_Incoming_Data_On_A_TCPIP_Connection)
                    {
                        error += "Error: Dataset \"" + datasetConfig.Name + "\" Row \"" + datasetConfigRow.DatabaseField + "\" - Connection not Set!" + Environment.NewLine;
                    }
                }

                if (datasetConfig.Trigger == DatasetTriggerType.Tags_Handshake_Trigger && datasetConfig.TriggerConnection == null)
                {
                    error += "Error: Dataset \"" + datasetConfig.Name + "\" Trigger Connection not set!" + Environment.NewLine;
                }



                foreach (DatasetConfigRow datasetConfigRow in datasetConfig.DatasetConfigRows)
                {
                    //Look if PLC-Connection was selected
                    if (datasetConfigRow.Connection == null && datasetConfig.Trigger != DatasetTriggerType.Triggered_By_Incoming_Data_On_A_TCPIP_Connection)
                    {
                        error += "Error: Dataset \"" + datasetConfig.Name + "\" Row \"" + datasetConfigRow.DatabaseField + "\" - Connection not Set!" + Environment.NewLine;
                    }
                    //Look if DatabaseFieldType was selected
                    if (datasetConfigRow.DatabaseFieldType == "")
                    {
                        error += "Error: Dataset \"" + datasetConfig.Name + "\" Row \"" + datasetConfigRow.DatabaseField + "\" - DatabaseFieldType not Set!" + Environment.NewLine;
                    }
                    ////Look if PLC-ValueType was selected
                    if (datasetConfigRow.PLCTag.LibNoDaveDataType == null)
                    {
                        error += "Error: Dataset \"" + datasetConfig.Name + "\" Row \"" + datasetConfigRow.DatabaseField + "\" - PLC-ValueType not Set!" + Environment.NewLine;
                    }

                    if (TestConnections && datasetConfig.Trigger != DatasetTriggerType.Triggered_By_Incoming_Data_On_A_TCPIP_Connection)
                    {
                        PLCConnection conn = null as PLCConnection;
                        if (datasetConfigRow.Connection != null)
                        {
                            try
                            {
                                conn = ConnectionList[datasetConfigRow.Connection] as PLCConnection;
                            }
                            catch
                            {
                                conn = null;
                            }
                        }
                        else
                        {
                            conn = null;
                        }
                        if (conn != null)
                        {
                            try
                            {
                                conn.ReadValue(datasetConfigRow.PLCTag);
                                if (datasetConfigRow.PLCTag.ItemDoesNotExist)
                                {
                                    error += "Error: Dataset \"" + datasetConfig.Name + "\" Row \"" + datasetConfigRow.DatabaseField + "\" - Error Reading Value on Address " + datasetConfigRow.PLCTag.S7FormatAddress + " !" + Environment.NewLine;
                                }
                            }
                            catch (Exception ex)
                            {
                                error += "Error: Dataset \"" + datasetConfig.Name + "\" Row \"" + datasetConfigRow.DatabaseField + "\" - Error Reading Value on Address " + datasetConfigRow.PLCTag.S7FormatAddress + " !" + Environment.NewLine;
                            }
                        }
                    }
                }
            }

            //Look if Connection Name exists only once
            var ConnectionNames = new List <string>();
            var ConnectionKeys  = new Dictionary <string, object>();

            foreach (ConnectionConfig item in Connections)
            {
                if (ConnectionKeys.ContainsKey(item.Name))
                {
                    error += "Error: Connection name \"" + item.Name + "\" - exist more than once!" + Environment.NewLine;
                }
                else
                {
                    ConnectionKeys.Add(item.Name, null);
                }
            }

            //Look if Storrage Name exists only once
            var StorageNames = new List <string>();
            var StoragesKeys = new Dictionary <string, object>();

            foreach (StorageConfig item in Storages)
            {
                if (StoragesKeys.ContainsKey(item.Name))
                {
                    error += "Error: Storage name \"" + item.Name + "\" - exist more than once!" + Environment.NewLine;
                }
                else
                {
                    StoragesKeys.Add(item.Name, null);
                }
            }


            //Look if the Database Field Type is in the Field Types List

            //Look if Field Name Exists only Once -> This is possible in excel, but not in databases

            if (error == "")
            {
                return(null);
            }
            return(error);
        }
コード例 #40
0
        private void EstablishConnections()
        {
            foreach (ConnectionConfig connectionConfig in akConfig.Connections)
            {
                LibNoDaveConfig plcConnConf   = connectionConfig as LibNoDaveConfig;
                TCPIPConfig     tcpipConnConf = connectionConfig as TCPIPConfig;
                DatabaseConfig  dbConnConf    = connectionConfig as DatabaseConfig;


                if (plcConnConf != null)
                {
                    Logging.LogText("Connection: " + connectionConfig.Name + " is starting...", Logging.LogLevel.Information);

                    PLCConnection tmpConn = new PLCConnection(plcConnConf.Configuration);
                    try
                    {
                        tmpConn.Connect();
                        if (!plcConnConf.StayConnected)
                        {
                            tmpConn.Disconnect();
                        }
                    }
                    catch (Exception ex)
                    {
                        Logging.LogText("Connection: " + connectionConfig.Name, ex, Logging.LogLevel.Warning);
                    }

                    ConnectionList.Add(connectionConfig, tmpConn);
                }
                else if (dbConnConf != null)
                {
                    var tmpConn = new DatabaseConnection(dbConnConf);
                    try
                    {
                        tmpConn.Connect();
                    }
                    catch (Exception ex)
                    {
                        Logging.LogText("Connection: " + connectionConfig.Name, ex, Logging.LogLevel.Warning);
                    }
                    ConnectionList.Add(connectionConfig, tmpConn);
                }
                else if (tcpipConnConf != null)
                {
                    //todo: legth of tcp conn
                    //TCPFunctionsAsync tmpConn = new TCPFunctionsAsync(new SynchronizationContext(), tcpipConnConf.IPasIPAddres, tcpipConnConf.Port, !tcpipConnConf.PassiveConnection, 0);

                    //tmpConn.Connect();

                    //ConnectionList.Add(connectionConfig, tmpConn);
                }
            }

            myReEstablishConnectionsThreads = new List <Thread>();
            foreach (ConnectionConfig connectionConfig in akConfig.Connections)
            {
                if (connectionConfig is LibNoDaveConfig)
                {
                    var thrd = new Thread(new ParameterizedThreadStart(ReEstablishConnectionsThreadProc))
                    {
                        Name = "EstablishConnectionsThreadProc"
                    };
                    thrd.Start(connectionConfig as LibNoDaveConfig);
                    this.myReEstablishConnectionsThreads.Add(thrd);
                }
            }
        }
コード例 #41
0
        private void cmdReadStruct_Click(object sender, EventArgs e)
        {
            myConn = new PLCConnection("SimpleCSharpDemonstrationConnection");
            myConn.Connect();
            //PLCTagGeneric
            PLCTag<TestStruct> tst = new PLCTag<TestStruct>() {DataBlockNumber = 97, ByteAddress = 0};
            myConn.ReadValue(tst);
            TestStruct read = tst.GenericValue;

            TestStruct wrt = new TestStruct();
            wrt.aa = 11;
            wrt.bb = 12;
            wrt.cc = 13;
            wrt.ee = 14;
            wrt.ff = 15;
            wrt.test = "Bin da!";
            tst.Controlvalue = wrt;
            myConn.WriteValue(tst);

        }
コード例 #42
0
        private void searchPasswordToolStripMenuItem_Click(object sender, EventArgs e)
        {
            char[] zeichen =
            {
                ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
                'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
                'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6',
                '7', '8', '9'
            };


            myConn = new PLCConnection((string) lstConnections.SelectedItem);
            myConn.Connect();

            Int64 anz = 0;

            bool pwdCorr = false;
            string pwd = "12345678";
            int[] cnt = new int[8];
            while (!pwdCorr)
            {
                cnt[0]++;
                for (int n = 0; n < 7; n++)
                {
                    if (cnt[n] >= zeichen.Length)
                    {
                        cnt[n + 1]++;
                        cnt[n] = 0;
                    }
                }

                if (cnt[7] >= zeichen.Length)
                {
                    MessageBox.Show("Password not found!");
                    return;
                }


                pwd = "";
                for (int n = 0; n < 8; n++)
                {
                    pwd += zeichen[cnt[n]];
                }

                anz++;
                pwdCorr = myConn.PLCSendPassword(pwd.Trim());
            }

            MessageBox.Show("Passwort:" + pwd + "\n" + "Anzahl geprüfter Kennwörter:" + anz.ToString());

        }
コード例 #43
0
 private void watchToolStripMenuItem_Click(object sender, EventArgs e)
 {
     lblStatus.Text = "";
     try
     {
         if (myConn != null) myConn.Dispose();
         myConn = new PLCConnection((string) lstConnections.SelectedItem);
         myConn.Connect();
         fetchPLCData.Enabled = true;
     }
     catch (Exception ex)
     {
         lblStatus.Text = ex.Message;
     }
     //myConn.Disconnect();
 }
コード例 #44
0
        private void EstablishConnections()
        {
            foreach (ConnectionConfig connectionConfig in akConfig.Connections)
            {
                LibNoDaveConfig plcConnConf = connectionConfig as LibNoDaveConfig;
                TCPIPConfig tcpipConnConf = connectionConfig as TCPIPConfig;
                DatabaseConfig dbConnConf = connectionConfig as DatabaseConfig;

                if (plcConnConf != null)
                {
                    Logging.LogText("Connection: " + connectionConfig.Name + " is starting...", Logging.LogLevel.Information);

                    PLCConnection tmpConn = new PLCConnection(plcConnConf.Configuration);
                    try
                    {
                        tmpConn.Connect();
                        if (!plcConnConf.StayConnected)
                            tmpConn.Disconnect();
                    }
                    catch (Exception ex)
                    {
                        Logging.LogText("Connection: " + connectionConfig.Name, ex, Logging.LogLevel.Warning);
                    }

                    ConnectionList.Add(connectionConfig, tmpConn);
                }
                else if (dbConnConf != null)
                {
                    var tmpConn = new DatabaseConnection(dbConnConf);
                    try
                    {
                        tmpConn.Connect();
                    }
                    catch (Exception ex)
                    {
                        Logging.LogText("Connection: " + connectionConfig.Name, ex, Logging.LogLevel.Warning);
                    }
                    ConnectionList.Add(connectionConfig, tmpConn);
                }
                else if (tcpipConnConf != null)
                {
                    //todo: legth of tcp conn
                    //TCPFunctionsAsync tmpConn = new TCPFunctionsAsync(new SynchronizationContext(), tcpipConnConf.IPasIPAddres, tcpipConnConf.Port, !tcpipConnConf.PassiveConnection, 0);

                    //tmpConn.Connect();

                    //ConnectionList.Add(connectionConfig, tmpConn);
                }
            }

            myReEstablishConnectionsThreads = new List<Thread>();
            foreach (ConnectionConfig connectionConfig in akConfig.Connections)
            {
                if (connectionConfig is LibNoDaveConfig)
                {
                    var thrd = new Thread(new ParameterizedThreadStart(ReEstablishConnectionsThreadProc)) { Name = "EstablishConnectionsThreadProc" };
                    thrd.Start(connectionConfig as LibNoDaveConfig);
                    this.myReEstablishConnectionsThreads.Add(thrd);
                }
            }
        }
コード例 #45
0
        private void tryConnect_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            PLCConnection tmpConn = null;
            try
            {
                var backup = myConfig.ConfigurationType;
                myConfig.ConfigurationType = LibNodaveConnectionConfigurationType.ObjectSavedConfiguration;
                tmpConn = new PLCConnection(myConfig);
                myConfig.ConfigurationType = backup;

                tmpConn.Connect();
                changeStatusLabel("Connected!");
                try
                {
                    var szlDat = tmpConn.PLCGetSZL(0x0111, 1);
                    if (szlDat.SZLDaten.Length > 0)
                    {
                        xy11Dataset xy11Szl = szlDat.SZLDaten[0] as xy11Dataset;
                        if (xy11Szl != null)
                            changeStatusLabel("Connected! (MLFB:" + xy11Szl.MlfB + ")");
                    }
                }
                catch (Exception ex)
                { }
                tmpConn.Dispose();
                
            }
            catch (Exception ex)
            {
                changeStatusLabel(ex.Message);
                tmpConn.Dispose();
            }
            finally
            {
                enableCmdTest();
            }
        }
        public string CheckConfiguration(bool TestConnections)
        {
            string error = "";

            var ConnectionList = new Dictionary<ConnectionConfig, PLCConnection>();

            //Try to Connect to the PLCs
            if (TestConnections)
                foreach (ConnectionConfig connectionConfig in Connections)
                {
                    LibNoDaveConfig lConn = connectionConfig as LibNoDaveConfig;
                    if (lConn != null)
                    {
                        using (PLCConnection myConn = new PLCConnection(lConn.Configuration))
                        {
                            try
                            {
                                myConn.Connect();
                                ConnectionList.Add(connectionConfig, myConn);
                            }
                            catch (Exception ex)
                            {
                                error += "Error: Error connecting \"" + lConn.Name + "\" : " + ex.Message + Environment.NewLine;
                            }
                        }
                    }
                }

            var TCPIPKeys = new Dictionary<string, int>();

            foreach (DatasetConfig datasetConfig in Datasets)
            {
                var DatasetConnectionKeys = new Dictionary<string, object>();

                //Look if TCPIP Connection is only used in one Dataset, because we need the length for each Connection!
                try
                {
                    if (datasetConfig.DatasetConfigRows.Count > 0)
                    {
                        if (datasetConfig.DatasetConfigRows[0].Connection != null)
                        {
                            var tcp = datasetConfig.DatasetConfigRows[0].Connection as TCPIPConfig;
                            var byteCount = ReadData.GetCountOfBytesToRead(datasetConfig.DatasetConfigRows);
                            if (tcp != null && !tcp.DontUseFixedTCPLength)
                            {
                                if (!TCPIPKeys.ContainsKey(datasetConfig.DatasetConfigRows[0].Connection.Name))
                                {
                                    TCPIPKeys.Add(datasetConfig.DatasetConfigRows[0].Connection.Name, byteCount);
                                }

                                if (TCPIPKeys[datasetConfig.DatasetConfigRows[0].Connection.Name] != byteCount)
                                {
                                    error += "Error: Dataset \"" + datasetConfig.Name + "\" - The same TCP/IP Connection is used in more than one Dataset with differnet bytes sizes, but fixed Length should be used!" + Environment.NewLine;
                                }
                            }
                        }
                    }
                    else
                    {
                        error += "Error: Dataset \"" + datasetConfig.Name + "\" - No DatasetConfigRow is set!" + Environment.NewLine;
                    }
                }
                catch
                {
                    error += "Error: Dataset \"" + datasetConfig.Name + "\" - The same TCP/IP Connection is used in more than one Dataset!" + Environment.NewLine;
                }

                //Look if Trigger on a Dataset with TCP/IP Connection is Incoming Data, and that this trigger is not used on a Connection without TCP/IP
                try
                {
                    if (!(datasetConfig.TriggerConnection is TCPIPConfig) && (datasetConfig.Trigger == DatasetTriggerType.Triggered_By_Incoming_Data_On_A_TCPIP_Connection))
                        error += "Error: Dataset \"" + datasetConfig.Name + "\" - The selected Connection for incoming Trigger is no TCP/IP Connection !" + Environment.NewLine;
                }
                catch { }

                //Look if Trigger Connection was selected (Handshake Trigger)
                if (datasetConfig.Trigger == DatasetTriggerType.Tags_Handshake_Trigger && datasetConfig.TriggerConnection == null)
                    error += "Error: Dataset \"" + datasetConfig.Name + "\" Trigger Connection not set!" + Environment.NewLine;

                //Look if Trigger Connection was selected (TCPIP Trigger)
                if (datasetConfig.Trigger == DatasetTriggerType.Triggered_By_Incoming_Data_On_A_TCPIP_Connection && datasetConfig.TriggerConnection == null)
                    error += "Error: Dataset \"" + datasetConfig.Name + "\" Trigger Connection not set!" + Environment.NewLine;

                if (datasetConfig.Storage == null)
                    error += "Error: Dataset \"" + datasetConfig.Name + " - Storage is not set!" + Environment.NewLine;

                foreach (DatasetConfigRow datasetConfigRow in datasetConfig.DatasetConfigRows)
                {
                    if (datasetConfigRow.Connection == null && datasetConfig.Trigger != DatasetTriggerType.Triggered_By_Incoming_Data_On_A_TCPIP_Connection)
                        error += "Error: Dataset \"" + datasetConfig.Name + "\" Row \"" + datasetConfigRow.DatabaseField + "\" - Connection not Set!" + Environment.NewLine;
                }

                if (datasetConfig.Trigger==DatasetTriggerType.Tags_Handshake_Trigger && datasetConfig.TriggerConnection==null)
                    error += "Error: Dataset \"" + datasetConfig.Name + "\" Trigger Connection not set!" + Environment.NewLine;

                foreach (DatasetConfigRow datasetConfigRow in datasetConfig.DatasetConfigRows)
                {
                    //Look if PLC-Connection was selected
                    if (datasetConfigRow.Connection == null && datasetConfig.Trigger != DatasetTriggerType.Triggered_By_Incoming_Data_On_A_TCPIP_Connection) error += "Error: Dataset \"" + datasetConfig.Name + "\" Row \"" + datasetConfigRow.DatabaseField + "\" - Connection not Set!" + Environment.NewLine;
                    //Look if DatabaseFieldType was selected
                    if (datasetConfigRow.DatabaseFieldType == "") error += "Error: Dataset \"" + datasetConfig.Name + "\" Row \"" + datasetConfigRow.DatabaseField + "\" - DatabaseFieldType not Set!" + Environment.NewLine;
                    ////Look if PLC-ValueType was selected
                    if (datasetConfigRow.PLCTag.TagDataType == null)
                        error += "Error: Dataset \"" + datasetConfig.Name + "\" Row \"" + datasetConfigRow.DatabaseField + "\" - PLC-ValueType not Set!" + Environment.NewLine;

                    if (TestConnections && datasetConfig.Trigger != DatasetTriggerType.Triggered_By_Incoming_Data_On_A_TCPIP_Connection)
                    {
                        PLCConnection conn = null as PLCConnection;
                        if (datasetConfigRow.Connection != null)
                        {
                            try
                            {
                                conn = ConnectionList[datasetConfigRow.Connection] as PLCConnection;
                            }
                            catch
                            {
                                conn = null;
                            }
                        }
                        else conn = null;
                        if (conn != null)
                        {
                            try
                            {
                                conn.ReadValue(datasetConfigRow.PLCTag);
                                if (datasetConfigRow.PLCTag.ItemDoesNotExist) error += "Error: Dataset \"" + datasetConfig.Name + "\" Row \"" + datasetConfigRow.DatabaseField + "\" - Error Reading Value on Address " + datasetConfigRow.PLCTag.S7FormatAddress + " !" + Environment.NewLine;
                            }
                            catch (Exception ex)
                            {
                                error += "Error: Dataset \"" + datasetConfig.Name + "\" Row \"" + datasetConfigRow.DatabaseField + "\" - Error Reading Value on Address " + datasetConfigRow.PLCTag.S7FormatAddress + " !" + Environment.NewLine;
                            }
                        }
                    }
                }
            }

            //Look if Connection Name exists only once
            var ConnectionNames = new List<string>();
            var ConnectionKeys = new Dictionary<string, object>();
            foreach (ConnectionConfig item in Connections)
            {
                if (ConnectionKeys.ContainsKey(item.Name))
                    error += "Error: Connection name \"" + item.Name + "\" - exist more than once!" + Environment.NewLine;
                else
                    ConnectionKeys.Add(item.Name, null);
            }

            //Look if Storrage Name exists only once
            var StorageNames = new List<string>();
            var StoragesKeys = new Dictionary<string, object>();
            foreach (StorageConfig item in Storages)
            {
                if (StoragesKeys.ContainsKey(item.Name))
                    error += "Error: Storage name \"" + item.Name + "\" - exist more than once!" + Environment.NewLine;
                else
                    StoragesKeys.Add(item.Name, null);
            }

            //Look if the Database Field Type is in the Field Types List

            //Look if Field Name Exists only Once -> This is possible in excel, but not in databases

            if (error == "")
                return null;
            return error;
        }
コード例 #47
0
        public static IEnumerable <object> ReadDataFromDataSources(DatasetConfig datasetConfig, IEnumerable <DatasetConfigRow> datasetConfigRows, Dictionary <ConnectionConfig, Object> activConnections, bool StartedAsService)
        {
            var usedConnections = from n in datasetConfigRows
                                  group n by n.Connection into g       //Es wird in das Object g hineingruppiert.
                                  select new { Connection = g.Key };   //n.Connection ist dann g.Key, g ist eine ganze Zeile

            foreach (var usedConnection in usedConnections)
            {
                var tags = from n in datasetConfigRows
                           where n.Connection == usedConnection.Connection
                           select n.PLCTag;
                if (usedConnection.Connection.GetType() == typeof(DatabaseConfig))
                {
                    //Read Data from a Database
                    var dbConn = (DatabaseConnection)activConnections[usedConnection.Connection];

                    var dta = dbConn.ReadData();

                    try
                    {
                        foreach (var plcTag in tags)
                        {
                            plcTag.Value = dta[plcTag.ValueName];
                        }
                    }
                    finally
                    {
                        dta.Close();
                    }
                }
                else if (usedConnection.Connection.GetType() == typeof(LibNoDaveConfig))
                {
                    PLCConnection plcConn = (PLCConnection)activConnections[usedConnection.Connection];

                    if (!plcConn.Connected)
                    {
                        plcConn.Connect();
                    }

                    if (plcConn.Connected)
                    {
                        try
                        {
                            plcConn.ReadValues(tags);
                        }
                        catch (Exception ex)
                        {
                            if (StartedAsService)
                            {
                                Logging.LogText("Error: Exception during ReadData, maybe Connection interupted?", ex, Logging.LogLevel.Error);
                                return(null);
                            }

                            throw;
                        }
                        foreach (var plcTag in tags)
                        {
                            if (plcTag.ItemDoesNotExist)
                            {
                                if (StartedAsService)
                                {
                                    Logging.LogText("Tag does not Exist! " + plcConn.Configuration.ConnectionName + ": " + plcTag.S7FormatAddress, Logging.LogLevel.Error);
                                }
                                else
                                {
                                    throw new Exception("Tag does not Exist! " + plcConn.Configuration.ConnectionName + ": " + plcTag.S7FormatAddress);
                                }
                            }
                        }

                        if (usedConnection.Connection is LibNoDaveConfig)
                        {
                            if (!((LibNoDaveConfig)usedConnection.Connection).StayConnected)
                            {
                                plcConn.Disconnect();
                            }
                        }
                    }
                    else
                    {
                        Logging.LogText("Error: Read Data returned \"null\" maybe a Connection is offline?", Logging.LogLevel.Error);
                        return(null);
                    }
                }
            }

            List <object> retVal = new List <object>();

            foreach (var datasetConfigRow in datasetConfigRows)
            {
                retVal.Add(datasetConfigRow.Value(datasetConfig.UseFloatIfMultiplierIsUsed));
            }

            return(retVal);
        }