using ORiN2.ManagedCAO; using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Windows.Forms; namespace Sample { #region Enums /// /// Register address capacity type /// public enum Bits { SixteenBit = 16, ThirtyTwoBit = 32, } /// /// Progress /// public enum ProgressType { /// /// Not connected /// Disconnection, /// /// Connection Status /// Connection, /// /// Data accessible state /// DataAccess, } /// /// Register type with 16-bit address capacity /// public enum SixTeenRegisterTypes { /// /// Common data register /// M, /// /// Input register /// I, } /// /// Register type with address capacity of 32 bits /// public enum ThirtyTwoRegisterTypes { /// /// System register /// S, /// /// Common data register /// M, /// /// Common global register /// G, /// /// Input register /// I, /// /// Output register /// O, } /// /// Data type /// public enum DataTypes { /// /// Bit type /// B, /// /// Integer type /// W, /// /// Long integer type /// L, /// /// 4-precision integer type /// Q, /// /// Single precision real type /// F, /// /// Double precision real type /// D, } /// /// VT data type /// public enum VTDataTypes { /// /// VT_BOOL /// BOOL, /// /// VT_I1 /// I1, /// /// VT_UI1 /// UI1, /// /// VT_I2 /// I2, /// /// VT_UI2 /// UI2, /// /// VT_I4 /// I4, /// /// VT_UI4 /// UI4, /// /// VT_I8 /// I8, /// /// VT_UI8 /// UI8, /// /// VT_R4 /// R4, /// /// VT_R8 /// R8, /// /// VT_BSTR /// BSTR, } #endregion public partial class MainForm : Form { #region Feilds /// /// Cao engine /// public CCaoEngine caoEngine = null; /// /// Cao workspace /// private CCaoWorkspace caoWorkspace = null; /// /// Cao controller /// private CCaoController caoController = null; /// /// Cao variable /// private CCaoVariable caoVariable = null; #endregion #region Constructors /// /// Constructor /// public MainForm() { InitializeComponent(); this.caoEngine = new CCaoEngine(); this.caoWorkspace = this.caoEngine.AddWorkspace("SampleWorkspace", ""); this.noRandomAccessRadioButton.Checked = true; this.randomAccessRadioButton.Checked = false; this.nonRandomAccessSettingPanel.Visible = true; this.randomAccessSettingPanel.Visible = false; this.bitComboBox.SelectedIndex = 0; this.UpdateEnable(ProgressType.Disconnection); this.UpdateAccessSettingPanelVisible(); this.UpdateDisplaySize(); } /// /// Destructor /// ~MainForm() { // Delete all from Engine this.caoEngine.Dispose(); this.caoEngine = null; } #endregion #region Event /// /// When the connect button is pressed /// /// /// private void connectButton_Click(object sender, EventArgs e) { String optionString = String.Empty; if (Convert.ToString(this.bitComboBox.SelectedValue) == "16Bit") { optionString = String.Format("Conn=TCP:{0}:{1},Timeout={2},Bit=16,CPUNo={3}", this.ipAddressTextBox.Text, this.portNoNumericUpDown.Value, this.timeoutNumericUpDown.Value, this.cpuNoNumericUpDown.Value); } else { optionString = String.Format("Conn=TCP:{0}:{1},Timeout={2},Bit=32,CPUNo={3}", this.ipAddressTextBox.Text, this.portNoNumericUpDown.Value, this.timeoutNumericUpDown.Value, this.cpuNoNumericUpDown.Value); } try { // Add Controller to Workspace this.caoController = this.caoWorkspace.AddController("Ctrl", "CaoProv.YASKAWA.ExMEMOBUS", "", optionString); this.UpdateEnable(ProgressType.Connection); } catch (Exception error) { System.Windows.Forms.MessageBox.Show(error.Message); } } /// /// When the disconnect button is pressed /// /// /// private void disconnectButton_Click(object sender, EventArgs e) { // Remove Vable from Controller if (this.caoVariable != null) { this.caoController.Variables.Remove(this.caoVariable.Index); this.caoVariable = null; } // Remove Controller from Workspace if (this.caoController != null) { this.caoWorkspace.Controllers.Remove(this.caoController.Index); this.caoController = null; } this.UpdateEnable(ProgressType.Disconnection); } /// /// When the confirm button is pressed /// /// /// private void confirmButton_Click(object sender, EventArgs e) { String optionString = String.Empty; if (this.noRandomAccessRadioButton.Checked) { optionString = this.nonRandomAccessSettingPanel.GetOptionString(); } else { optionString = this.randomAccessSettingPanel.GetOptionString(); if (this.randomAccessSettingPanel.GetElementCount() < 2) { System.Windows.Forms.MessageBox.Show("Set two or more registers to access."); return; } } try { // Add Variable to Controller this.caoVariable = this.caoController.AddVariable("var", optionString); UpdateEnable(ProgressType.DataAccess); } catch (Exception error) { System.Windows.Forms.MessageBox.Show(error.Message); } } /// /// When the edit button is pressed /// /// /// private void editButton_Click(object sender, EventArgs e) { // Remove Vable from Controller if (this.caoVariable != null) { this.caoController.Variables.Remove(this.caoVariable.Index); this.caoVariable = null; } this.UpdateEnable(ProgressType.Connection); } /// /// When the Get button is pressed /// /// /// private void getButton_Click(object sender, EventArgs e) { try { object data = this.caoVariable.Value; Type type = data.GetType(); if (type.IsArray) { this.dataTextBox.Text = this.ConvertArrayDataToString(data); } else if (type == typeof(String)) { this.dataTextBox.Text = data as String; } else { this.dataTextBox.Text = data.ToString(); } this.UpdateGetDataType(); } catch(Exception error) { System.Windows.Forms.MessageBox.Show(error.Message); } } /// /// When the Put button is pressed /// /// /// private void putButton_Click(object sender, EventArgs e) { UInt64 elemCount = 0; if (this.noRandomAccessRadioButton.Checked) { elemCount = this.nonRandomAccessSettingPanel.GetElementCount(); if (elemCount > 1) { this.SetArrayData(this.nonRandomAccessSettingPanel.GetVTDataType()); } else { this.SetData(this.nonRandomAccessSettingPanel.GetVTDataType()); } } else { elemCount = this.randomAccessSettingPanel.GetElementCount(); this.SetArrayData(); } } /// /// When the access method radio button is pressed /// /// /// private void AccessTypeRadioButton_Click(object sender, EventArgs e) { this.UpdateAccessSettingPanelVisible(); this.UpdateDisplaySize(); } private void bitComboBox_SelectedIndexChanged(object sender, EventArgs e) { if (Convert.ToString(this.bitComboBox.SelectedValue) == "16Bit") { this.nonRandomAccessSettingPanel.UpdateRegisterInfo(Bits.SixteenBit); this.randomAccessSettingPanel.UpdateRegisterInfo(Bits.SixteenBit); } else { this.nonRandomAccessSettingPanel.UpdateRegisterInfo(Bits.ThirtyTwoBit); this.randomAccessSettingPanel.UpdateRegisterInfo(Bits.ThirtyTwoBit); } } #endregion #region Methods /// /// Update Display Enabled /// /// Progress private void UpdateEnable(ProgressType status) { switch (status) { case ProgressType.Disconnection: this.settingGroupBox.Enabled = false; this.getputGroupBox.Enabled = false; this.connectPanel.Enabled = true; this.connectButton.Enabled = true; this.disconnectButton.Enabled = false; break; case ProgressType.Connection: this.connectPanel.Enabled = false; this.connectButton.Enabled = false; this.settingGroupBox.Enabled = true; this.getputGroupBox.Enabled = false; this.accessGroupBox.Enabled = true; this.nonRandomAccessSettingPanel.Enabled = true; this.randomAccessSettingPanel.Enabled = true; this.disconnectButton.Enabled = true; this.confirmButton.Enabled = true; this.editButton.Enabled = false; break; case ProgressType.DataAccess: this.nonRandomAccessSettingPanel.Enabled = false; this.randomAccessSettingPanel.Enabled = false; this.accessGroupBox.Enabled = false; this.editButton.Enabled = true; this.confirmButton.Enabled = false; this.getputGroupBox.Enabled = true; break; } } /// /// Switching the address reference method /// /// /// private void UpdateAccessSettingPanelVisible() { if (this.noRandomAccessRadioButton.Checked) { this.nonRandomAccessSettingPanel.Visible = true; this.randomAccessSettingPanel.Visible = false; } else { this.nonRandomAccessSettingPanel.Visible = false; this.randomAccessSettingPanel.Visible = true; } } /// /// Display size change /// /// private void UpdateDisplaySize() { if (this.noRandomAccessRadioButton.Checked) { this.confirmButton.Location = new Point(104, 226); this.editButton.Location = new Point(249, 226); this.tableLayoutPanel.RowStyles[1].Height = 260; this.Size = new Size(474, 570); this.MinimumSize = new Size(474, 570); this.MaximumSize = new Size(474, 570); } else { this.confirmButton.Location = new Point(104, 430); this.editButton.Location = new Point(249, 430); this.tableLayoutPanel.RowStyles[1].Height = 465; this.Size = new Size(474, 773); this.MinimumSize = new Size(474, 773); this.MaximumSize = new Size(474, 773); } } /// /// Set array data /// /// Elem count private void SetArrayData() { List datas = this.dataTextBox.Text.Split(',').ToList(); List vtDataTypes = this.randomAccessSettingPanel.GetVtDataTypeList(); List setDatas = new List(); int index = 0; foreach (string vtDataType in vtDataTypes) { switch (vtDataType) { case "BSTR": setDatas.Add(datas[index]); break; case "BOOL": setDatas.Add(Convert.ToBoolean(datas[index])); break; case "I1": setDatas.Add(Convert.ToInt16(datas[index])); break; case "UI1": setDatas.Add(Convert.ToUInt16(datas[index])); break; case "I2": setDatas.Add(Convert.ToInt32(datas[index])); break; case "UI2": setDatas.Add(Convert.ToUInt32(datas[index])); break; case "I4": setDatas.Add(Convert.ToInt32(datas[index])); break; case "UI4": setDatas.Add(Convert.ToUInt32(datas[index])); break; case "I8": setDatas.Add(Convert.ToInt64(datas[index])); break; case "UI8": setDatas.Add(Convert.ToUInt64(datas[index])); break; case "R4": setDatas.Add(Convert.ToSingle(datas[index])); break; case "R8": setDatas.Add(Convert.ToDouble(datas[index])); break; } index++; } try { this.caoVariable.Value = setDatas; } catch (Exception error) { System.Windows.Forms.MessageBox.Show(error.Message); } } /// /// Set array data(Other than string data type) /// /// Elem count private void SetArrayData(VTDataTypes vtDataType) { List datas = this.dataTextBox.Text.Split(',').ToList(); try { switch (vtDataType) { case VTDataTypes.BSTR: this.caoVariable.Value = this.dataTextBox.Text; break; case VTDataTypes.BOOL: List boolDatas = new List(); foreach (string data in datas) { boolDatas.Add(Convert.ToBoolean(data)); } this.caoVariable.Value = boolDatas; break; case VTDataTypes.I1: List i1Datas = new List(); foreach (string data in datas) { i1Datas.Add(Convert.ToInt16(data)); } this.caoVariable.Value = i1Datas; break; case VTDataTypes.UI1: List ui1Datas = new List(); foreach (string data in datas) { ui1Datas.Add(Convert.ToUInt16(data)); } this.caoVariable.Value = ui1Datas; break; case VTDataTypes.I2: List i2Datas = new List(); foreach (string data in datas) { i2Datas.Add(Convert.ToInt32(data)); } this.caoVariable.Value = i2Datas; break; case VTDataTypes.UI2: List ui2Datas = new List(); foreach (string data in datas) { ui2Datas.Add(Convert.ToUInt32(data)); } this.caoVariable.Value = ui2Datas; break; case VTDataTypes.I4: List i4Datas = new List(); foreach (string data in datas) { i4Datas.Add(Convert.ToInt32(data)); } this.caoVariable.Value = i4Datas; break; case VTDataTypes.UI4: List ui4Datas = new List(); foreach (string data in datas) { ui4Datas.Add(Convert.ToUInt32(data)); } this.caoVariable.Value = ui4Datas; break; case VTDataTypes.I8: List i8Datas = new List(); foreach (string data in datas) { i8Datas.Add(Convert.ToInt64(data)); } this.caoVariable.Value = i8Datas; break; case VTDataTypes.UI8: List ui8Datas = new List(); foreach (string data in datas) { ui8Datas.Add(Convert.ToUInt64(data)); } this.caoVariable.Value = ui8Datas; break; case VTDataTypes.R4: List r4Datas = new List(); foreach (string data in datas) { r4Datas.Add(Convert.ToSingle(data)); } this.caoVariable.Value = r4Datas; break; case VTDataTypes.R8: List r8Datas = new List(); foreach (string data in datas) { r8Datas.Add(Convert.ToDouble(data)); } this.caoVariable.Value = r8Datas; break; } } catch (Exception error) { System.Windows.Forms.MessageBox.Show(error.Message); } } /// /// Set the data /// private void SetData(VTDataTypes vtDataType) { try { switch (vtDataType) { case VTDataTypes.BSTR: this.caoVariable.Value = this.dataTextBox.Text; break; case VTDataTypes.BOOL: this.caoVariable.Value = Convert.ToBoolean(this.dataTextBox.Text); break; case VTDataTypes.I1: this.caoVariable.Value = Convert.ToInt16(this.dataTextBox.Text); break; case VTDataTypes.UI1: this.caoVariable.Value = Convert.ToUInt16(this.dataTextBox.Text); break; case VTDataTypes.I2: this.caoVariable.Value = Convert.ToInt32(this.dataTextBox.Text); break; case VTDataTypes.UI2: this.caoVariable.Value = Convert.ToUInt32(this.dataTextBox.Text); break; case VTDataTypes.I4: this.caoVariable.Value = Convert.ToInt32(this.dataTextBox.Text); break; case VTDataTypes.UI4: this.caoVariable.Value = Convert.ToUInt32(this.dataTextBox.Text); break; case VTDataTypes.I8: this.caoVariable.Value = Convert.ToInt64(this.dataTextBox.Text); break; case VTDataTypes.UI8: this.caoVariable.Value = Convert.ToUInt64(this.dataTextBox.Text); break; case VTDataTypes.R4: this.caoVariable.Value = Convert.ToSingle(this.dataTextBox.Text); break; case VTDataTypes.R8: this.caoVariable.Value = Convert.ToDouble(this.dataTextBox.Text); break; } } catch (Exception error) { System.Windows.Forms.MessageBox.Show(error.Message); } } /// /// Convert the obtained array data to a character string /// private string ConvertArrayDataToString(object data) { string dataString = string.Empty; if (data is object[]) { object[] datas = data as object[]; bool isFrist = true; foreach (object item in datas) { if (!isFrist) { dataString += ","; } else { isFrist = false; } dataString += item.ToString(); } } else if (data is string[]) { string[] datas = data as string[]; bool isFrist = true; foreach (object item in datas) { if (!isFrist) { dataString += ","; } else { isFrist = false; } dataString += item.ToString(); } } else if (data is short[]) { short[] datas = data as short[]; bool isFrist = true; foreach (object item in datas) { if (!isFrist) { dataString += ","; } else { isFrist = false; } dataString += item.ToString(); } } else if (data is char[]) { char[] datas = data as char[]; bool isFrist = true; foreach (object item in datas) { if (!isFrist) { dataString += ","; } else { isFrist = false; } dataString += item.ToString(); } } else if (data is byte[]) { byte[] datas = data as byte[]; bool isFrist = true; foreach (object item in datas) { if (!isFrist) { dataString += ","; } else { isFrist = false; } dataString += item.ToString(); } } else if (data is int[]) { int[] datas = data as int[]; bool isFrist = true; foreach (object item in datas) { if (!isFrist) { dataString += ","; } else { isFrist = false; } dataString += item.ToString(); } } else if (data is Int64[]) { Int64[] datas = data as Int64[]; bool isFrist = true; foreach (object item in datas) { if (!isFrist) { dataString += ","; } else { isFrist = false; } dataString += item.ToString(); } } else if (data is UInt64[]) { UInt64[] datas = data as UInt64[]; bool isFrist = true; foreach (object item in datas) { if (!isFrist) { dataString += ","; } else { isFrist = false; } dataString += item.ToString(); } } else if (data is bool[]) { bool[] datas = data as bool[]; bool isFrist = true; foreach (object item in datas) { if (!isFrist) { dataString += ","; } else { isFrist = false; } dataString += item.ToString(); } } else if (data is float[]) { float[] datas = data as float[]; bool isFrist = true; foreach (object item in datas) { if (!isFrist) { dataString += ","; } else { isFrist = false; } dataString += item.ToString(); } } else if (data is double[]) { double[] datas = data as double[]; bool isFrist = true; foreach (object item in datas) { if (!isFrist) { dataString += ","; } else { isFrist = false; } dataString += item.ToString(); } } return dataString; } /// /// Update display of acquired data type /// private void UpdateGetDataType() { string getDataTypeString = string.Empty; if (this.noRandomAccessRadioButton.Checked) { if (this.nonRandomAccessSettingPanel.GetElemCount() > 1) { getDataTypeString += "VT_ARRAY | "; } else { if (this.nonRandomAccessSettingPanel.GetIsArray()) { getDataTypeString += "VT_ARRAY | "; } } switch (this.nonRandomAccessSettingPanel.GetVTDataType()) { case VTDataTypes.BOOL: getDataTypeString += "VT_BOOL"; break; case VTDataTypes.BSTR: getDataTypeString += "VT_BSTR"; break; case VTDataTypes.I1: getDataTypeString += "VT_I1"; break; case VTDataTypes.UI1: getDataTypeString += "VT_UI1"; break; case VTDataTypes.I2: getDataTypeString += "VT_I2"; break; case VTDataTypes.UI2: getDataTypeString += "VT_UI2"; break; case VTDataTypes.I4: getDataTypeString += "VT_I4"; break; case VTDataTypes.UI4: getDataTypeString += "VT_UI4"; break; case VTDataTypes.I8: getDataTypeString += "VT_I8"; break; case VTDataTypes.UI8: getDataTypeString += "VT_UI8"; break; case VTDataTypes.R4: getDataTypeString += "VT_R4"; break; case VTDataTypes.R8: getDataTypeString += "VT_R8"; break; default: getDataTypeString += "VT_VARIANT"; break; } } else { getDataTypeString = "VT_ARRAY | VT_VARIANT"; } this.getDataTypeLabel.Text = getDataTypeString; } #endregion } }