﻿'============================================================================
'	FactoryIT I/F Sample Program

'		DEVICE ID:		0
'		Channel:		0ch
'=========================================================================== 
Module FitCnt

#Region "Address(Common)"

    Const FIT_IO As UInteger = &H300000
    Const FIT_IO_DEVICE_INFOR As UInteger = &H0
    Const FIT_IO_DEVICE_CONFIG As UInteger = &H1000
    Const FIT_IO_INPUT As UInteger = &H4000
    Const FIT_IO_OUTPUT As UInteger = &H5000

    Const FIT_IO_DEVICE_SIZE As UInteger = &H80
    Const FIT_PRODUCT_CATEGORY As UInteger = &H0
    Const FIT_MODULE_START As UInteger = &H10
    Const FIT_ERROR_STATUS As UInteger = &H11

#End Region

#Region "Information(Common)"

    Const FIT_PRODUCT_DIGITAL As Byte = &H1
    Const FIT_PRODUCT_ANALOG As Byte = &H2
    Const FIT_PRODUCT_COUNTER As Byte = &H3

    Const FIT_MODULE_START_OFF As Byte = &H0
    Const FIT_MODULE_START_ON As Byte = &H1

#End Region

#Region "Address(CNT)"

    Const FIT_CNT_BIT As Byte = &H12
    Const FIT_CNT_OUTPUT_SIGNAL As Byte = &H13
    Const FIT_CNT_OUTPUT_WIDTH As Byte = &H14
    Const FIT_CNT_CH_RESET As Byte = &H0
    Const FIT_CNT_INITIAL_MODE As Byte = &H1
    Const FIT_CNT_INITIAL_DATA As Byte = &H2
    Const FIT_CNT_INPUT_MODE As Byte = &H6
    Const FIT_CNT_FILTER_SET As Byte = &H7
    Const FIT_CNT_FILTER_VALUE As Byte = &H8
    Const FIT_CNT_FUNCTION As Byte = &HA
    Const FIT_CNT_RESOLUTION As Byte = &HB
    Const FIT_CNT_CLEAR_MODE As Byte = &HC
    Const FIT_CNT_DEIRECTION As Byte = &HD
    Const FIT_CNT_CLEAR_LOGIC As Byte = &HE
    Const FIT_CNT_CLEAR_INPUT As Byte = &HF

#End Region

    Sub Main()

        '======================================================================
        ' Sample 設定
        '======================================================================
        Dim FIT_SAMPLE_IP_ADDRESS() As Byte = {192, 168, 0, 169}
        Const FIT_SAMPLE_PORT As UShort = &H5007
        Const FIT_SAMPLE_DEVICE_ID As Byte = 0
        Const FIT_SAMPLE_CH As Byte = 0


        '======================================================================
        ' 各変数宣言
        '======================================================================
        Dim dwVaBase As UInteger
        Dim dwVaOffset As UInteger
        Dim dwVaChOffset As UInteger
        Dim hHandle As UShort
        Dim wStatus As UShort
        Dim byCategory(5) As Byte
        Dim byModuleStart(5) As Byte
        Dim byData(100) As Byte
        Dim byErrorStatus(5) As Byte


        '======================================================================
        ' Open
        '======================================================================
        hHandle = FIT_Open(FIT_SAMPLE_IP_ADDRESS, FIT_SAMPLE_PORT, Nothing)
        If hHandle = 0 Then
            Console.WriteLine("Error! FIT_Open = " + String.Format("{0:X4}", hHandle) + "(H)")
            Return
        End If

        dwVaOffset = FIT_IO_DEVICE_SIZE * FIT_SAMPLE_DEVICE_ID     'Offset Address


        '======================================================================
        ' Read 'Category'
        '======================================================================
        dwVaBase = FIT_IO + FIT_IO_DEVICE_CONFIG
        wStatus = FIT_Read(hHandle, dwVaBase + dwVaOffset + FIT_PRODUCT_CATEGORY, 1, byCategory)

        If wStatus <> 0 Then
            Console.WriteLine("Error! FIT_Read = " + String.Format("{0:X4}", wStatus) + "(H)")
            Return
        End If

        If byCategory(0) <> FIT_PRODUCT_COUNTER Then
            Console.WriteLine("Error! Category = " + String.Format("{0:X2}", byCategory(0)) + "(H)")
            Return
        End If

        dwVaChOffset = &H20 + &H18 * FIT_SAMPLE_CH     'Channel Offset


        '======================================================================
        ' Read 'Channel Configuration'
        '======================================================================
        wStatus = FIT_Read(hHandle, dwVaBase + dwVaOffset + dwVaChOffset, &H18, byData)

        If wStatus <> 0 Then
            Console.WriteLine("Error!FIT_Read=" + String.Format("{0:X4}", wStatus) + "(H)")
        End If


        '======================================================================
        ' Configuration Data Set
        '======================================================================
        byData(FIT_CNT_FUNCTION) = &H1


        '======================================================================
        ' Write 'Channel Configuration'
        '======================================================================
        wStatus = FIT_Write(hHandle, dwVaBase + dwVaOffset + dwVaChOffset, &H18, byData)

        If wStatus <> 0 Then
            Console.WriteLine("Error!FIT_Write=" + String.Format("{0:X4}", wStatus) + "(H)")
        End If


        '======================================================================
        ' Write 'Compare Data'
        '======================================================================
        dwVaBase = FIT_IO + FIT_IO_OUTPUT
        dwVaChOffset = 8 * FIT_SAMPLE_CH
        byData(0) = &HE8
        byData(1) = &H3
        byData(2) = &H0
        byData(3) = &H0
        wStatus = FIT_Write(hHandle, dwVaBase + dwVaOffset + dwVaChOffset, 4, byData)

        If wStatus <> 0 Then
            Console.WriteLine("Error!FIT_Write=" + String.Format("{0:X4}", wStatus) + "(H)")
        End If


        '======================================================================
        ' Write 'Module Start'
        '======================================================================
        dwVaBase = FIT_IO + FIT_IO_DEVICE_CONFIG
        byModuleStart(0) = FIT_MODULE_START_ON
        wStatus = FIT_Write(hHandle, dwVaBase + dwVaOffset + FIT_MODULE_START, 1, byModuleStart)

        If wStatus <> 0 Then
            Console.WriteLine("Error!FIT_Write=" + String.Format("{0:X4}", wStatus) + "(H)")
        End If


        '======================================================================
        ' Read 'Error Status'
        '======================================================================
        dwVaBase = FIT_IO + FIT_IO_DEVICE_INFOR
        wStatus = FIT_Read(hHandle, dwVaBase + dwVaOffset + FIT_ERROR_STATUS, 1, byErrorStatus)
        If wStatus <> 0 Then
            Console.WriteLine("Error!FIT_Read=" + String.Format("{0:X4}", wStatus) + "(H)")
            FIT_Close(hHandle)
            Return
        End If

        If byErrorStatus(0) <> &H0 Then
            Console.WriteLine("Error! Error Status = " + String.Format("{0:X2}", byErrorStatus(0)) + "(H)")
            FIT_Close(hHandle)
            Return
        End If

        '======================================================================
        ' Read 'Counter Data'
        '======================================================================
        dwVaBase = FIT_IO + FIT_IO_INPUT

        Console.WriteLine("- Hit Any Key:Stop -")
        While Console.KeyAvailable = False
            wStatus = FIT_Read(hHandle, dwVaBase + dwVaOffset + dwVaChOffset, 8, byData)

            If wStatus <> 0 Then
                Console.WriteLine("Error!FIT_Read=" + String.Format("{0:X4}", wStatus) + "(H)")
            End If
            Console.WriteLine("Counter CH" + String.Format(FIT_SAMPLE_CH) + " Data:" + String.Format("{0:X2}", byData(3)) + String.Format("{0:X2}", byData(2)) + String.Format("{0:X2}", byData(1)) + String.Format("{0:X2}", byData(0)))
            Console.SetCursorPosition(0, 1)

        End While


        '======================================================================
        ' Close
        '======================================================================
        FIT_Close(hHandle)

    End Sub

End Module
