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();
            }
        }
        static void Main(string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine();
                Console.WriteLine("Bitte so starten:");
                Console.WriteLine();
                Console.WriteLine("BackupS7 Configfile Zielverzeichnis");
                Console.WriteLine();
                Console.ReadLine();
            }
            else
            {
                Console.WriteLine();
                Console.WriteLine("Creating Directory...");
                var configFile      = args[0];
                var targetDirectory = Path.Combine(Environment.CurrentDirectory, args[1]);

                var text   = File.ReadAllText(configFile);
                var config = JsonConvert.DeserializeObject <Config>(text);

                Directory.CreateDirectory(targetDirectory);

                Console.WriteLine("Connecting to PLC...");
                var connection = new PLCConnection(new PLCConnectionConfiguration("BackupS7", LibNodaveConnectionConfigurationType.ObjectSavedConfiguration)
                {
                    ConnectionType = LibNodaveConnectionTypes.ISO_over_TCP, CpuIP = config.Ip, CpuRack = config.Rack, CpuSlot = config.Slot
                });
                connection.Connect();

                var szlDat = connection.PLCGetSZL(0x0111, 1);
                if (szlDat.SZLDaten.Length > 0)
                {
                    xy11Dataset xy11Szl = szlDat.SZLDaten[0] as xy11Dataset;
                    if (xy11Szl != null)
                    {
                        Console.WriteLine("Connected, MLFB: " + xy11Szl.MlfB);
                    }
                }

                Console.WriteLine("Read Blocks...");
                var existingBlocks = connection.PLCListBlocks(PLCBlockType.AllEditableBlocks);

                var backupBlocks = new List <string>();
                if (config.BackupBlocks != null)
                {
                    foreach (var b in config.BackupBlocks)
                    {
                        var txt = b.Trim().ToUpper().Replace(" ", "");
                        if (txt.Contains("-"))
                        {
                            var range = txt.Split('-');
                            var type  = range[0].Substring(0, 2);
                            var start = int.Parse(range[0].Substring(2));
                            var end   = int.Parse(range[1].Substring(2));
                            backupBlocks.AddRange(Enumerable.Range(start, (end - start) + 1).Select(x => type + x));
                        }
                        else
                        {
                            backupBlocks.Add(txt);
                        }
                    }
                }

                Console.WriteLine("Backup Blocks...");
                foreach (var b in existingBlocks)
                {
                    bool backUp = false;
                    if (config.BackupType.HasFlag(BackupType.Datablocks) && b.StartsWith("DB"))
                    {
                        backUp = true;
                    }
                    if (config.BackupType.HasFlag(BackupType.Functions) && b.StartsWith("FC"))
                    {
                        backUp = true;
                    }
                    if (config.BackupType.HasFlag(BackupType.FunctionBlocks) && b.StartsWith("FB"))
                    {
                        backUp = true;
                    }
                    if (backupBlocks.Contains(b.ToUpper().Trim()))
                    {
                        backUp = true;
                    }

                    if (backUp)
                    {
                        var blk = connection.PLCGetBlockInMC7(b);

                        string       file = Path.Combine(targetDirectory, b + ".blk");
                        BinaryWriter wrt  = new BinaryWriter(File.Open(file, FileMode.Create));
                        wrt.Write(blk);
                        wrt.Close();
                    }
                }
                connection.Disconnect();
                Console.WriteLine("Finish");
            }
        }
        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();
            }
        }
Exemple #4
0
        /// <summary>
        /// Go through all reasonable Configurations for S7 Connections, and return one that works
        /// </summary>
        /// <param name="plcConfig"></param>

        private void SearchCPUs(PLCConnectionConfiguration plcConfig)
        {
            //go through Slot 0 to 5 and return all found CPU's, this is because S7-400 may have several CPU's in different Slots in one rack
            for (int i = 0; i <= 5; i++)
            {
                PLCConnectionConfiguration Conf = new PLCConnectionConfiguration();
                Conf.ConnectionType = plcConfig.ConnectionType;
                Conf.CpuIP          = plcConfig.CpuIP;
                Conf.Port           = plcConfig.Port;
                Conf.CpuRack        = plcConfig.CpuRack;
                Conf.CpuSlot        = i;

                PLCConnection Con = new PLCConnection(Conf);
                try
                {
                    Con.Connect();

                    FoundPlc FoundPlc = new FoundPlc();
                    FoundPlc.S7ConnectionSettings = Conf;

                    //Read name of PLC
                    var HWInfos = Con.PLCGetSZL(0x1c, 0);

                    foreach (xy1CDataset HWInfo in HWInfos.SZLDaten)
                    {
                        switch (HWInfo.Index)
                        {
                        case 1:
                            FoundPlc.S7ConnectionSettings.ConnectionName = HWInfo.Text;
                            FoundPlc.PlcName = HWInfo.Text;
                            break;

                        case 2:
                            FoundPlc.ModuleName = HWInfo.Text;
                            break;

                        case 3:
                            FoundPlc.PlantIdentification = HWInfo.Text;
                            break;

                        case 4:
                            FoundPlc.Copyright = HWInfo.Text;
                            break;

                        case 5:
                            FoundPlc.ModuleSerialNumber = HWInfo.Text;
                            break;

                        case 6:
                            break;

                        case 7:
                            FoundPlc.ModuleTypeName = HWInfo.Text;
                            break;

                        case 8:
                            break;

                        case 9:
                            FoundPlc.Manufacturer = HWInfo.Text;
                            break;

                        case 10:
                            break;

                        case 11:
                            FoundPlc.Location = HWInfo.Text;
                            break;
                        }
                    }

                    //Check if we actually found an CPU
                    //This is because some S7-400 CPUs also accept connections to Modules other than CPU, such as CP or similiar. But
                    //these are not wat we are looking for.
                    if (!FoundPlc.ModuleTypeName.ToLower().Contains("cpu") && !FoundPlc.ModuleName.ToLower().Contains("cpu"))
                    {
                        Debug.WriteLine("Found an available interface that accepted the connection but apparently was not an CPU. The adress was {0} and the Slot was {1}", plcConfig.CpuIP, plcConfig.CpuSlot);
                        continue;
                    }

                    lock (_DiscoveredPLCs)
                    {
                        _DiscoveredPLCs.Add(FoundPlc);
                    }
                    NewPlcFound?.Invoke(this, new PlcFoundEventArgs {
                        FoundPlc = FoundPlc
                    });
                    System.Threading.Thread.Sleep(100); //Some CPUs, especially IBH Softec Simulatin CPUs dont like it when the request are coming to fast. IBH chrashes
                }
                catch (Exception ex)
                {
                    continue;//Ignore erorr and try next Slot
                }
                finally
                {
                    Con.Disconnect();
                }
            }
        }