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_AI_EXCEED_DATA_NUM:
case Ydx.YDX_RESULT_AI_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[] { "AIO-64/4/1B-USC", "AIO-60/4/1B-USC" });
modelNameComboBox.SelectedIndex = 0;
// レンジ
rangeTypeComboBox.ResetText();
rangeTypeComboBox.Items.AddRange(new string[] { "-10 ~ +10V", "-5 ~ +5V" });
}
オープン¶
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;
rangeTypeComboBox.SelectedIndexChanged -= new EventHandler(rangeTypeComboBox_SelectedIndexChanged);
rangeTypeComboBox.SelectedIndex = 0;
rangeTypeComboBox.SelectedIndexChanged += new EventHandler(rangeTypeComboBox_SelectedIndexChanged);
ResultShow("オープン", result);
}
}
レンジ切り替え¶
private void rangeTypeComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
int result = Ydx.AiSetRange(id, rangeTypeComboBox.SelectedIndex);
if (result != 0)
ResultShow("YdxAiSetRange", result);
}
アナログ入力¶
private void inputButton_Click(object sender, EventArgs e)
{
dataTextBox.ResetText();
Application.DoEvents();
const int CHANNEL_NUM = 6;
float[] data = new float[CHANNEL_NUM];
int result = Ydx.AiInputVolt(id, 0, CHANNEL_NUM, data);
if (result != 0)
{
ResultShow("YdxAiInputVolt", result);
return;
}
string txt = "";
for (int channel = 0; channel < CHANNEL_NUM; channel++)
{
txt += "CH" + channel.ToString() + " : " + data[channel].ToString(" 0.000;-0.000").PadLeft(7) + "V" + Environment.NewLine;
}
dataTextBox.Text = txt;
}
クローズ¶
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);
}
}