﻿Option Strict Off
Option Explicit On 
Imports System.Text

Friend Class Sampling
    Inherits System.Windows.Forms.Form

#Region " Windows フォーム デザイナで生成されたコード "

    Public Sub New()
        MyBase.New()

        ' この呼び出しは Windows フォーム デザイナで必要です。
        InitializeComponent()

        ' InitializeComponent() 呼び出しの後に初期化を追加します。

    End Sub

    ' Form は dispose をオーバーライドしてコンポーネント一覧を消去します。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    ' Windows フォーム デザイナで必要です。
    Private components As System.ComponentModel.IContainer

    ' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。
    ' Windows フォーム デザイナを使って変更してください。  
    ' コード エディタは使用しないでください。
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents Label5 As System.Windows.Forms.Label
    Friend WithEvents Button_DioInit As System.Windows.Forms.Button
    Friend WithEvents Button_Condition As System.Windows.Forms.Button
    Friend WithEvents Button_Start As System.Windows.Forms.Button
    Friend WithEvents Button_Stop As System.Windows.Forms.Button
    Friend WithEvents Button_Data As System.Windows.Forms.Button
    Friend WithEvents Button_Count As System.Windows.Forms.Button
    Friend WithEvents Button_Status As System.Windows.Forms.Button
    Friend WithEvents Button_DioExit As System.Windows.Forms.Button
    Friend WithEvents Button_Exit As System.Windows.Forms.Button
    Friend WithEvents Edit_DevName As System.Windows.Forms.TextBox
    Friend WithEvents Edit_TransCount As System.Windows.Forms.TextBox
    Friend WithEvents Edit_Status As System.Windows.Forms.TextBox
    Friend WithEvents Edit_Error As System.Windows.Forms.TextBox
    Friend WithEvents Edit_Return As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.Label4 = New System.Windows.Forms.Label()
        Me.Label5 = New System.Windows.Forms.Label()
        Me.Edit_DevName = New System.Windows.Forms.TextBox()
        Me.Edit_TransCount = New System.Windows.Forms.TextBox()
        Me.Edit_Status = New System.Windows.Forms.TextBox()
        Me.Edit_Error = New System.Windows.Forms.TextBox()
        Me.Edit_Return = New System.Windows.Forms.TextBox()
        Me.Button_DioInit = New System.Windows.Forms.Button()
        Me.Button_Condition = New System.Windows.Forms.Button()
        Me.Button_Start = New System.Windows.Forms.Button()
        Me.Button_Stop = New System.Windows.Forms.Button()
        Me.Button_Data = New System.Windows.Forms.Button()
        Me.Button_Count = New System.Windows.Forms.Button()
        Me.Button_Status = New System.Windows.Forms.Button()
        Me.Button_DioExit = New System.Windows.Forms.Button()
        Me.Button_Exit = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(24, 20)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(64, 16)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "デバイス名："
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(24, 213)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(72, 16)
        Me.Label2.TabIndex = 0
        Me.Label2.Text = "転送カウント："
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(24, 245)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(64, 16)
        Me.Label3.TabIndex = 0
        Me.Label3.Text = "ステータス："
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(24, 277)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(40, 16)
        Me.Label4.TabIndex = 0
        Me.Label4.Text = "エラー："
        '
        'Label5
        '
        Me.Label5.Location = New System.Drawing.Point(24, 309)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(48, 16)
        Me.Label5.TabIndex = 0
        Me.Label5.Text = "戻り値："
        '
        'Edit_DevName
        '
        Me.Edit_DevName.Location = New System.Drawing.Point(96, 18)
        Me.Edit_DevName.Name = "Edit_DevName"
        Me.Edit_DevName.Size = New System.Drawing.Size(128, 19)
        Me.Edit_DevName.TabIndex = 1
        Me.Edit_DevName.Text = "TextBox1"
        '
        'Edit_TransCount
        '
        Me.Edit_TransCount.Location = New System.Drawing.Point(96, 212)
        Me.Edit_TransCount.Name = "Edit_TransCount"
        Me.Edit_TransCount.Size = New System.Drawing.Size(232, 19)
        Me.Edit_TransCount.TabIndex = 1
        Me.Edit_TransCount.Text = "TextBox1"
        '
        'Edit_Status
        '
        Me.Edit_Status.Location = New System.Drawing.Point(96, 244)
        Me.Edit_Status.Name = "Edit_Status"
        Me.Edit_Status.Size = New System.Drawing.Size(232, 19)
        Me.Edit_Status.TabIndex = 1
        Me.Edit_Status.Text = "TextBox1"
        '
        'Edit_Error
        '
        Me.Edit_Error.Location = New System.Drawing.Point(96, 276)
        Me.Edit_Error.Name = "Edit_Error"
        Me.Edit_Error.Size = New System.Drawing.Size(232, 19)
        Me.Edit_Error.TabIndex = 1
        Me.Edit_Error.Text = "TextBox1"
        '
        'Edit_Return
        '
        Me.Edit_Return.Location = New System.Drawing.Point(96, 308)
        Me.Edit_Return.Name = "Edit_Return"
        Me.Edit_Return.Size = New System.Drawing.Size(232, 19)
        Me.Edit_Return.TabIndex = 1
        Me.Edit_Return.Text = "TextBox1"
        '
        'Button_DioInit
        '
        Me.Button_DioInit.Location = New System.Drawing.Point(336, 16)
        Me.Button_DioInit.Name = "Button_DioInit"
        Me.Button_DioInit.Size = New System.Drawing.Size(104, 24)
        Me.Button_DioInit.TabIndex = 2
        Me.Button_DioInit.Text = "DioInit"
        '
        'Button_Condition
        '
        Me.Button_Condition.Location = New System.Drawing.Point(336, 52)
        Me.Button_Condition.Name = "Button_Condition"
        Me.Button_Condition.Size = New System.Drawing.Size(104, 24)
        Me.Button_Condition.TabIndex = 2
        Me.Button_Condition.Text = "条件設定..."
        '
        'Button_Start
        '
        Me.Button_Start.Location = New System.Drawing.Point(336, 88)
        Me.Button_Start.Name = "Button_Start"
        Me.Button_Start.Size = New System.Drawing.Size(104, 24)
        Me.Button_Start.TabIndex = 2
        Me.Button_Start.Text = "転送スタート"
        '
        'Button_Stop
        '
        Me.Button_Stop.Location = New System.Drawing.Point(336, 124)
        Me.Button_Stop.Name = "Button_Stop"
        Me.Button_Stop.Size = New System.Drawing.Size(104, 24)
        Me.Button_Stop.TabIndex = 2
        Me.Button_Stop.Text = "転送ストップ"
        '
        'Button_Data
        '
        Me.Button_Data.Location = New System.Drawing.Point(336, 160)
        Me.Button_Data.Name = "Button_Data"
        Me.Button_Data.Size = New System.Drawing.Size(104, 24)
        Me.Button_Data.TabIndex = 2
        Me.Button_Data.Text = "取得データ..."
        '
        'Button_Count
        '
        Me.Button_Count.Location = New System.Drawing.Point(336, 196)
        Me.Button_Count.Name = "Button_Count"
        Me.Button_Count.Size = New System.Drawing.Size(104, 24)
        Me.Button_Count.TabIndex = 2
        Me.Button_Count.Text = "転送カウント"
        '
        'Button_Status
        '
        Me.Button_Status.Location = New System.Drawing.Point(336, 232)
        Me.Button_Status.Name = "Button_Status"
        Me.Button_Status.Size = New System.Drawing.Size(104, 24)
        Me.Button_Status.TabIndex = 2
        Me.Button_Status.Text = "転送ステータス"
        '
        'Button_DioExit
        '
        Me.Button_DioExit.Location = New System.Drawing.Point(336, 268)
        Me.Button_DioExit.Name = "Button_DioExit"
        Me.Button_DioExit.Size = New System.Drawing.Size(104, 24)
        Me.Button_DioExit.TabIndex = 2
        Me.Button_DioExit.Text = "DioExit"
        '
        'Button_Exit
        '
        Me.Button_Exit.Location = New System.Drawing.Point(336, 304)
        Me.Button_Exit.Name = "Button_Exit"
        Me.Button_Exit.Size = New System.Drawing.Size(104, 24)
        Me.Button_Exit.TabIndex = 2
        Me.Button_Exit.Text = "Exit"
        '
        'Sampling
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
        Me.ClientSize = New System.Drawing.Size(472, 342)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button_DioInit, Me.Edit_DevName, Me.Label1, Me.Label2, Me.Label3, Me.Label4, Me.Label5, Me.Edit_TransCount, Me.Edit_Status, Me.Edit_Error, Me.Edit_Return, Me.Button_Condition, Me.Button_Start, Me.Button_Stop, Me.Button_Data, Me.Button_Count, Me.Button_Status, Me.Button_DioExit, Me.Button_Exit})
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "Sampling"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "サンプリング"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Public Shared Id As Short               'デバイスID
    Dim Ret As Integer                      '戻り値
    Dim szError As New StringBuilder("", 256)
    Dim gCh As GCHandle
    Dim pBmBuf As IntPtr
    Const DATA_SIZE As Short = 1000         'バスマスタ用バッファサイズ
    Dim BmBuffer(DATA_SIZE) As Integer      'バスマスタ用バッファ
    Dim gConditionDlg As New ConditionDlg()

    '=========================================================
    ' ダイアログ初期化
    '=========================================================
    Private Sub Sampling_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load

        Me.Left = (Screen.PrimaryScreen.Bounds.Width - Me.Width) / 2
        Me.Top = (Screen.PrimaryScreen.Bounds.Height - Me.Height) / 2

        '--------------------------------
        ' 画面表示の初期化
        '--------------------------------
        Edit_DevName.Text = "DIO000"
        Edit_TransCount.Text = ""
        Edit_Status.Text = ""
        Edit_Error.Text = ""
        Edit_Return.Text = ""
        '--------------------------------
        ' BmBufferにPinnedタイプハンドルを割り当てます
        '--------------------------------
        If gCh.IsAllocated = False Then
            gCh = GCHandle.Alloc(BmBuffer, GCHandleType.Pinned)
        End If
    End Sub

    '=========================================================
    ' DioInit()
    '=========================================================
    Private Sub Button_DioInit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_DioInit.Click
        '--------------------------------
        ' 初期化処理
        '--------------------------------
        Ret = DioInit(Edit_DevName.Text, Id)
        '--------------------------------
        ' エラー処理
        '--------------------------------
        Call DioGetErrorString(Ret, szError)
        Edit_Return.Text = "DioInit Ret = " & Ret & ":" & szError.ToString()
    End Sub

    '=========================================================
    ' 条件設定
    '=========================================================
    Private Sub BUTTON_CONDITION_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Condition.Click
        gConditionDlg.Show()
    End Sub

    '=========================================================
    ' 転送スタート
    '=========================================================
    Private Sub BUTTON_START_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Start.Click

        '--------------------------------
        ' 表示初期化
        '--------------------------------
        Edit_TransCount.Text = ""
        Edit_Status.Text = ""
        Edit_Error.Text = ""
        '--------------------------------
        ' 転送完了イベント設定
        '--------------------------------
        Ret = DioDmSetStopEvent(Id, DIODM_DIR_IN, Me.Handle)
        If Ret <> DIO_ERR_SUCCESS Then
            Call DioGetErrorString(Ret, szError)
            Edit_Return.Text = "DioDmSetStopEvent Ret = " & Ret & ":" & szError.ToString()
            Exit Sub
        End If
        '--------------------------------
        ' 指定個数転送完了イベント設定
        '--------------------------------
        Ret = DioDmSetCountEvent(Id, DIODM_DIR_IN, ConditionDlg.NumberOfData, Me.Handle)
        If Ret <> DIO_ERR_SUCCESS Then
            Call DioGetErrorString(Ret, szError)
            Edit_Return.Text = "DioDmSetCountEvent Ret = " & Ret & ":" & szError.ToString()
            Exit Sub
        End If
        '--------------------------------
        ' FIFOのリセット(入力)
        '--------------------------------
        Ret = DioDmFifoReset(Id, DIODM_RESET_FIFO_IN)
        If Ret <> DIO_ERR_SUCCESS Then
            Call DioGetErrorString(Ret, szError)
            Edit_Return.Text = "DioDmFifoReset Ret = " & Ret & ":" & szError.ToString()
            Exit Sub
        End If
        '--------------------------------
        ' BmBufferを初期化、0に設定します
        '--------------------------------
        System.Array.Clear(BmBuffer, 0, BmBuffer.Length)
        '--------------------------------
        ' Pinned ハンドル内のオブジェクトのアドレスを取得します
        '--------------------------------
        pBmBuf = gCh.AddrOfPinnedObject()
        '--------------------------------
        ' データバッファの設定
        '--------------------------------
        Ret = DioDmSetBuffer(Id, DIODM_DIR_IN, pBmBuf, DATA_SIZE, DIODM_WRITE_ONCE)
        If Ret <> DIO_ERR_SUCCESS Then
            Call DioGetErrorString(Ret, szError)
            Edit_Return.Text = "DioDmSetBuffer Ret = " & Ret & ":" & szError.ToString()
            Exit Sub
        End If
        '--------------------------------
        ' 転送スタート
        '--------------------------------
        Ret = DioDmTransferStart(Id, DIODM_DIR_IN)
        If (Ret <> DIO_ERR_SUCCESS) Then
            Call DioGetErrorString(Ret, szError)
            Edit_Return.Text = "DioDmTransferStart Ret = " & Ret & ":" & szError.ToString()
            Exit Sub
        End If
    End Sub

    '=========================================================
    ' 転送ストップ
    '=========================================================
    Private Sub BUTTON_STOP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Stop.Click
        '--------------------------------
        ' 転送ストップDIODM_DIR_IN
        '--------------------------------
        Ret = DioDmTransferStop(Id, DIODM_DIR_IN)
        Call DioGetErrorString(Ret, szError)
        Edit_Return.Text = "DioDmTransferStop Ret = " & Ret & ":" & szError.ToString()
    End Sub

    '=========================================================
    ' 取得データ
    '=========================================================
    Private Sub BUTTON_DATA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Data.Click
        Dim i As Short
        Dim buf As String
        Dim DataFileName As String

        DataFileName = Application.StartupPath & "\SamplingData.csv"
        '--------------------------------
        ' ファイルを開く
        '--------------------------------
        FileOpen(1, DataFileName, OpenMode.Output)
        '--------------------------------
        ' ファイルに取得データを書き込みます
        '--------------------------------
        For i = 0 To DATA_SIZE - 1
            buf = String.Format("{0:d8}: ,{1:X8}", i, BmBuffer(i))
            PrintLine(1, buf)
        Next i
        '--------------------------------
        ' ファイルを閉じる
        '--------------------------------
        FileClose(1)
        Shell("notepad.exe " & DataFileName, AppWinStyle.NormalFocus)
        Exit Sub
    End Sub

    '=========================================================
    ' 転送カウント
    '=========================================================
    Private Sub BUTTON_COUNT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Count.Click
        Dim count As Integer = 0
        Dim carry As Integer = 0

        '--------------------------------
        ' 転送カウント取得
        '--------------------------------
        Ret = DioDmGetCount(Id, DIODM_DIR_IN, count, carry)
        Call DioGetErrorString(Ret, szError)
        Edit_Return.Text = "DioDmGetCount Ret = " & Ret & ":" & szError.ToString()
        If Ret = DIO_ERR_SUCCESS Then
            Edit_TransCount.Text = CStr(count)
        Else
            Edit_TransCount.Text = ""
        End If
    End Sub

    '=========================================================
    ' 転送ステータス
    '=========================================================
    Private Sub BUTTON_STATUS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Status.Click
        Dim status As Integer
        Dim err As Integer
		Dim buf As String = String.Empty

        '--------------------------------
        ' 転送ステータス取得
        '--------------------------------
        Ret = DioDmGetStatus(Id, DIODM_DIR_IN, status, err)
        Call DioGetErrorString(Ret, szError)
        Edit_Return.Text = "DioDmGetStatus Ret = " & Ret & ":" & szError.ToString()
        If (Ret <> DIO_ERR_SUCCESS) Then
            Exit Sub
        End If
        '--------------------------------
        ' 転送ステータス取得
        '--------------------------------
        Ret = DioDmGetStatus(Id, DIODM_DIR_IN, status, err)
        If Ret = DIO_ERR_SUCCESS Then
            '--------------------------------
            ' ステータス
            '--------------------------------
            If (status And DIODM_STATUS_BMSTOP) Then buf = buf & "BmStop,"
            If (status And DIODM_STATUS_PIOSTART) Then buf = buf & "PioStart,"
            If (status And DIODM_STATUS_PIOSTOP) Then buf = buf & "PioStop,"
            If (status And DIODM_STATUS_TRGIN) Then buf = buf & "TrgIn,"
            If (status And DIODM_STATUS_OVERRUN) Then buf = buf & "Overrun"
            Edit_Status.Text = buf
            '--------------------------------
            ' エラー
            '--------------------------------
            buf = ""
            If (err And DIODM_STATUS_FIFOEMPTY) Then buf = buf & "FifoEmpty,"
            If (err And DIODM_STATUS_FIFOFULL) Then buf = buf & "FifoFull,"
            If (err And DIODM_STATUS_SGOVERIN) Then buf = buf & "S/GOverIn,"
            If (err And DIODM_STATUS_TRGERR) Then buf = buf & "TrgErr,"
            If (err And DIODM_STATUS_CLKERR) Then buf = buf & "ClkErr,"
            If (err And DIODM_STATUS_SLAVEHALT) Then buf = buf & "SlaveHalt,"
            If (err And DIODM_STATUS_MASTERHALT) Then buf = buf & "MasterHalt"
            Edit_Error.Text = buf
        Else
            Edit_Status.Text = ""
            Edit_Error.Text = ""
        End If
    End Sub

    '=========================================================
    ' DioExit
    '=========================================================
    Private Sub BUTTON_DIO_EXIT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_DioExit.Click
        '--------------------------------
        ' DioExit()関数
        '--------------------------------
        Ret = DioExit(Id)
        '--------------------------------
        ' GCHandleを解放します
        '--------------------------------
        If gCh.IsAllocated = True Then
            gCh.Free()
        End If
        '--------------------------------
        ' エラー処理
        '--------------------------------
        Call DioGetErrorString(Ret, szError)
        Edit_Return.Text = "DioExit Ret = " & Ret & ":" & szError.ToString()
    End Sub

    '=========================================================
    ' ダイアログExit
    '=========================================================
    Private Sub BUTTON_EXIT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Exit.Click
        '--------------------------------
        ' DioExit()関数
        '--------------------------------
        Ret = DioExit(Id)
        End
    End Sub

    '=========================================================
    ' 転送完了メッセージ
    '=========================================================
    Protected Overrides Sub WndProc(ByRef m As Message)
        '--------------------------------
        ' 転送カウントメッセージ or 転送完了メッセージ
        '--------------------------------
        If m.Msg = DIO_DMM_COUNT Or m.Msg = DIO_DMM_STOP Then
            Call BUTTON_COUNT_Click(Button_Count, New System.EventArgs())
        End If
        '--------------------------------
        ' 転送完了メッセージ
        '--------------------------------
        If m.Msg = DIO_DMM_STOP Then
            Edit_Status.Text = "Stopped!"
        End If
        MyBase.WndProc(m)
    End Sub

End Class
