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

Visual Basic(.NET2002以降)

開発環境の設定

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

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

コントロール

変数

Dim id As Short
Dim result As Integer

実行結果の表示

Private Sub ResultShow(ByVal title As String, ByVal resultCode As Integer)
    Dim resultString As New StringBuilder(256)
    YdxCnvResultToString(resultCode, resultString)
    Select Case resultCode
        Case 0, Ydx.YDX_RESULT_DI_EXCEED_DATA_NUM, Ydx.YDX_RESULT_DI_EXCEED_BUF_SIZ
            MessageBox.Show(resultString.ToString(), title, MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
        Case Else
            MessageBox.Show(resultString.ToString(), title, MessageBoxButtons.OK, MessageBoxIcon.Hand)
    End Select
End Sub

フォームロード

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ' ユニット識別スイッチ
    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" })
End Sub

オープン

Private Sub openButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles openButton.Click
    result = YdxOpen(unitSwitchComboBox.SelectedIndex, modelNameComboBox.Text, 0, id)
    If result <> 0 Then
        ResultShow("YdxOpen", result)
    Else
        unitSwitchComboBox.Enabled = False
        modelNameComboBox.Enabled = False
        RemoveHandler rangeTypeComboBox.SelectedIndexChanged, AddressOf rangeTypeComboBox_SelectedIndexChange
        rangeTypeComboBox.SelectedIndex = 0
        AddHandler rangeTypeComboBox.SelectedIndexChanged, AddressOf rangeTypeComboBox_SelectedIndexChange
        ResultShow("オープン", result)
    End If
End Sub

レンジ切り替え

Private Sub rangeTypeComboBox_SelectedIndexChange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rangeTypeComboBox.SelectedIndexChanged
    result = YdxAiSetRange(id, rangeTypeComboBox.SelectedIndex)
    If result <> 0 Then
        ResultShow("YdxAiSetRange", result)
    End If
End Sub

入力開始

Private Sub startButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startButton.Click
    dataTextBox.ResetText()
    Application.DoEvents()

    ' データバッファの設定
    result = YdxAiSetBuffer(id, 0)   ' FIFOバッファ
    If result <> 0 Then
        ResultShow("YdxAiSetBuffer", result)
        Exit Sub
    End If

    ' チャネルの設定
    Const CHANNEL_NUM As Integer = 6  ' 6チャネルを有効にする
    For channel As Integer = 0 To CHANNEL_NUM - 1
        result = YdxAiSetChannel(id, channel, 1)
        If result <> 0 Then
            ResultShow("YdxAiSetChannel", result)
            Exit Sub
        End If
    Next

    ' サンプリングクロックの設定
    result = YdxAiSetClock(id, 0)    ' 内部クロック
    If result <> 0 Then
        ResultShow("YdxAiSetClock", result)
        Exit Sub
    End If

    ' 内部クロック周期の設定
    result = YdxAiSetClockInternal(id, 1000) ' 1000μsec
    If result <> 0 Then
        ResultShow("YdxAiSetClockInternal", result)
        Exit Sub
    End If

    ' サンプリング開始条件の設定
    result = YdxAiSetStartCondition(id, 0, 0)   ' ソフトウェア
    If result <> 0 Then
        ResultShow("YdxAiSetStartCondition", result)
        Exit Sub
    End If

    ' サンプリング停止条件の設定
    result = YdxAiSetStopCondition(id, 0, 0)    ' サンプル数
    If result <> 0 Then
        ResultShow("YdxAiSetStopCondition", result)
        Exit Sub
    End If

    ' サンプリング停止条件(サンプル数)の設定
    Const SAMPLE_NUM As Integer = 1000    ' 1000個
    result = YdxAiSetStopSampleNum(id, SAMPLE_NUM)
    If result <> 0 Then
        ResultShow("YdxAiSetStopSampleNum", result)
        Exit Sub
    End If

    ' データをクリア
    result = YdxAiClearData(id)
    If result <> 0 Then
        ResultShow("YdxAiClearData", result)
        Exit Sub
    End If

    ' アナログ入力動作を開始
    result = YdxAiStart(id)
    If result <> 0 Then
        ResultShow("YdxAiStart", result)
        Exit Sub
    End If

    ' 動作終了待ち
    Dim status, sampleCount, repeatCount As Integer
    ' 動作中ステータスがOFFになるまでポーリング
    Do
        ' ステータスの取得
        result = YdxAiGetStatus(id, status, sampleCount, repeatCount)
        If result <> 0 Then
            ResultShow("YdxAiGetStatus", result)
            Exit Sub
        End If
        statusTextBox.Text = status.ToString("X").PadLeft(8, "0"c) + "h"
        sampleCountTextBox.Text = sampleCount.ToString()
        repeatCountTextBox.Text = repeatCount.ToString()
        Application.DoEvents()

        If (status And YDX_STATUS_COMMUNICATE_ERR) <> 0 Then
            MessageBox.Show("通信エラーが発生しました", "", MessageBoxButtons.OK, MessageBoxIcon.Hand)
            Exit Sub
        End If

        If (status And YDX_STATUS_HARDWARE_ERR) <> 0 Then
            MessageBox.Show("ハードウェアエラーが発生しました", "", MessageBoxButtons.OK, MessageBoxIcon.Hand)
            Exit Sub
        End If

        If (status And YDX_STATUS_OVERRUN_ERR) <> 0 Then
            MessageBox.Show("オーバランエラーが発生しました", "", MessageBoxButtons.OK, MessageBoxIcon.Hand)
            Exit Sub
        End If

        If (status And YDX_STATUS_SAMPLE_CLOCK_ERR) <> 0 Then
            MessageBox.Show("サンプリングクロックエラーが発生しました", "", MessageBoxButtons.OK, MessageBoxIcon.Hand)
            Exit Sub
        End If
    Loop While (status And YDX_STATUS_BUSY) <> 0

    ' データの読み出し
    Dim data(sampleCount * CHANNEL_NUM) As Single   ' データ個数は、サンプル数 * 有効チャネル数
    Dim sampleNum As Integer = sampleCount
    result = YdxAiGetDataVolt(id, sampleNum, data)
    If result <> 0 Then
        ResultShow("YdxAiGetDataVolt", result)
        If result <> YDX_RESULT_AI_EXCEED_DATA_NUM And result <> YDX_RESULT_AI_EXCEED_BUF_SIZ Then
            Exit Sub
        End If
    End If

    ' 表示
    Dim txt As String = "    "
    For channel As Integer = 0 To CHANNEL_NUM - 1
        txt += "      CH" + channel.ToString()
    Next

    txt += Environment.NewLine
    For sampleIndex As Integer = 0 To sampleNum - 1
        txt += (sampleIndex + 1).ToString().PadLeft(5) + " : "
        For channel As Integer = 0 To CHANNEL_NUM - 1
            txt += data(sampleIndex * CHANNEL_NUM + channel).ToString(" 0.000;-0.000").PadLeft(7) + "V "
        Next
        txt += Environment.NewLine
    Next

    dataTextBox.Text = txt
End Sub

クローズ

Private Sub closeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles closeButton.Click
    unitSwitchComboBox.Enabled = True
    modelNameComboBox.Enabled = True
    result = YdxClose(id)
    If result <> 0 Then
        ResultShow("YdxClose", result)
    Else
        ResultShow("クローズ", result)
    End If
End Sub

フォームクローズ

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    result = YdxClose(id)
    If result <> 0 And result <> YDX_RESULT_NOT_OPEN Then
        ResultShow("YdxClose", result)
    End If
End Sub