using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using ORiN2.interop.CAO; namespace Project { public partial class Form1 : Form { private CaoEngine caoEng; private CaoWorkspaces caoWss; private CaoWorkspace caoWs; // Robot objects private CaoController caoCtrl; // Robot Controller // Task objects private CaoTask caoTask; // Task variable private CaoVariable caoTaskST; public Form1() { InitializeComponent(); } //-----------------------------Initialization---------------------------- private void cmdInit_Click(object sender, EventArgs e) { //CreateEngine caoEng = new CaoEngine(); caoWss = caoEng.Workspaces; caoWs = caoWss.Item(0); cmdInit.Enabled = false; cmdConnectRobot.Enabled = true; } // --------------------------------------------------------------- // Connect with the Robot // --------------------------------------------------------------- private void cmdConnectRobot_Click(object sender, EventArgs e) { try { // Create object and connect with RC8 controller caoCtrl = caoWs.AddController("Robot", "CaoProv.DENSO.RC8", "", "Server=192.168.0.1"); // Task control with RC8 controller caoTask = caoCtrl.AddTask("Pro1",""); // Task class system variable caoTaskST = caoTask.AddVariable("@STATUS",""); // Enable command buttons cmdConnectRobot.Enabled = false; cmdDisconnectRobot.Enabled = true; cmdTaskStart.Enabled = true; cmdTaskStop.Enabled = true; } catch (Exception ex) { cmdDisconnectRobot_Click(sender, e); MessageBox.Show(ex.Message.ToString()); } } // --------------------------------------------------------------- // disconnect from the Robot // --------------------------------------------------------------- private void cmdDisconnectRobot_Click(object sender, EventArgs e) { // Disconnect from the Task Variable if (caoTaskST != null) { if (caoTask != null) { caoTask.Variables.Remove(caoTaskST.Name); } Marshal.ReleaseComObject(caoTaskST); caoTaskST = null; } // Disconnect from the Task if(caoTask != null) { if (caoCtrl != null) { caoCtrl.Tasks.Remove(caoTask.Name); } Marshal.ReleaseComObject(caoTask); caoTask = null; } // Disconnect from the controller if (caoCtrl != null) { if (caoWs != null) { caoWs.Controllers.Remove(caoCtrl.Name); } Marshal.ReleaseComObject(caoCtrl); caoCtrl = null; } // Disable command buttons cmdConnectRobot.Enabled = true; cmdDisconnectRobot.Enabled = false; cmdTaskStart.Enabled = false; cmdTaskStop.Enabled = false; } private void cmdTaskStart_Click(object sender, EventArgs e) { try { // caoTaskST [0:Task not yet generated (NON_EXISTENT), 1:Hold-stopped, 2:Stopped, 3:Running, 4:Step-stopped] if(Int32.Parse(caoTaskST.Value.ToString()) != 3) { // Start [1:One cycle execution, 2:Continuous execution, 3:Step forward] caoTask.Start(2,""); } } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } private void cmdTaskStop_Click(object sender, EventArgs e) { try { // caoTaskST [0:Task not yet generated (NON_EXISTENT), 1:Hold-stopped, 2:Stopped, 3:Running, 4:Step-stopped] if (Int32.Parse(caoTaskST.Value.ToString()) == 3) { // Stop [0:Default stop, 1:Instant stop, 2:Step stop, 3:Cycle stop, 4:Initialized stop] caoTask.Stop(4,""); } } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { cmdDisconnectRobot_Click(sender, e); // Delete CaoEngine if (caoWs != null) { Marshal.ReleaseComObject(caoWs); caoWs = null; } if (caoWss != null) { Marshal.ReleaseComObject(caoWss); caoWss = null; } if (caoEng != null) { Marshal.ReleaseComObject(caoEng); caoEng = null; } } } }