C++/CLI¶
開発環境の設定¶
-
YdxCLI.h をプロジェクトフォルダにコピーします。
-
YdxCLI.h をプロジェクトに追加します。
-
ソースファイルに YdxCLI.h をインクルードします。
-
usingディレクティブを使ってYdxCLIを宣言します。
コントロール¶
変数¶
int id;
実行結果の表示¶
private: System::Void ResultShow(String^ title, int resultCode)
{
StringBuilder ^resultString = gcnew StringBuilder(256);
YdxCnvResultToString(resultCode, resultString);
switch (resultCode)
{
case 0:
case YDX_RESULT_AI_EXCEED_DATA_NUM:
case YDX_RESULT_AI_EXCEED_BUF_SIZ:
MessageBox::Show(resultString->ToString(), title, MessageBoxButtons::OK, MessageBoxIcon::Asterisk);
break;
default:
MessageBox::Show(resultString->ToString(), title, MessageBoxButtons::OK, MessageBoxIcon::Hand);
break;
}
}
フォームロード¶
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
// ユニット識別スイッチ
unitSwitchComboBox->ResetText();
unitSwitchComboBox->Items->AddRange(gcnew array<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(gcnew array<String^> { "AIO-64/4/1B-USC", "AIO-60/4/1B-USC" });
modelNameComboBox->SelectedIndex = 0;
// レンジ
rangeTypeComboBox->ResetText();
rangeTypeComboBox->Items->AddRange(gcnew array<String^> { "-10 ~ +10V", "-5 ~ +5V" });
}
オープン¶
private: System::Void openButton_Click(System::Object^ sender, System::EventArgs^ e)
{
int getId;
int result = YdxOpen(unitSwitchComboBox->SelectedIndex, modelNameComboBox->Text, 0, &getId);
if (result != 0)
ResultShow("YdxOpen", result);
else {
unitSwitchComboBox->Enabled = false;
modelNameComboBox->Enabled = false;
rangeTypeComboBox->SelectedIndexChanged -= gcnew EventHandler(this, &Form1::rangeTypeComboBox_SelectedIndexChanged);
rangeTypeComboBox->SelectedIndex = 0;
rangeTypeComboBox->SelectedIndexChanged += gcnew EventHandler(this,&Form1::rangeTypeComboBox_SelectedIndexChanged);
ResultShow("オープン", result);
id = getId;
}
}
レンジ切り替え¶
private: System::Void rangeTypeComboBox_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e)
{
int result = YdxAiSetRange(id, rangeTypeComboBox->SelectedIndex);
if (result != 0)
ResultShow("YdxAiSetRange", result);
}
アナログ入力¶
private: System::Void inputButton_Click(System::Object^ sender, System::EventArgs^ e)
{
const int CHANNEL_NUM = 6;
float data[CHANNEL_NUM];
int result = YdxAiInputVolt(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: System::Void closeButton_Click(System::Object^ sender, System::EventArgs^ e)
{
unitSwitchComboBox->Enabled = true;
modelNameComboBox->Enabled = true;
int result = YdxClose(id);
if (result != 0)
ResultShow("YdxClose", result);
else
ResultShow("クローズ", result);
}
フォームクローズ¶
private: System::Void Form1_FormClosing(System::Object^ sender, System::Windows::Forms::FormClosingEventArgs^ e)
{
int result = YdxClose(id);
if ((result != 0) && (result != YDX_RESULT_NOT_OPEN))
{
ResultShow("YdxClose", result);
}
}