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

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 inputButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles inputButton.Click
    dataTextBox.ResetText()
    Application.DoEvents()
    Const CHANNEL_NUM As Integer = 6
    Dim data(CHANNEL_NUM - 1) As Single
    result = YdxAiInputVolt(id, 0, CHANNEL_NUM, data)
    If result <> 0 Then
        ResultShow("YdxAiInput", result)
        Exit Sub
    End If

    Dim txt As String = ""
    Dim channel As Integer
    For channel = 0 To CHANNEL_NUM - 1
        txt += "CH" + channel.ToString() + " : " + data(channel).ToString(" 0.000;-0.000").PadLeft(7) + "V" + 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