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