﻿Option Strict Off
Option Explicit On 
Imports System.Text

Friend Class Sampling
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

    End Sub

    'Form overrides dispose to clean up the component list.
    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

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.
    'Do not modify it using the code editor.
    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(16, 23)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(80, 17)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Device Name:"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(16, 215)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(88, 16)
        Me.Label2.TabIndex = 0
        Me.Label2.Text = "Transfer Count:"
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(16, 254)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(48, 12)
        Me.Label3.TabIndex = 0
        Me.Label3.Text = "Status:"
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(16, 289)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(40, 15)
        Me.Label4.TabIndex = 0
        Me.Label4.Text = "Error:"
        '
        'Label5
        '
        Me.Label5.Location = New System.Drawing.Point(16, 327)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(72, 18)
        Me.Label5.TabIndex = 0
        Me.Label5.Text = "Return Code:"
        '
        'Edit_DevName
        '
        Me.Edit_DevName.Location = New System.Drawing.Point(104, 21)
        Me.Edit_DevName.Name = "Edit_DevName"
        Me.Edit_DevName.Size = New System.Drawing.Size(128, 20)
        Me.Edit_DevName.TabIndex = 1
        Me.Edit_DevName.Text = "TextBox1"
        '
        'Edit_TransCount
        '
        Me.Edit_TransCount.Location = New System.Drawing.Point(104, 213)
        Me.Edit_TransCount.Name = "Edit_TransCount"
        Me.Edit_TransCount.Size = New System.Drawing.Size(232, 20)
        Me.Edit_TransCount.TabIndex = 1
        Me.Edit_TransCount.Text = "TextBox1"
        '
        'Edit_Status
        '
        Me.Edit_Status.Location = New System.Drawing.Point(104, 251)
        Me.Edit_Status.Name = "Edit_Status"
        Me.Edit_Status.Size = New System.Drawing.Size(232, 20)
        Me.Edit_Status.TabIndex = 1
        Me.Edit_Status.Text = "TextBox1"
        '
        'Edit_Error
        '
        Me.Edit_Error.Location = New System.Drawing.Point(104, 289)
        Me.Edit_Error.Name = "Edit_Error"
        Me.Edit_Error.Size = New System.Drawing.Size(232, 20)
        Me.Edit_Error.TabIndex = 1
        Me.Edit_Error.Text = "TextBox1"
        '
        'Edit_Return
        '
        Me.Edit_Return.Location = New System.Drawing.Point(104, 327)
        Me.Edit_Return.Name = "Edit_Return"
        Me.Edit_Return.Size = New System.Drawing.Size(232, 20)
        Me.Edit_Return.TabIndex = 1
        Me.Edit_Return.Text = "TextBox1"
        '
        'Button_DioInit
        '
        Me.Button_DioInit.Location = New System.Drawing.Point(344, 19)
        Me.Button_DioInit.Name = "Button_DioInit"
        Me.Button_DioInit.Size = New System.Drawing.Size(104, 28)
        Me.Button_DioInit.TabIndex = 2
        Me.Button_DioInit.Text = "DioInit"
        '
        'Button_Condition
        '
        Me.Button_Condition.Location = New System.Drawing.Point(344, 57)
        Me.Button_Condition.Name = "Button_Condition"
        Me.Button_Condition.Size = New System.Drawing.Size(104, 28)
        Me.Button_Condition.TabIndex = 2
        Me.Button_Condition.Text = "Set Conditions..."
        '
        'Button_Start
        '
        Me.Button_Start.Location = New System.Drawing.Point(344, 95)
        Me.Button_Start.Name = "Button_Start"
        Me.Button_Start.Size = New System.Drawing.Size(104, 28)
        Me.Button_Start.TabIndex = 2
        Me.Button_Start.Text = "Start Transfer"
        '
        'Button_Stop
        '
        Me.Button_Stop.Location = New System.Drawing.Point(344, 133)
        Me.Button_Stop.Name = "Button_Stop"
        Me.Button_Stop.Size = New System.Drawing.Size(104, 28)
        Me.Button_Stop.TabIndex = 2
        Me.Button_Stop.Text = "Stop Transfer"
        '
        'Button_Data
        '
        Me.Button_Data.Location = New System.Drawing.Point(344, 171)
        Me.Button_Data.Name = "Button_Data"
        Me.Button_Data.Size = New System.Drawing.Size(104, 28)
        Me.Button_Data.TabIndex = 2
        Me.Button_Data.Text = "Get Data..."
        '
        'Button_Count
        '
        Me.Button_Count.Location = New System.Drawing.Point(344, 209)
        Me.Button_Count.Name = "Button_Count"
        Me.Button_Count.Size = New System.Drawing.Size(104, 28)
        Me.Button_Count.TabIndex = 2
        Me.Button_Count.Text = "Transfer Count"
        '
        'Button_Status
        '
        Me.Button_Status.Location = New System.Drawing.Point(344, 247)
        Me.Button_Status.Name = "Button_Status"
        Me.Button_Status.Size = New System.Drawing.Size(104, 28)
        Me.Button_Status.TabIndex = 2
        Me.Button_Status.Text = "Transfer Status"
        '
        'Button_DioExit
        '
        Me.Button_DioExit.Location = New System.Drawing.Point(344, 285)
        Me.Button_DioExit.Name = "Button_DioExit"
        Me.Button_DioExit.Size = New System.Drawing.Size(104, 28)
        Me.Button_DioExit.TabIndex = 2
        Me.Button_DioExit.Text = "DioExit"
        '
        'Button_Exit
        '
        Me.Button_Exit.Location = New System.Drawing.Point(344, 323)
        Me.Button_Exit.Name = "Button_Exit"
        Me.Button_Exit.Size = New System.Drawing.Size(104, 28)
        Me.Button_Exit.TabIndex = 2
        Me.Button_Exit.Text = "Exit"
        '
        'Sampling
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(472, 366)
        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 = "Sampling"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Public Shared Id As Short               'Device Id
    Dim Ret As Integer                      'Return Code
    Dim szError As New StringBuilder("", 256)
    Dim gCh As GCHandle
    Dim pBmBuf As IntPtr
    Const DATA_SIZE As Short = 1000         'Data Size
    Dim BmBuffer(DATA_SIZE) As Integer      'Data Buffer
    Dim gConditionDlg As New ConditionDlg()

    '=========================================================
    ' Dialog initialization
    '=========================================================
    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

        '--------------------------------
        ' Screen item initialization
        '--------------------------------
        Edit_DevName.Text = "DIO000"
        Edit_TransCount.Text = ""
        Edit_Status.Text = ""
        Edit_Error.Text = ""
        Edit_Return.Text = ""
        '--------------------------------
        ' Pinned type handle is allocated to BmBuffer
        '--------------------------------
        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
        '--------------------------------
        ' Execute initialization function and get ID
        '--------------------------------
        Ret = DioInit(Edit_DevName.Text, Id)
        '--------------------------------
        ' Error process
        '--------------------------------
        Call DioGetErrorString(Ret, szError)
        Edit_Return.Text = "DioInit Ret = " & Ret & ":" & szError.ToString()
    End Sub

    '=========================================================
    ' Show conditionDlg window
    '=========================================================
    Private Sub BUTTON_CONDITION_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Condition.Click
        gConditionDlg.Show()
    End Sub

    '=========================================================
    ' Start transfer
    '=========================================================
    Private Sub BUTTON_START_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Start.Click

        '--------------------------------
        ' Screen item initialization
        '--------------------------------
        Edit_TransCount.Text = ""
        Edit_Status.Text = ""
        Edit_Error.Text = ""
        '--------------------------------
        ' Set stop message
        '--------------------------------
        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
        '--------------------------------
        ' Set notification of message
        '--------------------------------
        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
        '--------------------------------
        ' Reset FIFO (input)
        '--------------------------------
        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 is initialized by 0
        '--------------------------------
        System.Array.Clear(BmBuffer, 0, BmBuffer.Length)
        '--------------------------------
        ' The fixed pointer to an object is acquired
        '--------------------------------
        pBmBuf = gCh.AddrOfPinnedObject()
        '--------------------------------
        ' Set data buffer
        '--------------------------------
        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
        '--------------------------------
        ' Start transfer
        '--------------------------------
        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

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

    '=========================================================
    ' Get data
    '=========================================================
    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"
        '--------------------------------
        ' Open file
        '--------------------------------
        FileOpen(1, DataFileName, OpenMode.Output)
        '--------------------------------
        ' Write data into file
        '--------------------------------
        For i = 0 To DATA_SIZE - 1
            buf = String.Format("{0:d8}: ,{1:X8}", i, BmBuffer(i))
            PrintLine(1, buf)
        Next i
        '--------------------------------
        ' Close file
        '--------------------------------
        FileClose(1)
        Shell("notepad.exe " & DataFileName, AppWinStyle.NormalFocus)
        Exit Sub
    End Sub

    '=========================================================
    ' Transfer count
    '=========================================================
    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

        '--------------------------------
        ' Get transfer count
        '--------------------------------
        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

    '=========================================================
    ' Transfer status
    '=========================================================
    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

        '--------------------------------
        ' Get transfer status
        '--------------------------------
        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
        '--------------------------------
        ' Get transfer status
        '--------------------------------
        Ret = DioDmGetStatus(Id, DIODM_DIR_IN, status, err)
        If Ret = DIO_ERR_SUCCESS Then
            '--------------------------------
            ' Status
            '--------------------------------
            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
            '--------------------------------
            ' Error
            '--------------------------------
            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 is freed
        '--------------------------------
        If gCh.IsAllocated = True Then
            gCh.Free()
        End If
        '--------------------------------
        ' Error process
        '--------------------------------
        Call DioGetErrorString(Ret, szError)
        Edit_Return.Text = "DioExit Ret = " & Ret & ":" & szError.ToString()
    End Sub

    '=========================================================
    ' Dialog 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

    '=========================================================
    ' Transfer stop message
    '=========================================================
    Protected Overrides Sub WndProc(ByRef m As Message)
        '--------------------------------
        ' Transfer count message or transfer stop message
        '--------------------------------
        If m.Msg = DIO_DMM_COUNT Or m.Msg = DIO_DMM_STOP Then
            Call BUTTON_COUNT_Click(Button_Count, New System.EventArgs())
        End If
        '--------------------------------
        ' Transfer stop message
        '--------------------------------
        If m.Msg = DIO_DMM_STOP Then
            Edit_Status.Text = "Stopped!"
        End If
        MyBase.WndProc(m)
    End Sub

End Class
