﻿Option Strict Off
Option Explicit On
Imports System.Text

Public Class Form1
    Dim Id As Short ' デバイスID
    Const MAX_SAMPLES As Short = 512    ' 最大サンプリング数
    Dim Data(MAX_SAMPLES) As Integer    ' 取得データの配列
    Dim UpperData(MAX_SAMPLES) As Integer   ' 波形判定上限データの配列
    Dim LowerData(MAX_SAMPLES) As Integer   ' 波形判定下限データの配列
    Dim Judge(1) As Short     ' 波形判定結果
    Dim JudgeDetail(MAX_SAMPLES) As Short   ' 波形判定結果詳細
    Dim Records As Integer  ' 取得レコード数
    Dim Resolution As Integer   ' デバイスの分解能
    Dim bmp As Bitmap    ' ビットマップ

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        bmp = New Bitmap(Graph.Size.Width, Graph.Size.Height)        ' グラフと同サイズのビットマップを生成
    End Sub

    '==================================
    ' 初期化処理
    '==================================
    Private Sub CommandInit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CommandInit.Click
        Dim Ret As Integer     ' 戻り値
        Dim Ret2 As Integer    ' 戻り値2
        Dim ErrorString As New StringBuilder("", 256)     ' エラー文字列

        '----------------------------------
        ' 初期化処理
        '----------------------------------
        Ret = DigInit(TextDevice.Text, Id)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigInit = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' デバイスのリセット
        '----------------------------------
        Ret = DigResetDevice(Id)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigResetDevice = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        TextInfo.Text = "初期化処理 : 正常終了"
    End Sub

    '==================================
    ' デバイス設定
    '==================================
    Private Sub CommandSetting_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CommandSetting.Click
        Dim Ret As Integer        ' 戻り値
        Dim Ret2 As Integer       ' 戻り値2
        Dim ErrorString As New StringBuilder("", 256)     ' エラー文字列

        '----------------------------------
        ' 変数を初期化
        '----------------------------------
        Records = 0
        Resolution = 1024        ' DIG-100M1002-PCI用、固定設定

        '----------------------------------
        ' 動作モード設定：波形判定機モード
        '----------------------------------
        Ret = DigSetDigitizerMode(Id, DIG_MODE_JUDGE)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigSetDigitizerMode = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' 強制停止要因：サンプリングクロックエラー、波形判定カウント一致、
        '      デバイスメモリオーバーフロー
        '----------------------------------
        Ret = DigSetHaltCondition(Id, DIG_HALT_CLOCK_ERROR Or DIG_HALT_JUDGE_COUNT Or DIG_HALT_DEVMEM_OVERFLOW)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigSetHaltCondition = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' サンプリングクロックソース設定：ボード内水晶発振機
        '----------------------------------
        Ret = DigSetClockSource(Id, DIG_ECU_SRC_CLOCK)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigSetClockSource = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' サンプリングレート設定：1MHz
        '----------------------------------
        Ret = DigSetSampleRate(Id, 1000000)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigSetSampleRate = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' レコード長設定：512サンプル
        '----------------------------------
        Ret = DigSetRecordLength(Id, MAX_SAMPLES)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigSetRecordLength = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' レコード収集回数設定：無限回
        '----------------------------------
        Ret = DigSetRecordNumber(Id, 0)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigSetRecordNumber = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' デューティ比設定：50%
        '----------------------------------
        Ret = DigSetDutyCycle(Id, 50)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigSetDutyCycle = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' チャネル設定：0チャネルのみ有効
        '----------------------------------
        ' 0チャネルを有効にする
        Ret = DigSetChannel(Id, DIG_CHANNEL0, DIG_ENABLE)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigSetChannel = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        ' 1チャネルを無効にする
        Ret = DigSetChannel(Id, DIG_CHANNEL1, DIG_DISABLE)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigSetChannel = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' レンジ設定：±20V
        '----------------------------------
        Ret = DigSetRange(Id, DIG_CHANNEL0, DIG_RANGE_40VPP)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigSetRange = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' プリトリガ種類の設定：自動
        '----------------------------------
        Ret = DigSetPreTriggerType(Id, DIG_TRIGGERTYPE_AUTO)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigSetPreTriggerType = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' ポストトリガ種類の設定：アナログエッジトリガ
        '----------------------------------
        Ret = DigSetPostTriggerType(Id, DIG_TRIGGERTYPE_AIEDGE)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigSetPostTriggerType = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' ポストトリガの詳細設定：トリガ信号0チャネル、トリガレベル0V、正方向エッジ、1回
        '----------------------------------
        Ret = DigSetPostTriggerAiEdge(Id, DIG_CHANNEL0, 0, DIG_TRIGGER_SLOPE_RISE, 1)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigSetPostTriggerAiEdge = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' 波形判定を行うチャネルを設定：0チャネル
        '----------------------------------
        Ret = DigSetJudgeChannel(Id, DIG_CHANNEL0, DIG_ENABLE)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigSetJudgeChannel = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' 波形判定結果のカウント上限回数を設定：失敗回数をカウント、カウント上限1回
        '----------------------------------
        Ret = DigSetJudgeCountLimit(Id, DIG_CHANNEL0, DIG_JUDGE_NG, 1)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigSetJudgeCountLimit = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' 波形判定用データ自動設定：水平軸マージン±10データ、垂直軸マージン±50データ、新規作成
        '----------------------------------
        Ret = DigSetAutoMask(Id, DIG_CHANNEL0, 10, 50, DIG_AUTOMASK_NEW)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigSetAutoMask = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        ' 実際に設定されたデータの取得
        Ret = DigGetJudgeData32(Id, DIG_CHANNEL0, MAX_SAMPLES, UpperData, LowerData)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigGetJudgeData = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        TextInfo.Text = "デバイス設定：正常終了"
    End Sub

    '==================================
    ' 判定開始
    '==================================
    Private Sub CommandStart_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CommandStart.Click
        Dim Ret As Integer        ' 戻り値
        Dim Ret2 As Integer       ' 戻り値2
        Dim ErrorString As New StringBuilder("", 256)     ' エラー文字列

        '----------------------------------
        ' 動作開始
        '----------------------------------
        Ret = DigStart(Id)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigStart = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
        TextInfo.Text = "収集開始 : 正常終了"

        '----------------------------------
        ' 動作中ステータスがOFFになるまでポーリング
        '----------------------------------
        Dim Status As Integer
        Dim NgCount As Integer       ' 波形判定の失敗回数

        Do
            System.Windows.Forms.Application.DoEvents()          ' ダイアログ操作を有効にするための処理
            '----------------------------------
            ' ドライバメモリ内レコード数を取得
            '----------------------------------
            Ret = DigGetDriverMemoryRecords(Id, Records)
            If Ret <> 0 Then
                Ret2 = DigGetErrorString(Ret, ErrorString)
                TextInfo.Text = "DigGetDriverMemoryRecords = " & Ret & " : " & ErrorString.ToString()
                Exit Sub
            End If

            '----------------------------------
            ' ドライバメモリ内にレコードが存在すれば、新しい方からデータを取得
            '----------------------------------
            If Records > 0 Or Records = -1 Then
                Ret = DigReadLatestRecords32(Id, DIG_RECORD_DATA, 1, Data, Judge, JudgeDetail)
                If Ret <> 0 Then
                    Ret2 = DigGetErrorString(Ret, ErrorString)
                    TextInfo.Text = "DigReadLatestRecords = " & Ret & " : " & ErrorString.ToString()
                    Exit Sub
                End If
                '----------------------------------
                ' グラフの描画
                '----------------------------------
                DrawGraph()
            End If

            '----------------------------------
            ' 波形判定の失敗回数を取得
            '----------------------------------
            Ret = DigGetJudgeCount(Id, NgCount)
            If Ret <> 0 Then
                Ret2 = DigGetErrorString(Ret, ErrorString)
                TextInfo.Text = "DigGetJudgeCount = " & Ret & " : " & ErrorString.ToString()
                Exit Sub
            End If
            LabelJudgeCount.Text = NgCount.ToString

            '----------------------------------
            ' ステータスの取得
            '----------------------------------
            Ret = DigGetStatus(Id, Status)
            If Ret <> 0 Then
                Ret2 = DigGetErrorString(Ret, ErrorString)
                TextInfo.Text = "DigGetStatus = " & Ret & " : " & ErrorString.ToString()
                Exit Sub
            End If
            LabelStatus.Text = Hex(Status) & "H"
        Loop While (Status And DIG_STATUS_BUSY) = DIG_STATUS_BUSY

        '----------------------------------
        ' 波形判定の失敗回数を取得
        '----------------------------------
        Ret = DigGetJudgeCount(Id, NgCount)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigGetJudgeCount = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
        LabelJudgeCount.Text = NgCount.ToString

        '----------------------------------
        ' 動作停止後ドライバメモリ内に残っているレコード数を取得
        '----------------------------------
        Ret = DigGetDriverMemoryRecords(Id, Records)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigGetDriverMemoryRecords = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' ドライバメモリ内にレコードが存在すれば、新しい方からデータを取得
        '----------------------------------
        If Records > 0 Or Records = -1 Then
            Ret = DigReadLatestRecords32(Id, DIG_RECORD_DATA, 1, Data, Judge, JudgeDetail)
            If Ret <> 0 Then
                Ret2 = DigGetErrorString(Ret, ErrorString)
                TextInfo.Text = "DigReadLatestRecords = " & Ret & " : " & ErrorString.ToString()
                Exit Sub
            End If
            '----------------------------------
            ' グラフの描画
            '----------------------------------
            DrawGraph()
        End If
    End Sub

    '==================================
    ' 状態取得
    '==================================
    Private Sub CommandInfo_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CommandInfo.Click
        Dim Ret As Integer        ' 戻り値
        Dim Ret2 As Integer       ' 戻り値2
        Dim ErrorString As New StringBuilder("", 256)     ' エラー文字列
        Dim Status As Integer         ' ステータス

        '----------------------------------
        ' ステータスの取得
        '----------------------------------
        Ret = DigGetStatus(Id, Status)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigGetStatus = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
        LabelStatus.Text = Hex(Status) & "H"

        TextInfo.Text = "状態取得 : 正常終了"
    End Sub

    '==================================
    ' 収集停止
    '==================================
    Private Sub CommandStop_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CommandStop.Click
        Dim Ret As Integer        ' 戻り値
        Dim Ret2 As Integer       ' 戻り値2
        Dim ErrorString As New StringBuilder("", 256)     ' エラー文字列

        Ret = DigStop(Id)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigExit = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        TextInfo.Text = "収集停止 : 正常終了"
    End Sub


    '==================================
    ' 終了処理
    '==================================
    Private Sub CommandExit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CommandExit.Click
        Dim Ret As Integer        ' 戻り値
        Dim Ret2 As Integer       ' 戻り値2
        Dim ErrorString As New StringBuilder("", 256)     ' エラー文字列

        Ret = DigExit(Id)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigExit = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        TextInfo.Text = "終了処理 : 正常終了"
    End Sub

    '==================================
    ' フォームのアンロード
    '==================================
    Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        Dim Ret As Integer        ' 戻り値
        Ret = DigExit(Id)
    End Sub

    '==================================
    ' グラフ描画
    '==================================
    Private Sub DrawGraph()
        Dim XUnit As Single       ' X軸描画インターバル
        Dim PosX As Single        ' X軸描画位置
        Dim PosY As Single        ' Y軸描画位置
        Dim OldX As Single        ' X軸座標退避用
        Dim OldY As Single        ' Y軸座標退避用
        Dim i As Integer
        Dim j As Integer

        '-----------------------------------------
        ' 現在のデータをグラフに描画します。
        '-----------------------------------------
        Dim pen As New Pen(System.Drawing.Color.Red, 1)     ' 赤色ペンの作成
        Graph.Image = bmp
        Dim grp As Graphics = Graphics.FromImage(Graph.Image)       ' ビットマップから描画対象オブジェクトを生成する
        grp.Clear(Graph.BackColor)                                  ' グラフをクリア

        XUnit = Graph.Size.Width() / CSng(MAX_SAMPLES)      ' 1データあたりの横軸移動ピクセル数を計算
        For i = 0 To 2
            OldX = 0
            Select Case i
                Case 0
                    ' 上限データの描画位置原点設定
                    OldY = Graph.Height * (Resolution - UpperData(0)) / CSng(Resolution)                    ' Y軸の初期値を計算
                Case 1
                    ' 下限データの描画位置原点設定
                    OldY = Graph.Height * (Resolution - LowerData(0)) / CSng(Resolution)                    ' Y軸の初期値を計算

                Case 2
                    ' 収集データの描画位置原点設定
                    OldY = Graph.Height * (Resolution - Data(0)) / CSng(Resolution)                 ' Y軸の初期値を計算
            End Select
            If OldY >= Graph.Height Then
                OldY = Graph.Height - 1               ' Y軸の初期値がグラフの描画領域を超える場合、位置を修正
            End If
            If OldY < 0 Then
                OldY = 0                 ' Y軸の初期値がグラフの描画領域を下回る場合、位置を修正
            End If

            '----------------------------------------------------
            ' 収集データ数分だけ、描画処理を繰り返します。
            '----------------------------------------------------
            For j = 1 To MAX_SAMPLES - 1
                PosX = OldX + XUnit               ' X軸の座標計算
                Select Case i
                    Case 0
                        ' 上限データの描画位置設定
                        PosY = Graph.Height * (Resolution - UpperData(j)) / CSng(Resolution)                          ' Y軸の描画位置を計算
                    Case 1
                        ' 下限データの描画位置設定
                        PosY = Graph.Height * (Resolution - LowerData(j)) / CSng(Resolution)                          ' Y軸の描画位置を計算

                    Case 2
                        ' 収集データの描画位置設定
                        PosY = Graph.Height * (Resolution - Data(j)) / CSng(Resolution)                       ' Y軸の描画位置を計算
                End Select
                If PosY >= Graph.Height() Then
                    ' Y軸の座標が描画領域を超える場合
                    PosY = Graph.Height() - 1                     ' 描画位置の修正

                ElseIf PosY < 0 Then
                    ' Y軸の座標が描画領域を下回る場合
                    PosY = 0                      ' 描画位置の修正
                End If
                grp.DrawLine(pen, OldX, OldY, PosX, PosY)                ' 描画

                OldX = PosX               ' X軸の座標を描画用に保持
                OldY = PosY               ' Y軸の座標を描画用に保持
            Next j
        Next i
        pen.Dispose()        ' リソースの解放
        grp.Dispose()        ' リソースの解放
    End Sub
End Class
