コンテンツにスキップ
サンプルプログラム > アナログ出力 > AoEvent >

C++/CLI

開発環境の設定

  1. YdxCLI.h をプロジェクトフォルダにコピーします。

  2. YdxCLI.h をプロジェクトに追加します。

  3. ソースファイルに YdxCLI.h をインクルードします。

  4. usingディレクティブを使ってYdxCLIを宣言します。

using namespace 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-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 startButton_Click(System::Object^ sender, System::EventArgs^ e)
{
    int result;

    // データバッファの設定
    result = YdxAoSetBuffer(id, 0);   // FIFOバッファ
    if (result != 0)
    {
        ResultShow("YdxAoSetBuffer", result);
        return;
    }

    // チャネルの設定
    const int CHANNEL_NUM = 4;  // 4チャネルを有効にする
    for (int channel = 0; channel < CHANNEL_NUM; channel++)
    {
        result = YdxAoSetChannel(id, channel, 1);
        if (result != 0)
        {
            ResultShow("YdxAoSetChannel", result);
            return;
        }
    }

    // サンプリングクロックの設定
    result = YdxAoSetClock(id, 0);    // 内部クロック
    if (result != 0)
    {
        ResultShow("YdxAoSetClock", result);
        return;
    }

    // 内部クロック周期の設定
    result = YdxAoSetClockInternal(id, 1000); // 1000μsec
    if (result != 0)
    {
        ResultShow("YdxAoSetClockInternal", result);
        return;
    }

    // データの設定
    const int SAMPLE_NUM = 1000;  // サンプル数
    float data[SAMPLE_NUM * CHANNEL_NUM];   // データ個数は、サンプル数 * 有効チャネル数
    for (int i = 0; i < SAMPLE_NUM; i++)
    {
        float waveData = (float)(10 * Math::Sin(2 * Math.PI * i / SAMPLE_NUM));    // 正弦波
        for (int channel = 0; channel < CHANNEL_NUM; channel++)
        {
            data[i * CHANNEL_NUM + channel] = waveData;
        }
    }
    result = YdxAoSetDataVolt(id, SAMPLE_NUM, data);
    if (result != 0)
    {
        ResultShow("YdxAoSetDataVolt", result);
        return;
    }

    // サンプリング開始条件の設定
    result = YdxAoSetStartCondition(id, 0, 0);   // ソフトウェア
    if (result != 0)
    {
        ResultShow("YdxAoSetStartCondition", result);
        return;
    }

    // サンプリング停止条件の設定
    result = YdxAoSetStopCondition(id, 0, 0);    // データ終了
    if (result != 0)
    {
        ResultShow("YdxAoSetStopCondition", result);
        return;
    }

    // イベントオブジェクト作成
    AutoResetEvent^ hEvent = gcnew AutoResetEvent(false);

    // イベントの設定
    result = YdxAoSetEvent(id,
        YDX_EVENT_COMMUNICATE_ERR |
        YDX_EVENT_HARDWARE_ERR |
        YDX_EVENT_SAMPLE_CLOCK_ERR |
        YDX_EVENT_STOP,
        hEvent->Handle);
    if (result != 0)
    {
        ResultShow("YdxAoSetEvent", result);
        hEvent->Close();
        return;
    }

    // アナログ出力動作を開始
    result = YdxAoStart(id);
    if (result != 0)
    {
        ResultShow("YdxAoStart", result);
        hEvent->Close();
        return;
    }

    // イベント発生待ち
    hEvent->WaitOne();
    hEvent->Close();

    // ステータスの取得
    int factor, sampleCount, repeatCount, notOutNum;
    result = YdxAoGetEventStatus(id, &factor, &sampleCount, &repeatCount, &notOutNum);
    if (result != 0)
    {
        ResultShow("YdxAoGetEventStatus", result);
        return;
    }

    if ((factor & YDX_EVENT_COMMUNICATE_ERR) != 0)
    {
        MessageBox::Show("通信エラーが発生しました", "", MessageBoxButtons::OK, MessageBoxIcon::Hand);
        return;
    }
    if ((factor & YDX_EVENT_HARDWARE_ERR) != 0)
    {
        MessageBox::Show("ハードウェアエラーが発生しました", "", MessageBoxButtons::OK, MessageBoxIcon::Hand);
        return;
    }
    if ((factor & YDX_EVENT_SAMPLE_CLOCK_ERR) != 0)
    {
        MessageBox::Show("サンプリングクロックエラーが発生しました", "", MessageBoxButtons::OK, MessageBoxIcon::Hand);
        return;
    }

    ResultShow("アナログ出力", 0);
}

クローズ

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);
    }
}