﻿'============================================================================
'	FactoryIT RemoteI/O I/F Asist_Server Sample Program
'
'		UNIT_ID			0x10
'		DEVICE ID:		0	DIO-8/8(FIT)
'============================================================================
Module FitrAsDio

#Region "Address(Common)"

    Const FIT_IO As UInteger = &H300000
    Const FIT_IO_UNIT_SIZE As UInteger = &H10000
    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_INPUT_CHANNELS As UInteger = &H4
    Const FIT_OUTPUT_CHANNELS As UInteger = &H6
    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

    Sub Main()
        '======================================================================
        ' Sample 設定
        '======================================================================
        Dim FIT_SAMPLE_IP_ADDRESS() As Byte = {192, 168, 0, 50}
        Const FIT_SAMPLE_PORT As UShort = &H5007
        Const FIT_SAMPLE_GROUP_ID As Byte = &H0
        Const FIT_SAMPLE_ASIST_ID As Byte = &H10
        Const FIT_SAMPLE_UNIT_ID As Byte = &H0
        Const FIT_SAMPLE_DEVICE_ID As Byte = 0


        '======================================================================
        ' 各変数宣言
        '======================================================================
        Dim dwVaBase As UInteger
        Dim dwVaOffset As UInteger
        Dim wStatus As UShort
        Dim byCategory(5) As Byte
        Dim byInputChannels(5) As Byte
        Dim byOutputChannels(5) As Byte
        Dim byData(100) As Byte
        Dim byChCount As Byte


        '======================================================================
        ' Open
        '======================================================================
        wStatus = FITR_Open(FIT_SAMPLE_GROUP_ID, FIT_SAMPLE_ASIST_ID, Nothing)
        If wStatus <> 0 Then
            Console.WriteLine("Error! FIT_Open = " + String.Format("{0:X4}", wStatus) + "(H)")
            Return
        End If

        dwVaOffset = FIT_IO_DEVICE_SIZE * FIT_SAMPLE_DEVICE_ID     'Offset Address


        '======================================================================
        ' Read 'Category'
        '======================================================================
        dwVaBase = FIT_IO + (FIT_IO_UNIT_SIZE * FIT_SAMPLE_UNIT_ID) + FIT_IO_DEVICE_CONFIG
        wStatus = FITR_Read(FIT_SAMPLE_GROUP_ID, FIT_SAMPLE_ASIST_ID, dwVaBase + dwVaOffset + FIT_PRODUCT_CATEGORY, 1, byCategory)

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

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


        '======================================================================
        ' Read 'Input Channels'
        '======================================================================
        wStatus = FITR_Read(FIT_SAMPLE_GROUP_ID, FIT_SAMPLE_ASIST_ID, dwVaBase + dwVaOffset + FIT_INPUT_CHANNELS, 1, byInputChannels)
        If wStatus <> 0 Then
            Console.WriteLine("Error! FITR_Read = " + String.Format("{0:X4}", wStatus) + "(H)")
            Return
        End If


        '======================================================================
        ' Read 'Output Channels'
        '======================================================================
        wStatus = FITR_Read(FIT_SAMPLE_GROUP_ID, FIT_SAMPLE_ASIST_ID, dwVaBase + dwVaOffset + FIT_OUTPUT_CHANNELS, 1, byOutputChannels)
        If wStatus <> 0 Then
            Console.WriteLine("Error! FITR_Read = " + String.Format("{0:X4}", wStatus) + "(H)")
            Return
        End If


        '======================================================================
        ' Read 'Input Data'
        '======================================================================
        dwVaBase = FIT_IO + (FIT_IO_UNIT_SIZE * FIT_SAMPLE_UNIT_ID) + FIT_IO_INPUT
        wStatus = FITR_Read(FIT_SAMPLE_GROUP_ID, FIT_SAMPLE_ASIST_ID, dwVaBase + dwVaOffset, byInputChannels(0), byData)

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

        While byChCount <> byInputChannels(0)
            Console.WriteLine("Input CH" + String.Format(byChCount) + " Data:" + String.Format("{0:X2}", byData(byChCount)) + "(H)")
            byChCount += 1
        End While

        byChCount = 0


        '======================================================================
        ' Write 'Output Data'
        '======================================================================
        While byChCount <> byOutputChannels(0)
            byData(byChCount) = &H55
            Console.WriteLine("Output CH" + String.Format(byChCount) + " Data:" + String.Format("{0:X2}", byData(byChCount)) + "(H)")
            byChCount += 1
        End While

        dwVaBase = FIT_IO + (FIT_IO_UNIT_SIZE * FIT_SAMPLE_UNIT_ID) + FIT_IO_OUTPUT
        wStatus = FITR_Write(FIT_SAMPLE_GROUP_ID, FIT_SAMPLE_ASIST_ID, dwVaBase + dwVaOffset, byOutputChannels(0), byData)

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


        '======================================================================
        ' Close
        '======================================================================
        FITR_Close(FIT_SAMPLE_GROUP_ID, FIT_SAMPLE_ASIST_ID)

    End Sub

End Module
