Visual Basic 6.0¶
開発環境の設定¶
-
Ydx.bas をプロジェクトフォルダにコピーします。
-
Ydx.bas をプロジェクトに追加します。
コントロール¶
定義¶
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function CreateEvent Lib "kernel32" Alias "CreateEventA" (ByVal lpEventAttributes As Long, ByVal bManualReset As Long, ByVal bInitialState As Long, ByVal lpName As String) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Const INFINITE = &HFFFF
変数¶
Dim id As Long
Dim result As Long
実行結果の表示¶
Private Sub ResultShow(ByVal title As String, ByVal resultCode As Long)
Dim resultString As String
Call YdxCnvResultToString(resultCode, resultString)
Select Case resultCode
Case 0, Ydx.YDX_RESULT_AI_EXCEED_DATA_NUM, Ydx.YDX_RESULT_AI_EXCEED_BUF_SIZ
MsgBox resultString, vbInformation, title
Case Else
MsgBox resultString, vbCritical, title
End Select
End Sub
フォームロード¶
Private Sub Form_Load()
' ユニット識別スイッチ
cboUnitSwitch.AddItem "0"
cboUnitSwitch.AddItem "1"
cboUnitSwitch.AddItem "2"
cboUnitSwitch.AddItem "3"
cboUnitSwitch.AddItem "4"
cboUnitSwitch.AddItem "5"
cboUnitSwitch.AddItem "6"
cboUnitSwitch.AddItem "7"
cboUnitSwitch.AddItem "8"
cboUnitSwitch.AddItem "9"
cboUnitSwitch.AddItem "A"
cboUnitSwitch.AddItem "B"
cboUnitSwitch.AddItem "C"
cboUnitSwitch.AddItem "D"
cboUnitSwitch.AddItem "E"
cboUnitSwitch.AddItem "F"
cboUnitSwitch.ListIndex = 0
' 型名
cboModelName.AddItem "AIO-64/4/1B-USC"
cboModelName.AddItem "AIO-04/4/1B-USC"
cboModelName.ListIndex = 0
End Sub
オープン¶
Private Sub cmdOpen_Click()
result = YdxOpen(cboUnitSwitch.ListIndex, cboModelName.Text, 0, id)
If result <> 0 Then
Call ResultShow("YdxOpen", result)
Else
cboUnitSwitch.Enabled = False
cboModelName.Enabled = False
Call ResultShow("オープン", result)
End If
End Sub
出力開始¶
Private Sub cmdStart_Click()
' データバッファの設定
result = YdxAoSetBuffer(id, 0) ' FIFOバッファ
If result <> 0 Then
Call ResultShow("YdxAoSetBuffer", result)
Exit Sub
End If
' チャネルの設定
Const CHANNEL_NUM As Long = 4 ' 4チャネルを有効にする
Dim channel As Long
For channel = 0 To CHANNEL_NUM - 1
result = YdxAoSetChannel(id, channel, 1)
If result <> 0 Then
Call ResultShow("YdxAoSetChannel", result)
Exit Sub
End If
Next
' サンプリングクロックの設定
result = YdxAoSetClock(id, 0) ' 内部クロック
If result <> 0 Then
Call ResultShow("YdxAoSetClock", result)
Exit Sub
End If
' 内部クロック周期の設定
result = YdxAoSetClockInternal(id, 1000) ' 1000μsec
If result <> 0 Then
Call ResultShow("YdxAoSetClockInternal", result)
Exit Sub
End If
' データの設定
Const SAMPLE_NUM As Long = 1000 ' サンプル数
Dim data(SAMPLE_NUM * CHANNEL_NUM) As Single ' データ個数は、サンプル数 * 有効チャネル数
Dim i As Long
For i = 0 To SAMPLE_NUM - 1
Dim waveData As Single
waveData = 10 * Math.Sin(2 * 3.14159265358979 * i / SAMPLE_NUM) ' 正弦波
For channel = 0 To CHANNEL_NUM - 1
data(i * CHANNEL_NUM + channel) = waveData
Next
Next
result = YdxAoSetDataVolt(id, SAMPLE_NUM, data(0))
If result <> 0 Then
Call ResultShow("YdxAoSetDataVolt", result)
Exit Sub
End If
' サンプリング開始条件の設定
result = YdxAoSetStartCondition(id, 0, 0) ' ソフトウェア
If result <> 0 Then
Call ResultShow("YdxAoSetStartCondition", result)
Exit Sub
End If
' サンプリング停止条件の設定
result = YdxAoSetStopCondition(id, 0, 0) ' データ終了
If result <> 0 Then
Call ResultShow("YdxAoSetStopCondition", result)
Exit Sub
End If
' イベントオブジェクト作成
Dim hEvent As Long
hEvent = CreateEvent(0, False, False, 0)
If hEvent = 0 Then
MsgBox "イベントオブジェクトの作成に失敗しました", vbCritical
Exit Sub
End If
' イベントの設定
result = YdxAoSetEvent(id, _
YDX_EVENT_COMMUNICATE_ERR Or _
YDX_EVENT_HARDWARE_ERR Or _
YDX_EVENT_SAMPLE_CLOCK_ERR Or _
YDX_EVENT_STOP, _
hEvent)
If result <> 0 Then
Call ResultShow("YdxAoSetEvent", result)
CloseHandle hEvent
Exit Sub
End If
' アナログ出力動作を開始
result = YdxAoStart(id)
If result <> 0 Then
Call ResultShow("YdxAoStart", result)
CloseHandle hEvent
Exit Sub
End If
' イベント発生待ち
Call WaitForSingleObject(hEvent, INFINITE)
CloseHandle hEvent
' ステータスの取得
Dim factor, sampleCount, repeatCount, notOutNum As Long
result = YdxAoGetEventStatus(id, factor, sampleCount, repeatCount, notOutNum)
If result <> 0 Then
Call ResultShow("YdxAoGetEventStatus", result)
Exit Sub
End If
If (factor And YDX_STATUS_COMMUNICATE_ERR) <> 0 Then
MsgBox "通信エラーが発生しました", vbCritical
Exit Sub
End If
If (factor And YDX_STATUS_HARDWARE_ERR) <> 0 Then
MsgBox "ハードウェアエラーが発生しました", vbCritical
Exit Sub
End If
If (factor And YDX_STATUS_SAMPLE_CLOCK_ERR) <> 0 Then
MsgBox "サンプリングクロックエラーが発生しました", vbCritical
Exit Sub
End If
Call ResultShow("アナログ出力", 0)
End Sub
クローズ¶
Private Sub cmdClose_Click()
cboUnitSwitch.Enabled = True
cboModelName.Enabled = True
result = YdxClose(id)
If result <> 0 Then
Call ResultShow("YdxClose", result)
Else
Call ResultShow("クローズ", result)
End If
End Sub
フォームアンロード¶
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
result = YdxClose(id)
If result <> 0 And result <> YDX_RESULT_NOT_OPEN Then
Call ResultShow("YdxClose", result)
End If
End Sub