Visual Basic(.NET2002以降)¶
開発環境の設定¶
-
Ydx.vb をプロジェクトフォルダにコピーします。
-
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-04/4/1B-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
' データバッファの設定
result = YdxAoSetBuffer(id, 0) ' FIFOバッファ
If result <> 0 Then
ResultShow("YdxAoSetBuffer", result)
Exit Sub
End If
' チャネルの設定
Const CHANNEL_NUM As Integer = 4 ' 4チャネルを有効にする
For channel As Integer = 0 To CHANNEL_NUM - 1
result = YdxAoSetChannel(id, channel, 1)
If result <> 0 Then
ResultShow("YdxAoSetChannel", result)
Exit Sub
End If
Next
' サンプリングクロックの設定
result = YdxAoSetClock(id, 0) ' 内部クロック
If result <> 0 Then
ResultShow("YdxAoSetClock", result)
Exit Sub
End If
' 内部クロック周期の設定
result = YdxAoSetClockInternal(id, 1000) ' 1000μsec
If result <> 0 Then
ResultShow("YdxAoSetClockInternal", result)
Exit Sub
End If
' データの設定
Const SAMPLE_NUM As Integer = 1000 ' サンプル数
Dim data(SAMPLE_NUM * CHANNEL_NUM) As Single ' データ個数は、サンプル数 * 有効チャネル数
For i As Integer = 0 To SAMPLE_NUM - 1
Dim waveData As Single = CType(10 * Math.Sin(2 * Math.PI * i / SAMPLE_NUM), Single) ' 正弦波
For channel As Integer = 0 To CHANNEL_NUM - 1
data(i * CHANNEL_NUM + channel) = waveData
Next
Next
result = YdxAoSetDataVolt(id, SAMPLE_NUM, data)
If result <> 0 Then
ResultShow("YdxAoSetDataVolt", result)
Exit Sub
End If
' サンプリング開始条件の設定
result = YdxAoSetStartCondition(id, 0, 0) ' ソフトウェア
If result <> 0 Then
ResultShow("YdxAoSetStartCondition", result)
Exit Sub
End If
' サンプリング停止条件の設定
result = YdxAoSetStopCondition(id, 0, 0) ' データ終了
If result <> 0 Then
ResultShow("YdxAoSetStopCondition", result)
Exit Sub
End If
' イベントオブジェクト作成
Dim hEvent As AutoResetEvent = New AutoResetEvent(False)
' イベントの設定
result = YdxAoSetEvent(id, _
YDX_EVENT_COMMUNICATE_ERR Or _
YDX_EVENT_HARDWARE_ERR Or _
YDX_EVENT_SAMPLE_CLOCK_ERR Or _
YDX_EVENT_STOP, _
hEvent.Handle)
If result <> 0 Then
ResultShow("YdxAoSetEvent", result)
hEvent.Close()
Exit Sub
End If
' アナログ出力動作を開始
result = YdxAoStart(id)
If result <> 0 Then
ResultShow("YdxAoStart", result)
hEvent.Close()
Exit Sub
End If
' イベント発生待ち
hEvent.WaitOne()
hEvent.Close()
' ステータスの取得
Dim factor, sampleCount, repeatCount, notOutNum As Integer
result = YdxAoGetEventStatus(id, factor, sampleCount, repeatCount, notOutNum)
If result <> 0 Then
ResultShow("YdxAoGetEventStatus", result)
Exit Sub
End If
If (factor And YDX_STATUS_COMMUNICATE_ERR) <> 0 Then
MessageBox.Show("通信エラーが発生しました", "", MessageBoxButtons.OK, MessageBoxIcon.Hand)
Exit Sub
End If
If (factor And YDX_STATUS_HARDWARE_ERR) <> 0 Then
MessageBox.Show("ハードウェアエラーが発生しました", "", MessageBoxButtons.OK, MessageBoxIcon.Hand)
Exit Sub
End If
If (factor And YDX_STATUS_SAMPLE_CLOCK_ERR) <> 0 Then
MessageBox.Show("サンプリングクロックエラーが発生しました", "", MessageBoxButtons.OK, MessageBoxIcon.Hand)
Exit Sub
End If
ResultShow("アナログ出力", 0)
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