コンテンツにスキップ

サンプルプログラム > デジタル入出力 > Dio >

Visual Basic .NET

開発環境の設定

  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", "AIO-04/4/1B-USC" })
    modelNameComboBox.SelectedIndex

オープン

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
        ResultShow("オープン", result)
    End If
End Sub

デジタル入力

Private Sub inputButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles inputButton.Click
    inputDataTextBox.ResetText()
    Application.DoEvents()
    Const DI_CHANNEL_NUM As Integer = 4
    Dim data(DI_CHANNEL_NUM - 1) As Integer
    result = YdxDiInputBit(id, 0, DI_CHANNEL_NUM, 0, data)
    If result <> 0 Then
        ResultShow("YdxDiInputBit", result)
        Exit Sub
    End If

    Dim txt As String = ""
    Dim channel As Integer
    For channel = 0 To DI_CHANNEL_NUM - 1
        txt += "CH" + channel.ToString() + " : " + data(channel).ToString() + Environment.NewLine
    Next

    inputDataTextBox.Text = txt
End Sub

デジタル出力

Private Sub outputButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles outputButton.Click
    Dim data(0) As Integer
    Dim doubleData As Double
    If Double.TryParse(outputDataTextBox.Text, Globalization.NumberStyles.Integer, Nothing, doubleData) = False Then
        MessageBox.Show("データが不正です", "", MessageBoxButtons.OK, MessageBoxIcon.Hand)
        Exit Sub
    End If

    data(0) = CType(doubleData, Integer)
    result = YdxDoOutputBit(id, 0, 1, data)
    ResultShow("デジタル出力", result)
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