Visual Basic(.NET2002以降)¶
開発環境の設定¶
-
Ydx.vb をプロジェクトフォルダにコピーします。
-
Ydx.vb をプロジェクトに追加します。
コントロール¶
変数¶
Dim id As Integer
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() { "DIO-16/16C-USC", "DIO-16/16D-UBC", "DIO-16/16D-USC" })
modelNameComboBox.SelectedIndex = 0
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
ResultShow("オープン", 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 = YdxDiSetBuffer(id, 0) ' FIFOバッファ
If result <> 0 Then
ResultShow("YdxDiSetBuffer", result)
Exit Sub
End If
' サンプリングクロックの設定
result = YdxDiSetClock(id, 0) ' 内部クロック
If result <> 0 Then
ResultShow("YdxDiSetClock", result)
Exit Sub
End If
' 内部クロック周期の設定
result = YdxDiSetClockInternal(id, 1000) ' 1000μsec
If result <> 0 Then
ResultShow("YdxDiSetClockInternal", result)
Exit Sub
End If
' サンプリング開始条件の設定
result = YdxDiSetStartCondition(id, 0, 0) ' ソフトウェア
If result <> 0 Then
ResultShow("YdxDiSetStartCondition", result)
Exit Sub
End If
' サンプリング停止条件の設定
result = YdxDiSetStopCondition(id, 0, 0) ' サンプル数
If result <> 0 Then
ResultShow("YdxDiSetStopCondition", result)
Exit Sub
End If
' サンプリング停止条件(サンプル数)の設定
result = YdxDiSetStopSampleNum(id, 1000)
If result <> 0 Then
ResultShow("YdxDiSetStopSampleNum", result)
Exit Sub
End If
' データをクリア
result = YdxDiClearData(id)
If result <> 0 Then
ResultShow("YdxDiClearData", result)
Exit Sub
End If
' デジタル入力動作を開始
result = YdxDiStart(id)
If result <> 0 Then
ResultShow("YdxDiStart", result)
Exit Sub
End If
' 動作終了待ち
Dim status, sampleCount, repeatCount As Integer
' 動作中ステータスがOFFになるまでポーリング
Do
' ステータスの取得
result = YdxDiGetStatus(id, status, sampleCount, repeatCount)
If result <> 0 Then
ResultShow("YdxDiGetStatus", 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) As Integer
result = YdxDiGetData(id, sampleCount, data)
If result <> 0 Then
ResultShow("YdxDiGetData", result)
If result <> YDX_RESULT_DI_EXCEED_DATA_NUM And result <> YDX_RESULT_DI_EXCEED_BUF_SIZ Then
Exit Sub
End If
End If
' 表示
Dim txt As String = ""
For sampleIndex As Integer = 0 To sampleCount - 1
txt &= (sampleIndex + 1).ToString().PadLeft(5) & " : "
' 2進数表記
txt &= Convert.ToString(data(sampleIndex) >> 16 And &HF, 2).PadLeft(4, "0"c) & " "
txt &= Convert.ToString(data(sampleIndex) >> 8 And &HF, 2).PadLeft(4, "0"c) & " "
txt &= Convert.ToString(data(sampleIndex) >> 4 And &HF, 2).PadLeft(4, "0"c) & " "
txt &= Convert.ToString(data(sampleIndex) And &HF, 2).PadLeft(4, "0"c)
' 16進数表記
txt &= " (" & data(sampleIndex).ToString("X").PadLeft(4) & "h)"
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