Example #1
0
 private void connectToolStripMenuItem_Click(object sender, EventArgs e)
 {
     if (select_dialog == null)
     {
         select_dialog = new PortalSelect(tiaThread);
     }
     if (select_dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
     {
         TiaPortalProcess proc = select_dialog.selectedProcess();
         if (proc != null)
         {
             WaitConnect wait = new WaitConnect();
             wait.Show();
             Application.DoEvents();
             try
             {
                 tiaPortal = (TiaPortal)tiaThread.RunSync((_) => { return(proc.Attach()); }, null);
                 connectToolStripMenuItem.Enabled = false;
                 btn_connect.Enabled = false;
                 disconnectToolStripMenuItem.Enabled = true;
                 btn_disconnect.Enabled = true;
                 PortalConnected();
             }
             catch (EngineeringException ex)
             {
                 MessageBox.Show("Failed to connect to TIAPortal: " + ex.Message);
             }
             wait.Hide();
             wait.Dispose();
         }
     }
 }
Example #2
0
        public bool CurrentProject(TiaPortalProcess tiaProcess, TiaPortal _MyTiaPortal, Project _MyProject)
        {
            bool ProjectStatus;
            IList <TiaPortalProcess> processes = TiaPortal.GetProcesses();

            switch (processes.Count)
            {
            case 1:
                tiaProcess   = processes[0];
                _MyTiaPortal = tiaProcess.Attach();

                if (_MyTiaPortal.Projects.Count <= 0)
                {
                    ProjectStatus = false;
                    return(ProjectStatus);
                }
                _MyProject    = _MyTiaPortal.Projects[0];
                ProjectStatus = true;
                break;

            case 0:
                ProjectStatus = false;
                return(ProjectStatus);

            default:
                ProjectStatus = false;
                return(ProjectStatus);
            }
            return(ProjectStatus);
        }
Example #3
0
        private void btn_ConnectTIA(object sender, EventArgs e)
        {
            btn_Connect.Enabled = false;
            IList <TiaPortalProcess> processes = TiaPortal.GetProcesses();

            switch (processes.Count)
            {
            case 1:
                _tiaProcess = processes[0];
                MyTiaPortal = _tiaProcess.Attach();
                if (MyTiaPortal.GetCurrentProcess().Mode == TiaPortalMode.WithUserInterface)
                {
                    rdb_WithUI.Checked = true;
                }
                else
                {
                    rdb_WithoutUI.Checked = true;
                }


                if (MyTiaPortal.Projects.Count <= 0)
                {
                    txt_Status.Text     = "No TIA Portal Project was found!";
                    btn_Connect.Enabled = true;
                    return;
                }
                MyProject = MyTiaPortal.Projects[0];
                break;

            case 0:
                txt_Status.Text     = "No running instance of TIA Portal was found!";
                btn_Connect.Enabled = true;
                return;

            default:
                txt_Status.Text     = "More than one running instance of TIA Portal was found!";
                btn_Connect.Enabled = true;
                return;
            }
            txt_Status.Text            = _tiaProcess.ProjectPath.ToString();
            btn_Start.Enabled          = false;
            btn_Connect.Enabled        = true;
            btn_Dispose.Enabled        = true;
            btn_CompileHW.Enabled      = true;
            btn_CloseProject.Enabled   = true;
            btn_SearchProject.Enabled  = false;
            btn_Save.Enabled           = true;
            btn_AddHW.Enabled          = true;
            btnCreateProject.Enabled   = false;
            btnExportBlocks.Enabled    = true;
            btnExportTagTables.Enabled = true;
            btnImportBlock.Enabled     = true;
            btnImportTagTables.Enabled = true;
            btnImportUDT.Enabled       = true;
            btnExportUDT.Enabled       = true;
            btnHWConfig.Enabled        = true;
        }
Example #4
0
        private void btn_Connect_Click(object sender, EventArgs e)
        {
            btn_Connect.Enabled = false;

            switch (processes.Count)
            {
            case 1:
                _tiaProcess = processes[0];
                MyTiaPortal = _tiaProcess.Attach();
                if (MyTiaPortal.GetCurrentProcess().Mode == TiaPortalMode.WithUserInterface)
                {
                    rdb_WithUI.Checked = true;
                }
                else
                {
                    rdb_WithoutUI.Checked = true;
                }
                if (MyTiaPortal.Projects.Count <= 0)
                {
                    txt_Status.Text     = "No TIA Portal Project was found!";
                    btn_Connect.Enabled = true;
                    return;
                }
                MyProject = MyTiaPortal.Projects[0];
                GenerateTreeView();
                tiaLibraryHelper.OpenGlobalLibrary(MyTiaPortal, textBox1);
                break;

            case 0:
                txt_Status.Text     = "No running instance of TIA Portal was found!";
                btn_Connect.Enabled = true;
                return;

            default:
                txt_Status.Text     = "More than one running instance of TIA Portal was found!";
                btn_Connect.Enabled = true;
                return;
            }
            txt_Status.Text           = _tiaProcess.ProjectPath.ToString();
            btn_startTIA.Enabled      = false;
            btn_Connect.Enabled       = true;
            btn_disposeTIA.Enabled    = true;
            btn_CompileHW.Enabled     = true;
            btn_CloseProject.Enabled  = true;
            btn_SearchProject.Enabled = false;
            btn_Save.Enabled          = true;
            btn_AddHW.Enabled         = true;
            btn_deleteChkDev.Enabled  = true;
            btn_refresh.Enabled       = true;
        }
Example #5
0
        private void btn_ConnectTIA(object sender, EventArgs e)
        {
            btn_Connect.Enabled = false;
            IList <TiaPortalProcess> processes = TiaPortal.GetProcesses();

            switch (processes.Count)
            {
            case 1:
                _tiaProcess = processes[0];
                MyTiaPortal = _tiaProcess.Attach();
                if (MyTiaPortal.GetCurrentProcess().Mode == TiaPortalMode.WithUserInterface)
                {
                    rdb_WithUI.Checked = true;
                }
                else
                {
                    rdb_WithoutUI.Checked = true;
                }
                if (MyTiaPortal.Projects.Count <= 0)
                {
                    SetTextInRichTextBox(Color.Green, "No TIA Portal Project was found!");
                    btn_Connect.Enabled = true;
                    return;
                }
                MyProject = MyTiaPortal.Projects[0];
                break;

            case 0:
                SetTextInRichTextBox(Color.Red, "No running instance of TIA Portal was found!");
                btn_Connect.Enabled = true;
                return;

            default:
                SetTextInRichTextBox(Color.Red, "More than one running instance of TIA Portal was found!");
                btn_Connect.Enabled = true;
                return;
            }
            SetTextInRichTextBox(Color.Green, $"Connected to project\n{_tiaProcess.ProjectPath.ToString()}");
            btn_Start.Enabled         = false;
            btn_Connect.Enabled       = true;
            btn_Dispose.Enabled       = true;
            btn_CloseProject.Enabled  = true;
            btn_SearchProject.Enabled = false;
            btn_Save.Enabled          = true;
        }
        private async Task InitializeConnectionWithTiaPortalProject()
        {
            await Task.Run(() =>
            {
                if (!accessAvailable)
                {
                    IList <TiaPortalProcess> processes = TiaPortal.GetProcesses();

                    switch (processes.Count)
                    {
                    case 1:
                        _tiaProcess = processes[0];
                        MyTiaPortal = _tiaProcess.Attach();
                        break;

                    case 0:
                        throw new EngineeringTargetInvocationException(
                            "No running instance of TIA Portal was found!");

                    default:
                        throw new EngineeringTargetInvocationException(
                            "More than one running instance of TIA Portal was found!");
                    }

                    if (MyTiaPortal.Projects.Count <= 0)
                    {
                        throw new EngineeringTargetInvocationException(
                            "No open TIA Portal Project was found!");
                    }

                    MyProject = MyTiaPortal.Projects[0];

                    exclusiveAccess = MyTiaPortal.ExclusiveAccess("My Activity");
                    accessAvailable = true;
                }

                if (!transactionOpen)
                {
                    transaction     = exclusiveAccess.Transaction(MyProject, "No changes commited.");
                    transactionOpen = true;
                }
            }).ConfigureAwait(true);
        }
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // if no project is open
            if (project == null)
            {
                // see if we have a open instance and use the first one
                foreach (TiaPortalProcess tiaPortalProcess in TiaPortal.GetProcesses())
                {
                    TiaPortalProcess p = TiaPortal.GetProcess(tiaPortalProcess.Id);

                    tiaPortal = p.Attach();
                    break;
                }

                // we don't habe an instance then open a new instance
                if (tiaPortal == null)
                {
                    tiaPortal = new TiaPortal(TiaPortalMode.WithUserInterface);
                }

                tiaPortal.Notification += TiaPortal_Notification;
                tiaPortal.Confirmation += TiaPortal_Confirmation;

                // let's get the projects
                ProjectComposition projects = tiaPortal.Projects;

                // no open project - then open file dialog
                if (projects.Count == 0)
                {
                    string p = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;


                    string filePath = (string)Properties.Settings.Default["PathOpenProject"];
                    if (filePath == string.Empty)
                    {
                        filePath = Application.StartupPath;
                    }

                    using (OpenFileDialog openFileDialog = new OpenFileDialog())
                    {
                        openFileDialog.Filter           = "V16 project files (*.ap16)|*.ap16|All files (*.*)|*.*";
                        openFileDialog.FilterIndex      = 1;
                        openFileDialog.InitialDirectory = filePath;

                        if (openFileDialog.ShowDialog() == DialogResult.OK)
                        {
                            Properties.Settings.Default["PathOpenProject"] = openFileDialog.FileName;
                            Properties.Settings.Default.Save();

                            // load projectpath
                            FileInfo projectPath = new FileInfo(openFileDialog.FileName);
                            try
                            {
                                project = projects.Open(projectPath);
                            }
                            catch (Exception)
                            {
                                Console.WriteLine(String.Format("Could not open project {0}", projectPath.FullName));
                                Application.Exit();
                            }
                        }
                        else
                        {
                            return; // no file selected
                        }
                    }
                }
                else
                {
                    // for now we use the first project
                    project = tiaPortal.Projects[0];
                }

                txtProject.Text = "Project: " + project.Name;

                // loop through the data
                IterateThroughDevices(project);
            }
        }
Example #8
0
        // connect to existing project
        // and read project library and devices
        private void Btn_ConnectProject_Click(object sender, EventArgs e)
        {
            // Project connection
            IList <TiaPortalProcess> processes = TiaPortal.GetProcesses();

            statusBox.AppendText("Attempting to connect..." + processes.Count + " processes found");
            statusBox.AppendText(Environment.NewLine);

            switch (processes.Count)
            {
            case 1:
                _tiaProcess = processes[0];
                MyTiaPortal = _tiaProcess.Attach();

                if (MyTiaPortal.Projects.Count <= 0)
                {
                    statusBox.AppendText("No TIA Portal Project was found!");
                    statusBox.AppendText(Environment.NewLine);
                    return;
                }
                statusBox.AppendText("Connected to running instance of TIA Portal");
                statusBox.AppendText(Environment.NewLine);
                MyProject = MyTiaPortal.Projects[0];
                break;

            case 0:
                statusBox.AppendText("No running instance of TIA Portal was found!");
                statusBox.AppendText(Environment.NewLine);
                return;

            default:
                statusBox.AppendText("More than one running instance of TIA Portal was found!");
                statusBox.AppendText(Environment.NewLine);
                return;
            }
            // Disable list boxes updating before adding new elements
            projectLibraryCheckList.BeginUpdate();
            devicesCheckList.BeginUpdate();
            blocksCheckList.BeginUpdate();

            // Read project Devices and add them to check list
            foreach (var device in MyProject.Devices)
            {
                if (device.TypeIdentifier != "System:Device.PC")
                {
                    devicesCheckList.Items.Add(device.Name);
                }
            }

            // Read project library master copies names and add them to check list
            List <String> libMasterCopies = BlockManagement.ReadProjectLibrary(MyProject.ProjectLibrary.MasterCopyFolder, "");

            foreach (string entry in libMasterCopies)
            {
                projectLibraryCheckList.Items.Add(entry);
            }

            // Read plc blocks and add them to check list
            // TO DO - Move this code into a function
            // device represents the rack
            // first element of DeviceItems (modules in the rack) is the plc
            PlcSoftware firstPlcSoftware = BlockManagement.GetSoftwareFrom(MyProject.Devices[0].DeviceItems[1]);

            List <String> blocks = new List <string>();

            if (firstPlcSoftware != null)
            {
                // Type of BlockGroup is PlcBlockSystemGroup is not compatible with type of
                // Group that is PlcBlockUserGroup so the same functions can't be applied in both cases
                // that's the reason for the exception when reading the blocks in root folder
                foreach (var block in firstPlcSoftware.BlockGroup.Blocks)
                {
                    string blockType = block.GetType().ToString();
                    blockType = blockType.Substring(blockType.LastIndexOf('.') + 1);

                    blocks.Add(block.Name + " - " + blockType + " - " + block.Number);
                }

                foreach (var group in firstPlcSoftware.BlockGroup.Groups)
                {
                    blocks.AddRange(BlockManagement.ReadBlocks(group));
                }

                blocksCheckList.Items.AddRange(blocks.ToArray());
            }

            // Enable list boxex updating after elements added
            projectLibraryCheckList.EndUpdate();
            devicesCheckList.EndUpdate();
            blocksCheckList.EndUpdate();
        }
Example #9
0
        // open project
        private void btnOpen_Click(object sender, EventArgs e)
        {
            // if no project is open
            if (project == null)
            {
                // see if we have a open instance and use the first one
                foreach (TiaPortalProcess tiaPortalProcess in TiaPortal.GetProcesses())
                {
                    TiaPortalProcess p = TiaPortal.GetProcess(tiaPortalProcess.Id);

                    tiaPortal = p.Attach();
                    Console.WriteLine("TIA Portal has been attached");
                    break;
                }

                // we don't habe an instance then open a new instance
                if (tiaPortal == null)
                {
                    tiaPortal = new TiaPortal(TiaPortalMode.WithUserInterface);
                    Console.WriteLine("TIA Portal has started");
                }

                tiaPortal.Confirmation += TiaPortal_Confirmation;
                tiaPortal.Notification += TiaPortal_Notification;

                // let's get the projects
                ProjectComposition projects = tiaPortal.Projects;

                // projects available ?
                Console.WriteLine("TIA projects " + projects.Count.ToString());

                // no open project - then open file dialog
                if (projects.Count == 0)
                {
                    Console.WriteLine("Opening Project...");

                    string filePath = string.Empty;
                    using (OpenFileDialog openFileDialog = new OpenFileDialog())
                    {
                        openFileDialog.Filter           = "V16 project files (*.ap16)|*.ap16|All files (*.*)|*.*";
                        openFileDialog.FilterIndex      = 1;
                        openFileDialog.RestoreDirectory = true;

                        if (openFileDialog.ShowDialog() == DialogResult.OK)
                        {
                            //Get the path of specified file
                            filePath = openFileDialog.FileName;

                            // load projectpath
                            FileInfo projectPath = new FileInfo(filePath);
                            try
                            {
                                project = projects.Open(projectPath);
                            }
                            catch (Exception)
                            {
                                Console.WriteLine(String.Format("Could not open project {0}", projectPath.FullName));
                                Application.Exit();
                            }
                        }
                        else
                        {
                            return; // no file selected
                        }
                    }
                }
                else
                {
                    // for now we use the first project
                    project = tiaPortal.Projects[0];
                }

                //Console.WriteLine(String.Format("Project {0} is open", project.Path.FullName));
                txtProject.Text = "Project: " + project.Name;

                // loop through the data
                IterateThroughDevices(project);
            }
        }