C#¶
開発環境の設定¶
-
Ydx.cs をプロジェクトフォルダにコピーします。
-
Ydx.cs をプロジェクトに追加します。
-
ソースファイルにusing ディレクティブを使ってYdxCsを宣言します。
using YdxCs;
コントロール¶
変数¶
private int id;
実行結果の表示¶
private void ResultShow(string title, int resultCode)
{
string resultString;
Ydx.CnvResultToString(resultCode, out resultString);
switch (resultCode)
{
case 0:
case Ydx.YDX_RESULT_DI_EXCEED_DATA_NUM:
case Ydx.YDX_RESULT_DI_EXCEED_BUF_SIZ:
MessageBox.Show(resultString, title, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
break;
default:
MessageBox.Show(resultString, title, MessageBoxButtons.OK, MessageBoxIcon.Hand);
break;
}
}
フォームロード¶
private void Form1_Load(object sender, EventArgs e)
{
// ユニット識別スイッチ
unitSwitchComboBox.ResetText();
unitSwitchComboBox.Items.AddRange(new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" });
unitSwitchComboBox.SelectedIndex = 0;
// 型名
modelNameComboBox.ResetText();
modelNameComboBox.Items.AddRange(new string[] { "DIO-16/16C-USC", "DIO-16/16D-UBC", "DIO-16/16D-USC" });
modelNameComboBox.SelectedIndex = 0;
// 入力チャネル
inputChannelComboBox.ResetText();
inputChannelComboBox.Items.AddRange(new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15" });
inputChannelComboBox.SelectedIndex = 0;
// 出力チャネル
outputChannelComboBox.ResetText();
outputChannelComboBox.Items.AddRange(new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15" });
outputChannelComboBox.SelectedIndex = 0;
// 出力データ
outputDataComboBox.ResetText();
outputDataComboBox.Items.AddRange(new string[] { "0", "1" });
outputDataComboBox.SelectedIndex = 0;
}
オープン¶
private void openButton_Click(object sender, EventArgs e)
{
int result = Ydx.Open(unitSwitchComboBox.SelectedIndex, modelNameComboBox.Text, 0, out id);
if(result != 0)
ResultShow("YdxOpen", result);
else
{
unitSwitchComboBox.Enabled = false;
modelNameComboBox.Enabled = false;
ResultShow("オープン", result);
}
}
入力¶
private void inputButton_Click(object sender, EventArgs e)
{
int[] data = new int[1];
int result = Ydx.DiInputBit(id, inputChannelComboBox.SelectedIndex, 1, 0, data);
if(result != 0)
ResultShow("YdxDiInputBit", result);
else
MessageBox.Show("データ : " + data[0].ToString(), "入力", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
出力¶
private void outputButton_Click(object sender, EventArgs e)
{
int[] data = new int[1];
data[0] = outputDataComboBox.SelectedIndex;
int result = Ydx.DoOutputBit(id, outputChannelComboBox.SelectedIndex, 1, data);
ResultShow("出力", result);
}
クローズ¶
private void closeButton_Click(object sender, EventArgs e)
{
unitSwitchComboBox.Enabled = true;
modelNameComboBox.Enabled = true;
int result = Ydx.Close(id);
if(result != 0)
ResultShow("YdxClose", result);
else
ResultShow("クローズ", result);
}
フォームクローズ¶
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
int result = Ydx.Close(id);
if((result != 0) && (result != Ydx.YDX_RESULT_NOT_OPEN))
{
ResultShow("YdxClose", result);
}
}