﻿'===========================================================================
'	FactoryIT I/F Sample Program
'
'		DEVICE ID:		0
'		Channel:		0 to 3ch
'		Range:			-10 to 10V
'===========================================================================
Module FitDai

#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(AIO)"

    Const FIT_AIO_AI_BIT As Byte = &H12
    Const FIT_AIO_AI_RANGE As Byte = &H13
    Const FIT_AIO_AI_MODE As Byte = &H14
    Const FIT_AIO_AO_BIT As Byte = &H1A
    Const FIT_AIO_AO_RANGE As Byte = &H1B

#End Region

#Region "Information(AIO)"

    Const FIT_AIO_RANGE_PM10 As Byte = 0
    Const FIT_AIO_RANGE_PM5 As Byte = 1
    Const FIT_AIO_RANGEP10 As Byte = 50
    Const FIT_AIO_RANGE_P5 As Byte = 51
    Const FIT_AIO_RANGE_P20MA As Byte = 100

#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


        '======================================================================
        ' 各変数宣言
        '======================================================================
        Dim dwVaBase As UInteger
        Dim dwVaOffset As UInteger
        Dim hHandle As UShort
        Dim wStatus As UShort
        Dim byCategory(5) As Byte
        Dim byModuleStart(5) As Byte
        Dim byRange(5) As Byte
        Dim byData(100) As Byte
        Dim byErrorStatus(5) As Byte
        Dim byChCount 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)")
            FIT_Close(hHandle)
            Return
        End If

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


        '======================================================================
        ' Write 'D/A Range'
        '======================================================================
        byRange(0) = FIT_AIO_RANGE_PM10
        wStatus = FIT_Write(hHandle, dwVaBase + dwVaOffset + FIT_AIO_AO_RANGE, 1, byRange)

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


        '======================================================================
        ' Write 'Module Start'
        '======================================================================
        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

        '======================================================================
        ' Write 'D/A Data'
        '======================================================================
        For byChCount = 0 To 3
            'Output Data:10V
            byData(byChCount * 2 + 1) = &HF
            byData(byChCount * 2) = &HFF
            Console.WriteLine("D/A CH" + String.Format(byChCount) + " DATA:" + String.Format("{0:X2}", byData(byChCount * 2 + 1)) + String.Format("{0:X2}", byData(byChCount * 2)))
        Next byChCount

        dwVaBase = FIT_IO + FIT_IO_OUTPUT
        wStatus = FIT_Write(hHandle, dwVaBase + dwVaOffset, 2 * 4, byData)

        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)")
        End If

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


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

    End Sub
End Module
