' ' =======================================================================================
'
'	Copyright (c) 2017 Hamamatsu Photonics K.K.
'
'	Project		: MS Minispectrometer Console Sample Application(VB.NET)
'
'	.NET Framework	: .NET Framework 3.5
'
'========================================================================================

'-----------------------------------------------------------------------------
' External Reference DLL
'-----------------------------------------------------------------------------
Imports HMSUSB2_DLL

'-----------------------------------------------------------------------------
' Namespace
'-----------------------------------------------------------------------------
Namespace SampleAPL

    Module Module1
        '-----------------------------------------------------------------------------
        ' Definition
        '-----------------------------------------------------------------------------
        Public Const D_MAX_CAMERA_DEVICE As Integer = 8         'Max MiniSpec Count
        Public Const D_SERIAL_NUMBERSIZE As Integer = 16        'Serial Number Size
        Public Const D_INTEGRATION_DATASIZE As Integer = 256    'Data Size
        Public Const D_REPEAT_COUNT As Integer = 5              'Repeat Count

        '-----------------------------------------------------------------------------
        ' Main Function
        '----------------------------------------------------------------------------
        Sub Main()
            Dim lReturn As Long

            lReturn = 0

            Console.WriteLine("Hamamatsu Minispectrometer for MS Sample Application")

            'Create object of HSSUSB2 Class
            Dim objHMSUSB2 As New HMSUSB2()
            If IsDBNull(objHMSUSB2) Then
                Return
            End If
            'Start Measurement
            lReturn = StartMeasure(objHMSUSB2)
            If lReturn <> E_HMSErrCode.HMS_success Then
                Return
            End If
        End Sub

        '-----------------------------------------------------------------------------
        ' Measurement Start Function
        '----------------------------------------------------------------------------
        Public Function StartMeasure(ByVal objHMSUSB2 As HMSUSB2) As Long

            Dim lReturn As Integer
            Dim nRepeatCount As Integer
            Dim strDevice As String
            Dim usNum As UShort
            Dim ulIntegrationTime As Integer
            Dim aryConnList() As String
            Dim aryusIntegrationData() As UShort

            'Argument error check
            If IsDBNull(objHMSUSB2) Then
                Return E_HMSErrCode.HMS_unsuccess
            End If
            Console.WriteLine("Opening Device............")

            'Initialization
            lReturn = objHMSUSB2.HMS_initialize()
            If lReturn <> E_HMSErrCode.HMS_success Then
                Console.WriteLine("Error code 0x{0:x04}: Dll Can not be Initialized.", lReturn)
                Return lReturn
            End If

            'Device ID storage area allocation
            ReDim aryConnList(D_MAX_CAMERA_DEVICE)

            'Get connected device list
            lReturn = objHMSUSB2.HMS_getDeviceList(aryConnList, usNum)
            If lReturn <> E_HMSErrCode.HMS_success Then
                Console.WriteLine("Error code 0x{0:x04}: Module Connection list failed.", lReturn)
                Return lReturn
            End If

            If usNum > 0 Then
                strDevice = aryConnList(0)
            Else
                Console.WriteLine("Module Connection list Nothing.")
                Return lReturn
            End If

            'Open device
            lReturn = objHMSUSB2.HMS_openDevice(strDevice)
            If lReturn <> E_HMSErrCode.HMS_success Then
                Console.WriteLine("Error code 0x{0:x04}: Device can not be open.", lReturn)
                Return lReturn
            End If

            'Check device
            lReturn = objHMSUSB2.HMS_checkDevice(strDevice)
            If lReturn <> E_HMSErrCode.HMS_success Then
                Console.WriteLine("Error code 0x{0:x04}: Device Error.", lReturn)
                Return lReturn
            End If

            If lReturn = E_HMSErrCode.HMS_success Then
                'Get Integration Time
                lReturn = objHMSUSB2.HMS_getIntegrationTime(strDevice, ulIntegrationTime)
                If lReturn <> E_HMSErrCode.HMS_success Then
                    Console.WriteLine("Error code 0x{0:x04}: HMS_getIntegrationTime failed.", lReturn)
                    Return lReturn
                End If
                'Data acquisition is reapeated 5 times while determining status of capture  
                Do
                    'Allocate a buffer to store data
                    ReDim aryusIntegrationData(D_INTEGRATION_DATASIZE)
                    'Get data from device
                    lReturn = objHMSUSB2.HMS_getSensorData(strDevice, ulIntegrationTime, aryusIntegrationData)
                    If lReturn <> E_HMSErrCode.HMS_success Then
                        Console.WriteLine("Error code 0x{0:x04}: HMS_getSensorData failed.", lReturn)
                        Return lReturn
                    End If
                    'Display Data
                    DisplayData(aryusIntegrationData)
                    nRepeatCount += 1
                    System.Threading.Thread.Sleep(1)
                Loop While nRepeatCount < D_REPEAT_COUNT
            End If

            'Close device
            lReturn = objHMSUSB2.HMS_closeDevice(strDevice)
            If lReturn <> E_HMSErrCode.HMS_success Then
                Console.WriteLine("Error code 0x{0:x04}: Device can not be closed.", lReturn)
                Return lReturn
            End If
            'Uninitialization
            lReturn = objHMSUSB2.HMS_unininitalize()
            If lReturn <> E_HMSErrCode.HMS_success Then
                Console.WriteLine("Error code 0x{0:x04}: Uninitialize failed.", lReturn)
            End If

        End Function

        '-----------------------------------------------------------------------------
        ' Display Data Function 
        '----------------------------------------------------------------------------
        Public Function DisplayData(ByVal aryusImageData() As UShort) As Boolean
            Dim strData As String
            Dim nData As Integer
            Dim nIdx As Integer
            nIdx = 0
            strData = ""
            nData = 0
            'Display data on Console 
            For nIdx = 0 To (aryusImageData.Length - 1)
                nData = aryusImageData(nIdx)
                strData = String.Format("{0:X04} ", nData)
                Console.Write(strData)
                strData = ""
            Next
            Console.WriteLine("")
        End Function
    End Module
End Namespace


