using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using ORiN2.ManagedCAO; namespace NJSample { public partial class FrmMain : Form { private CCaoController _ctrl = null; public FrmMain() { InitializeComponent(); UpdateVariableArea(); } private void CreateTree() { VarInfo.RootVariableInfo info = new NJSample.VarInfo.RootVariableInfo(); info.Create(_ctrl); trvVariable.Nodes.Clear(); UpdateVariableArea(); trvVariable.Nodes.Add(info.CreateNode()); } private void btnConnect_Click(object sender, EventArgs e) { try { if (_ctrl != null) { caoEngine.Workspaces[0].Controllers.Remove(_ctrl.Index); _ctrl = null; } _ctrl = caoEngine.Workspaces[0].AddController("NJ", "CaoProv.OMRON.NJ", null, txtOption.Text); CreateTree(); } catch (Exception ex) { MessageBox.Show(this, ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void UpdateVariableArea() { txtGetValue.Text = string.Empty; txtGetValueType.Text = string.Empty; txtPutValue.Text = string.Empty; txtCIPType.Text = string.Empty; txtVariantType.Text = string.Empty; bool editable = false; VarInfo.VariableInfo info = null; if (trvVariable.SelectedNode != null) { info = trvVariable.SelectedNode.Tag as VarInfo.VariableInfo; } if (info != null) { txtVarName.Text = info.GetVariableAccessName(); txtCIPType.Text = info.CIPType.ToString(); txtVariantType.Text = info.VarType.ToString(); //editable = info.HasValue; // for test editable = !string.IsNullOrEmpty(info.GetVariableAccessName()); } else { txtVarName.Text = string.Empty; } txtPutValue.ReadOnly = !editable; btnPutValue.Enabled = editable; btnPutValueCtrl.Enabled = editable; btnGetValue.Enabled = editable; btnGetValueCtrl.Enabled = editable; } private void trvVariable_AfterSelect(object sender, TreeViewEventArgs e) { UpdateVariableArea(); } private void btnGetValue_Click(object sender, EventArgs e) { try { VarInfo.VariableInfo info = null; if (trvVariable.SelectedNode != null) { info = trvVariable.SelectedNode.Tag as VarInfo.VariableInfo; } if (info != null /* && info.HasValue*/ ) { VarInfo.GetValueResult res = info.GetValue(); string tp = res.Type.Name; if (res.Value is Array) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < ((Array)res.Value).Length; i++) { if (i > 0) { sb.Append(","); } else { tp = ((Array)res.Value).GetValue(i).GetType().Name + "[]"; } sb.Append(Convert.ToString(((Array)res.Value).GetValue(i))); } txtGetValue.Text = sb.ToString(); } else { txtGetValue.Text = Convert.ToString(res.Value); } txtGetValueType.Text = tp; //txtCIPType.Text = obj == null ? "(null)" : obj.GetType().Name; } } catch (Exception ex) { MessageBox.Show(this, ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void btnPutValue_Click(object sender, EventArgs e) { try { VarInfo.VariableInfo info = null; if (trvVariable.SelectedNode != null) { info = trvVariable.SelectedNode.Tag as VarInfo.VariableInfo; } if (info != null /*&& info.HasValue*/) { info.PutValue(GetPutValue(info)); } } catch (Exception ex) { MessageBox.Show(this, ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void btnGetValueCtrl_Click(object sender, EventArgs e) { try { VarInfo.VariableInfo info = null; if (trvVariable.SelectedNode != null) { info = trvVariable.SelectedNode.Tag as VarInfo.VariableInfo; } if (info != null /*&& info.HasValue*/) { object obj = _ctrl.Execute("GetValue", info.GetVariableAccessName()); string tp = obj.GetType().Name; if (obj is Array) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < ((Array)obj).Length; i++) { if (i > 0) { sb.Append(","); } else { tp = ((Array)obj).GetValue(i).GetType().Name + "[]"; } sb.Append(Convert.ToString(((Array)obj).GetValue(i))); } txtGetValue.Text = sb.ToString(); } else { txtGetValue.Text = Convert.ToString(obj); } txtGetValueType.Text = tp; //txtCIPType.Text = obj == null ? "(null)" : obj.GetType().Name; } } catch (Exception ex) { MessageBox.Show(this, ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void btnPutValueCtrl_Click(object sender, EventArgs e) { try { VarInfo.VariableInfo info = null; if (trvVariable.SelectedNode != null) { info = trvVariable.SelectedNode.Tag as VarInfo.VariableInfo; } if (info != null /*&& info.HasValue*/) { _ctrl.Execute("PutValue", new object[]{ info.GetVariableAccessName(), GetPutValue(info) }); } } catch (Exception ex) { MessageBox.Show(this, ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void FrmMain_Load(object sender, EventArgs e) { CCaoWorkspace ws = caoEngine.Workspaces[0]; } private object GetPutValue(VarInfo.VariableInfo info) { VarInfo.ArrayVariableInfo arrInfo = info as VarInfo.ArrayVariableInfo; if (!chkConvertClient.Checked) { return arrInfo == null ? (object)txtPutValue.Text : (object)txtPutValue.Text.Split(','); } object obj = txtPutValue.Text; if (arrInfo != null) { obj = txtPutValue.Text.Split(','); } else { if (info.CIPType == CIPType.STRING) { // do nothing } else if (info.CIPType == CIPType.ULINT || info.CIPType == CIPType.DATE_AND_TIME_NSEC || info.CIPType == CIPType.DATE_NSEC || info.CIPType == CIPType.TIME_NSEC || info.CIPType == CIPType.TIME_OF_DAY_NSEC) { obj = Convert.ToUInt64(obj); } else if (info.CIPType == CIPType.REAL || info.CIPType == CIPType.LREAL) { obj = Convert.ToDouble(obj); } else { obj = Convert.ToInt64(obj); } } return obj; } } }