using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; // for ORiN using ORiN2.interop.CAO; using System.Runtime.InteropServices; namespace Task { public partial class FrmTask : Form { // Cao objects private CaoEngine m_caoEng; private CaoWorkspaces m_caoWss; private CaoWorkspace m_caoWs; // Cao objects for controlling robot controller private CaoControllers m_caoCtrls; private CaoController m_caoCtrl; private CaoTasks m_caoTsks; public FrmTask() { InitializeComponent(); } private void FrmTask_Load(object sender, EventArgs e) { // Initialize form cboParam.SelectedIndex = 0; cboStart.SelectedIndex = 0; cboStop.SelectedIndex = 0; try { // Create CaoEngine m_caoEng = new CaoEngine(); //Get CaoWorkspaces m_caoWss = m_caoEng.Workspaces; // Get CaoWorkspace m_caoWs = m_caoEng.Workspaces.Item(0); // Get CaoControllers m_caoCtrls = m_caoWs.Controllers; // Initialize cao objects as null m_caoCtrl = null; m_caoTsks = null; //Button and timer off ControlEnabled(false); } catch (Exception ex) { MessageBox.Show(ex.Message); } } /* * Control components * * bEnabled = true: Enable components * bEnabled = false; Disable components * */ private void ControlEnabled(Boolean bEnable) { //timer on/off timer1.Enabled = bEnable; //button on/off btnRefresh.Enabled = bEnable; btnStart.Enabled = bEnable; btnStop.Enabled = bEnable; } private void FrmTask_FormClosing(object sender, FormClosingEventArgs e) { try { Disconnect(); //Release Workspace System.Runtime.InteropServices.Marshal.ReleaseComObject(m_caoWs); m_caoWs = null; //Release Workspaces System.Runtime.InteropServices.Marshal.ReleaseComObject(m_caoWss); m_caoWss = null; //Release Engine System.Runtime.InteropServices.Marshal.ReleaseComObject(m_caoEng); m_caoEng = null; } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void btnExit_Click(object sender, EventArgs e) { this.Close(); } private void btnRefresh_Click(object sender, EventArgs e) { Refreshment(); } private void Refreshment() { //reset caotasks foreach(CaoTask Tsk in m_caoTsks) { m_caoTsks.Remove(Tsk.Index); } object[] names; names = (object[])m_caoCtrl.get_TaskNames(""); for(int i = 0; i < names.Length ; i++) { //set task CaoTask caoTsk; caoTsk = m_caoCtrl.AddTask((string)names[i],""); String[] strArray = new String[] { "@STATUS", "@LINE_NO", "@CYCLE_TIME", "@PRIORITY" }; foreach (String strName in strArray) { CaoVariable caoVar = caoTsk.AddVariable(strName, ""); System.Runtime.InteropServices.Marshal.ReleaseComObject(caoVar); } System.Runtime.InteropServices.Marshal.ReleaseComObject(caoTsk); } dataTask.Rows.Clear(); dataTask.Rows.Add(names.Length); timer1_Tick(new object(), new EventArgs()); } // Connect controller private void btnConn_Click(object sender, EventArgs e) { try { string sConn; Disconnect(); sConn = cboParam.Text; m_caoCtrl = m_caoWs.AddController("", "CaoProv.DENSO.RC8", "", sConn); m_caoTsks = m_caoCtrl.Tasks; Refreshment(); ControlEnabled(true); // Enable components } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } // Disconnect controller private void Disconnect() { try { if (m_caoTsks != null) { m_caoTsks.Clear(); System.Runtime.InteropServices.Marshal.ReleaseComObject(m_caoTsks); m_caoTsks = null; } // Release controller object if (m_caoCtrl != null) { //OK // 1. CaoControllersオブジェクトから対象コントローラの参照を取り除く m_caoCtrls.Remove(m_caoCtrl.Name); // 2. 対象コントローラの参照を取り除く System.Runtime.InteropServices.Marshal.ReleaseComObject(m_caoCtrl); m_caoCtrl = null; } ControlEnabled(false); // Disable components dataTask.Rows.Clear(); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } private void btnDisconn_Click(object sender, EventArgs e) { Disconnect(); } // Task start private void btnStart_Click(object sender, EventArgs e) { try { // Start mode 1: One cycle execution, 2: Continuous execution, 3: Step forward int Mode; Mode = cboStart.SelectedIndex + 1; // Add 1 because start mode begins with 1. CaoTask caoTsk; caoTsk = m_caoCtrl.Tasks.Item(dataTask.CurrentCell.RowIndex); caoTsk.Start(Mode, ""); System.Runtime.InteropServices.Marshal.ReleaseComObject(caoTsk); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } // Task stop private void btnStop_Click(object sender, EventArgs e) { try { // Stop mode 0: Default stop, 1: Instant stop, 2: Step stop, 3: Cycle stop, 4: Initialized stop int Mode; Mode = cboStop.SelectedIndex; CaoTask caoTsk; caoTsk = m_caoCtrl.Tasks.Item(dataTask.CurrentCell.RowIndex); caoTsk.Stop(Mode, ""); System.Runtime.InteropServices.Marshal.ReleaseComObject(caoTsk); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } //Monitor the task status private void timer1_Tick(object sender, EventArgs e) { for (int i = 0; i < m_caoTsks.Count; i++) { CaoTask caoTsk; caoTsk = m_caoTsks.Item(i); dataTask.Rows[i].Cells[0].Value = caoTsk.Name; CaoVariables caoVars = caoTsk.Variables; for (int j = 0; j < caoVars.Count; j++) { CaoVariable caoVar = caoVars.Item(j); dataTask.Rows[i].Cells[j+1].Value = caoVar.Value; System.Runtime.InteropServices.Marshal.ReleaseComObject(caoVar); } System.Runtime.InteropServices.Marshal.ReleaseComObject(caoVars); System.Runtime.InteropServices.Marshal.ReleaseComObject(caoTsk); } } } }