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", "AIO-04/4/1B-USC" });
modelNameComboBox->SelectedIndex = 0;
}
オープン¶
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;
ResultShow("オープン", result);
id = getId;
}
}
デジタル入力¶
private: System::Void inputButton_Click(System::Object^ sender, System::EventArgs^ e)
{
inputDataTextBox->ResetText();
Application::DoEvents();
const int DI_CHANNEL_NUM = 4;
int data[DI_CHANNEL_NUM];
int result = YdxDiInputBit(id, 0, DI_CHANNEL_NUM, 0, data);
if (result != 0)
{
ResultShow("YdxDiInputBit", result);
return;
}
String^ txt = "";
for (int channel = 0; channel < DI_CHANNEL_NUM; channel++)
{
txt += "CH" + channel.ToString() + " : " + data[channel].ToString() + Environment::NewLine;
}
inputDataTextBox->Text = txt;
}
デジタル出力¶
private: System::Void outputButton_Click(System::Object^ sender, System::EventArgs^ e)
{
int data[1];
if (!int::TryParse(outputDataTextBox->Text, data[0]))
{
MessageBox::Show("データが不正です", "", MessageBoxButtons::OK, MessageBoxIcon::Hand);
return;
}
int result = YdxDoOutputBit(id, 0, 1, data);
ResultShow("デジタル出力", result);
}
クローズ¶
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);
}
}